unity ugui image change pictures

1: use of resources loading

. 1  the using UnityEngine;  
 2  the using the System.Collections;  
 . 3  the using UnityEngine.UI;  
 . 4      
. 5  public  class ChangeImage: MonoBehaviour  
 . 6  {   
 . 7    public    Image myImage;  
 . 8      
. 9     // the Use for the this Initialization   
10      void the Start ()  
 . 11      {  
 12 is  // needs its own Creating Resources folder under Assets, imagename is the picture under the name Resources 
13          myImage.sprite = Resources.Load ( " imagename " , typeof (Sprite))AS the Sprite;      // Image / PIC in Assets / Resources / directory   
14      }  
 15 }  

2: Direct assignment pulling

 1 using UnityEngine;  
 2 using System.Collections; 
 3 using UnityEngine.UI;  
 4     
 5 public class ChangeImage : MonoBehaviour  
 6 {   
 7   public   Image myImage;  
 8   public   Sprite mySprite;  
 9     // Use this for initialization  
10     void Start()  
11     {  
12         myImage.sprite = mySprite;    
13     }  
14 }

3: Replace the network to download

 1 using UnityEngine;  
 2 using System.Collections; 
 3 using UnityEngine.UI;  
 4     
 5 public class ChangeImage : MonoBehaviour  
 6 {   
 7   public   Image myImage;  
 8  
 9     // Use this for initialization  
10     void Start()  
11     {  
12        StartCoroutine(WWWGetImage());
13     }  
14 
15 
16      IEnumerator WWWGetImage()
17   {
18       string url = "http://pic02.1sucai.com/190302/330853-1Z302203J169-lp.jpg";
19       WWW www = new WWW(url);
20       yield return www;
21       if (string.IsNullOrEmpty(www.error))
22       {
23           Texture2D tex = www.texture;
24           Sprite temp = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), Vector2.zero);
25          myImage.sprite = temp;
26       }
27   }  
28 
29 }

 

Guess you like

Origin www.cnblogs.com/INSIST-NLJY/p/11806350.html
Recommended