Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Bug Exception when instantiating VisualTreeAsset with text from Addressables

Discussion in 'UI Toolkit' started by Hellfim, Jan 15, 2024.

  1. Hellfim

    Hellfim

    Joined:
    Sep 11, 2014
    Posts:
    127
    I'm receiving UnityException: CreateScriptableObjectInstanceFromType can only be called from the main thread exception, when using addressables, but the project works fine.

    Code (csharp):
    1.  
    2. [SerializeField]
    3. private AssetReferenceT<VisualTreeAsset> _myVisualTreeAssetReference; //Set up in inspector
    4.  
    5. private PanelSettings _myPanelSettings; //Set up via Addressables.LoadAssetAsync<PanelSettings>(...) and a string key in-advance
    6.  
    7. private async UniTask<VisualTreeAsset> LoadMyVisualTreeAsset()
    8. {
    9.     return await Addressables.LoadAssetAsync<VisualTreeAsset>(_myVisualTreeAssetReference);
    10. }
    11.  
    12. public async UniTask<UIDocument> CreateUIDocument(String name)
    13. {
    14.     var container = new GameObject(name);
    15.     container.SetActive(false);
    16.  
    17.     var uiDocument = container.AddComponent<UIDocument>();
    18.     uiDocument.panelSettings = _myPanelSettings;
    19.  
    20.     var layout = await LoadMyVisualTreeAsset();
    21.     uiDocument.visualTreeAsset = layout;
    22.  
    23.     return uiDocument;
    24. }
    25.  

    So, the problem arises only if VisualTreeAsset layout has text, and I assign it to uiDocument.visualTreeAsset property.
    Initially I thought that Addressables.LaodAssetAsync(...) loads asset in a separate thread and doesn't return back to the MainUnityThread, however it seems that is not the case.
    I'm unable to mitigate the problem by inserting await UniTask.SwitchToMainThread() or await UniTask.NextFrame(), or any other delay, which is certainly returns control to the main thread.
    I'm also unable to catch the exception, because (as you can see in the stack trace below) none of my code invokes the execution.

    I'm using Unity 2023.2.3f1

    UnityException: CreateScriptableObjectInstanceFromType 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.ScriptableObject.CreateScriptableObjectInstanceFromType (System.Type type, System.Boolean applyDefaultsAndReset) (at <61f56803c0b0466195d97f1cb7494aa1>:0)
    UnityEngine.ScriptableObject.CreateInstance (System.Type type) (at <61f56803c0b0466195d97f1cb7494aa1>:0)
    UnityEngine.ScriptableObject.CreateInstance[T] () (at <61f56803c0b0466195d97f1cb7494aa1>:0)
    UnityEngine.UIElements.PanelTextSettings.get_defaultPanelTextSettings () (at <5566f9e97c3b47c3802abbb5405c3a92>:0)
    UnityEngine.UIElements.TextUtilities.GetTextSettingsFrom (UnityEngine.UIElements.VisualElement ve) (at <5566f9e97c3b47c3802abbb5405c3a92>:0)
    UnityEngine.UIElements.UITKTextHandle.ConvertUssToTextGenerationSettings () (at <5566f9e97c3b47c3802abbb5405c3a92>:0)
    UnityEngine.UIElements.UITKTextJobSystem+PrepareTextJobData.Execute (System.Int32 index) (at <5566f9e97c3b47c3802abbb5405c3a92>:0)
    Unity.Jobs.IJobParallelForExtensions+ParallelForJobStruct`1[T].Execute (T& jobData, System.IntPtr additionalPtr, System.IntPtr bufferRangePatchData, Unity.Jobs.LowLevel.Unsafe.JobRanges& ranges, System.Int32 jobIndex) (at <61f56803c0b0466195d97f1cb7494aa1>:0)
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,570
  3. Hellfim

    Hellfim

    Joined:
    Sep 11, 2014
    Posts:
    127
    @karl_jones, hey sorry for the delay, was kinda busy. Here is a case number IN-66871 with a repro project.
     
    karl_jones likes this.
  4. pzolla

    pzolla

    Joined:
    Apr 21, 2020
    Posts:
    19
    I have same error and I am not using addressables
     
  5. Hellfim

    Hellfim

    Joined:
    Sep 11, 2014
    Posts:
    127
    My bug report was confirmed and now it's status is Pending. I believe this means they have a fix, but it's not merged yet. The issue was marked as internal, so there is no link to a public issue. I believe fix should appear in new versions this spring.