Load pictures in Unity two mode

Source: Internet
Author: User

See the grass and sheep hair pictures, is also really lazy.
Remember to write the weather forecast now forget clean, good memory than rotten pen, or should remember more. I'll shoot you down, okay?
usingSystem;usingSystem.Collections;usingSystem.Collections.Generic;usingUnityengine;usingUnityengine.ui;usingSystem.IO; Public classreadpictures:monobehaviour{Privateimage Image; Private stringLoadPath ="d:/someother/ngui/assets/streamingassets/test.jpg";//path loaded by IO mode    Private stringPicpathwww ="test.jpg";//load mode path of www//Use this for initialization    Private voidStart () {image= getcomponent<image>(); //Io method loading speed fast//Loadbyio (); //WWW loading speed is slowloadbywww (); }    Private voidLoadbyio () {DoubleStartTime = (Double) Time.time; //Create a file streamFileStream FileStream =NewFileStream (LoadPath, FileMode.Open, FileAccess.Read); Filestream.seek (0, Seekorigin.begin); //create a buffer for file length        byte[] bytes =New byte[Filestream.length]; //Read FileFileStream.Read (Bytes,0, (int) filestream.length); //release file read LiuFilestream.close ();        Filestream.dispose (); FileStream=NULL; //Create Texture        intwidth = -; intHeight =372; Texture2d texture2d=Newtexture2d (width, height);        Texture2d.loadimage (bytes); Sprite Sprite= Sprite.create (Texture2d,NewRect (0,0, Texture2d.width, texture2d.height),NewVector2 (0.5f,0.5f)); Image.sprite=Sprite; DoubleTime = (Double) Time.time-StartTime; Debug.Log ("io load Time:"+Time ); }    Private voidloadbywww () {Startcoroutine (Load ()); }    Private stringURL ="file://D:/SOmeOther/NGUI/Assets/StreamingAssets/";//private String url = "Https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1502532130856&di=7135149ed906483861efdfc9770def3b& Imgtype=0&src=http%3a%2f%2fwww.newasp.net%2fattachment%2fsoft%2f2017%2f0811%2f144057_83971519.png "; Of course, you can change the URL of the network image to load the network pictures    PrivateIEnumerator Load () {DoubleStartTime = (Double) Time.time; www www=NewWWW (URL + picpathwww);//only URLs can be placed//www www = new www (URL);//can only be placed URL here to change the URL of the network        yield returnwww; if(www!=NULL&&string. IsNullOrEmpty (Www.error)) {texture2d Texture=www.texture; //Create a SpriteSprite Sprite = sprite.create (texture,NewRect (0,0, Texture.width,texture.height),NewVector2 (0.5f,0.5f) ); Image.sprite=Sprite; DoubleTime = (Double) Time.time-StartTime; Debug.Log ("www load time:"+Time ); }    }}

Loading pictures in Unity two

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.