下面直接上代碼
第一種替換Image圖片的方式:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Test : MonoBehaviour
{
[SerializeField]
Image myImage;
void Start( )
{
// Image/pic 在 Assets/Resources/目錄下
myImage.sprite = Resources.Load("Image/pic", typeof(Sprite)) as Sprite; ??
}
}
第二種替換Image圖片的方式:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Test : MonoBehaviour
{
[SerializeField]
Image myImage;
[SerializeField]
Sprite mySprite;
void Start( )
{
// mySprite 為外部指定的圖片資源
myImage.sprite = mySprite; ? ?
}
}
第三種替換Image圖片的方式:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Test : MonoBehaviour {
[SerializeField]
Image myImage;
void Start ( ) {
StartCoroutine(GetImage( ));
}
IEnumerator GetImage( )
{
string url = "http://www.5dbb.com/images/logo.gif";
WWW www = new WWW(url);
yield return www;
if (string.IsNullOrEmpty(www.error))
{
Texture2D tex = www.texture;
Sprite temp = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0, 0));
myImage.sprite = temp;
}
}
}
根據(jù)個(gè)人喜好使用自己喜歡的替換即可全庸。