我遇到这个问题的情况是, 在Start()中直接使用WWW价值本地图片,可能是加载图片相对比较耗时,就出现了这个错误。
解决的办法是使用协程:
// Use this for initialization
void Start () {
AddSampleData ();
}
void AddSampleData(){
StartCoroutine(WaitLoadImage("/Users/xxxx/xxx/xxx.jpg", go));
}
IEnumerator WaitLoadImage(string fileName, GameObject targetGameObject)
{
RectTransform rectTransform = targetGameObject.GetComponent<RectTransform> ();
WWW www=new WWW("file://"+fileName);
Image img = targetGameObject.GetComponent<Image> ();
Debug.Log(www.url);
yield return www;
Rect spriteRect = new Rect (0, 0, www.texture.width, www.texture.height);
Sprite imageSprite = Sprite.Create (www.texture, spriteRect, new Vector2 (0.5f, 0.5f));
img.sprite = imageSprite;
}