将纹理标记为通过代码进行读/写

时间:2017-12-15 09:28:38

标签: c# ios unity3d

我尝试下载,设置并保存iOS手机上的纹理,但我一直有同样的错误:

纹理需要标记为读/写才能在播放器中使用GetRawTextureData UnityEngine.Texture2D:GetRawTextureData()

仅在iPhone转换后才会发生,但它在macOS上运行正常。感谢您的帮助:))

       UnityWebRequest www = UnityWebRequest.GetTexture(url);
       AsyncOperation op = www.Send();

       while (op.isDone == false)
       {
           yield return new WaitForEndOfFrame();
       }
      if (www.isError)
        {
            Debug.Log(www.error);
        }
     else
        {
            texture = ((DownloadHandlerTexture)www.downloadHandler).texture;

            Texture2D texture2d = new Texture2D(texture.width, texture.height, TextureFormat.ARGB32, false);
            texture2d.LoadRawTextureData(texture.GetRawTextureData());
            texture2d.Apply();

            byte[] bytes = texture2d.EncodeToJPG();
            Debug.Log("after");

            File.WriteAllBytes(Application.persistentDataPath + "/" + name + ".png", bytes);

            texture = texture2d;

            setTheTexture();
        }

1 个答案:

答案 0 :(得分:0)

您需要选择要在函数中使用的所有纹理,并在编辑器中将其读/写设置为启用。

enter image description here