Unity3d-www for picture resource display and save and local loading

Source: Internet
Author: User

This article fixed connection: http://blog.csdn.net/u013108312/article/details/52712844

www implementation of picture resource display and save and local loading

Using Unityengine;Using System.Collections;Using System.IO;Using Unityeditor;Enum getpictype{DownLoad =0, Localload,}PublicClass picture:monobehaviour{Here is the local IP addressString url ="Http://127.0.0.1:80000/1.jpg";///<summary>Web-downloaded pictures///</summary>Private texture2d img =Null///<summary>Local pictures///</summary>Private Texture2d Img2 =NullPrivateBOOL Downloadok =Falsevoid Ongui () {if (This.img! =NULL) GUI. Drawtexture (New Rect (0,0,200,300),THIS.IMG);if (This.img2! =NULL) GUI. Drawtexture (New Rect (320,0,200,300),THIS.IMG2);if (GUI. Button (New Rect (210,0,100,20),"Show Network Pictures") {Startcoroutine (This. Downloadtexture (This.url, getpictype.download)); }if (GUI. Button (New Rect (210,40M100,20),"Show local picture") {if (This.downloadok) {Startcoroutine (Downloadtexture ("file://" + Application.streamingassetspath +"/1.png", Getpictype.localload)); }else {Debug.logerror ("No download Completed"); }}} IEnumerator Downloadtexture (String URL, getpictype getType) {www www =New WWW (URL); Texture2d Tempimage;Yieldreturn www;if (www.isDone && www.error = =NULL) {Switch (getType) {Case Getpictype.download: {this.img = www.texture; Tempimage =THIS.IMG; Debug.Log (tempimage.width +  "+ Tempimage.height"); break;} case getpictype.localload: this.img2 = www.texture; tempImage = THIS.IMG; Debug.Log (tempimage.width +  "+ Tempimage.height"); break; default:tempimage = null; break;} if (tempimage! = null) {byte[] data = Tempimage.encodetopng (); File.writeallbytes (Application.streamingassetspath +  "/1.png", data); this.downloadok = true;}} }} 

Unity3d-www to display picture resources and save and load locally

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.