привет у меня вышла такая ошибка UnityException: SupportsTextureFormatNative can only be…
- User
привет у меня вышла такая ошибка UnityException: SupportsTextureFormatNative can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function. UnityEngine.SystemInfo.SupportsTextureFormat (UnityEngine.TextureFormat format) (at <685c48cf8f0b48abb797275c046dda6a>:0) UnityEngine.Texture.ValidateFormat (UnityEngine.TextureFormat format) (at <685c48cf8f0b48abb797275c046dda6a>:0) UnityEngine.Texture2D.ValidateFormat (UnityEngine.TextureFormat format, System.Int32 width, System.Int32 height) (at <685c48cf8f0b48abb797275c046dda6a>:0) UnityEngine.Texture2D..ctor (System.Int32 width, System.Int32 height, UnityEngine.TextureFormat textureFormat, System.Int32 mipCount, System.Boolean linear, System.IntPtr nativeTex) (at <685c48cf8f0b48abb797275c046dda6a>:0) UnityEngine.Texture2D..ctor (System.Int32 width, System.Int32 height) (at <685c48cf8f0b48abb797275c046dda6a>:0) UI.PdfFilesUI.LoadPNG (System.String filePath) (at Assets/Scripts/UI/PDF/PdfFilesUI.cs:178) UI.PdfFilesUI.ApplyTextureToUI (System.String filePath) (at Assets/Scripts/UI/PDF/PdfFilesUI.cs:187) UI.PdfFilesUI+<>c__DisplayClass17_2.b__1 () (at Assets/Scripts/UI/PDF/PdfFilesUI.cs:86) System.Threading.Tasks.Task`1[TResult].InnerInvoke () (at :0) System.Threading.Tasks.Task.Execute () (at :0) --- End of stack trace from previous location where exception was thrown --- UI.PdfFilesUI.ProcessPdfFileAsync (System.String file) (at Assets/Scripts/UI/PDF/PdfFilesUI.cs:90) UI.PdfFilesUI.FindPdfFilesAsync () (at Assets/Scripts/UI/PDF/PdfFilesUI.cs:61) UI.PdfFilesUI.Initialize () (at Assets/Scripts/UI/PDF/PdfFilesUI.cs:46) System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.b__7_0 (System.Object state) (at :0) UnityEngine.UnitySynchronizationContext+WorkRequest.Invoke () (at <685c48cf8f0b48abb797275c046dda6a>:0) UnityEngine.UnitySynchronizationContext.Exec () (at <685c48cf8f0b48abb797275c046dda6a>:0) UnityEngine.UnitySynchronizationContext.ExecuteTasks () (at <685c48cf8f0b48abb797275c046dda6a>:0)
- Assistant
Эта ошибка в Unity указывает на то, что вы пытались использовать функцию
SupportsTextureFormatNative, которая делает вызов к системным возможностям и требует выполнения в основном потоке, в контексте, который не гарантированно выполняется в основном потоке (например, в конструкторе объекта или инициализаторе поля).Проблемный вызов произошел внутри метода
LoadPNGвашего классаPdfFilesUI.Unity требует, чтобы все взаимодействия с графическими ресурсами (такие как текстуры) и в целом большинство вызовов к API происходили в основном потоке, потому что движок не рассчитан на многопоточность в этих частях. Многопоточность поддерживается ограниченно и преимущественно для задач, не взаимодействующих непосредственно с графическими ресурсами или состоянием игрового мира.
Чтобы решить эту проблему, вам нужно перенести любые вызовы, связанные с созданием, изменением или проверкой текстур, в методы , или другие методы, гарантированно выполняемые в основном потоке, например в обработчики событий Unity (Update, FixedUpdate и т.д.) или же в делегаты, явно вызванные из основного потока (через или Coroutine).
- User
- Assistant
- User
ApplyTextureToUI принимает стринг как мне его нужно обновить?
- Assistant