【问题标题】:Mark Texture as Read/Write through code [duplicate]通过代码将纹理标记为读/写[重复]
【发布时间】:2017-12-15 09:28:37
【问题描述】:

我正在尝试在 iOS 手机上下载、设置和保存纹理,但我一直遇到同样的错误:

(纹理需要被标记为读/写才能在播放器中获取RawTextureData 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();
        }

【问题讨论】:

    标签: c# ios unity3d


    【解决方案1】:

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

    【讨论】:

    • 我知道我可以做到,但我从网上下载纹理,所以我无法在检查器上检查它。
    • 您需要使用Texture Importer来保存下载的纹理并将其可读性设置为true。
    • 您也可以尝试将下载的纹理渲染到 RenderTexture,然后像这样从 RenderTexture 获取像素 [stackoverflow.com/questions/44733841/…
    • 我怎么会错过这个......非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2015-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-30
    • 1970-01-01
    • 1970-01-01
    • 2012-01-05
    相关资源
    最近更新 更多