1
在unity3d中,我使用WWW類從鏈接下載紋理,然後使用獲得的紋理創建一個Sprite,並將其顯示在屏幕上。這在Unity編輯器中工作得很好,但是當我在瀏覽器中將它作爲HTML5運行而不是顯示我的Sprite時,會出現一個紅色問號。爲什麼?爲什麼統一顯示問號而不是想要的精靈?
這是我的C#代碼:
GameObject myImage;
Sprite neededSprite;
IEnumerator Start() {
WWW getMyImage= new WWW("http://previews.123rf.com/images/burakowski/burakowski1202/burakowski120200228/12221967-Grunge-Example-stamp-Stock-Vector-demo.jpg");
myImage = GameObject.Find("myImage");
yield return getMyImage;
Texture2D rawImage = getMyImage.texture;
neededSprite = Sprite.Create(rawImage, new Rect(0.0f, 0.0f, rawImage.width, rawImage.height), new Vector2(0.5f, 0.5f), 100.0f);
myImage.GetComponent<Image>().sprite = neededSprite;
}
你有你的瀏覽器控制檯的任何錯誤?我懷疑是一個跨源問題:https://docs.unity3d.com/Manual/webgl-networking.html – Hellium
可能的重複:http://stackoverflow.com/questions/30090907/unity3d-webgl-cross-origin-request -blocked – Hellium
嘗試將下載的紋理保存爲png以查看它是否爲有效紋理。使用['Texture2D.EncodeToPNG'](https://docs.unity3d.com/ScriptReference/Texture2D.EncodeToPNG.html) – zwcloud