public Sprite sp;
IEnumerator loadSprite(string path)
{
string filePath = path;
WWW www = new WWW(filePath);
yield return www;
Texture2D texture = www.texture;
sp = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
}
}
这样,我们就能将本地图片加载到sp里面,通过观察者模式,可以动态显示图片。
示例程序:
// Use this for initialization
void Start () {
StartCoroutine(loadSprite("file://" +"C:\\Users\\HP\\Desktop\\相册\\合照\\IMG_7438.jpg"));
}
public Image img;
// Update is called once per frame
void Update () {
}
public Sprite sp;
IEnumerator loadSprite(string path)
{
string filePath = path;
WWW www = new WWW(filePath);
yield return www;
Texture2D texture = www.texture;
//因为我们定义的是Image,所以这里需要把Texture2D转化为Sprite
sp = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
img.sprite = sp;
}
需要注意,在pc端,需要加上
"file://"
前缀