下面直接上代码
第一种替换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;
}
}
}
根据个人喜好使用自己喜欢的替换即可。