1

I'm trying download, set and save the texture on the iOS phone, but I keep having the same error:

(Texture needs to be marked as Read/Write to be able to GetRawTextureData in player UnityEngine.Texture2D:GetRawTextureData())

It happens only after iPhone conversion but it works fine on macOS. Thanks for any help :)

       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();
        }
Programmer
  • 121,791
  • 22
  • 236
  • 328
Alexandra
  • 23
  • 4

1 Answers1

1

You need to select all the textures you want to use in your function and set their Read/Write to enabled in the editor.

enter image description here

Fiffe
  • 1,196
  • 2
  • 13
  • 23
  • I know i can do that but i download texture from web so i cannot check it on inspector. – Alexandra Dec 15 '17 at 09:46
  • You need to use [Texture Importer](https://docs.unity3d.com/ScriptReference/TextureImporter.html) to save downloaded texture and set it's readability to true. – Fiffe Dec 15 '17 at 09:59
  • You could also try rendering the downloaded texture to RenderTexture and then get the pixels from RenderTexture like in this [https://stackoverflow.com/questions/44733841/how-to-make-texture2d-readable-via-script](question) – Fiffe Dec 15 '17 at 10:05
  • How could i miss that.. Thank you so much! – Alexandra Dec 15 '17 at 10:32