ScriptableObjectFactory | Unity editor extension | Game Engine library
kandi X-RAY | ScriptableObjectFactory Summary
kandi X-RAY | ScriptableObjectFactory Summary
A Unity editor extension for creating any ScriptableObject type. Download as a .UnityPackage: ScriptableObjectFactory. Originally introduced in this Blog Post.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of ScriptableObjectFactory
ScriptableObjectFactory Key Features
ScriptableObjectFactory Examples and Code Snippets
Community Discussions
Trending Discussions on ScriptableObjectFactory
QUESTION
Please read the entire question, and run the example before posting an answer.
OverviewI ran into some inconsistent behavior in Unity 5.6.1 when loading nested assets in a static Editor script (so in the static constructor of a class marked with [InitializeOnLoad]
).
I am loading a ScriptableObject
Asset with Resources.Load
, and the ScriptableObject has a public reference to another asset resource, let's suppose a GameObject Prefab. From this point I will refer to the ScriptableObject as the 'Wrapper', since in this simplified example that is the only purpose it serves.
While Resources.Load
correctly returns the Wrapper, the nested Prefab reference is often not yet loaded during the first run, but is loaded after the second run:
To my understanding this is an order of execution issue where the Prefab resource in question has not been loaded yet during static construction, and on subsequent runs it remains cached.
I assumed that when loading in an asset with a serialized reference to another asset, that the nested asset would automatically be loaded by default, regardless of whether this is during static init or not. This however does not seem to be the case here.
Proof that the Wrapper asset does indeed correctly reference the Prefab in its serialized data (with Asset Serialization set to Force Text):
I have also tried to use AssetDAtabase.LoadAssetAtPath
(at least while in the editor), which has not made a difference.
You can download a UnityPackage here, which contains the following:
Or reproduce it as follows:
The Scripts:
ExampleWrapper.cs:
...
ANSWER
Answered 2017-Jun-18 at 17:42There's a misconception regarding your question.
The reference is passed to the Loader
class, and you can check it by logging Wrapper.Value
after scene initialization is completed.
Most probably, the problem is (as you pointed out) in the execution/serialization order, apparently it happens something like this:
- The
Loader
constructor is called, andWrapper
reference is passed correctly. Debug.Log(Wrapper.Value)
returnsnull
because the fields of the scriptable object haven't yet been serializedWrapper
's fields are serialized, now loggingWrapper.Value
shows correctlyExampleObject
.
So, unless you're planning something "special" to do with the Wrapper
fields during initalization, there's really not a problem in your code: I tried to run Debug.Log(Loader.Wrapper.Value)
during the OnEnable
of ExampleWrapper
, and I got the correct value.
Regarding your edit, apparently it happens "by Design", as clearly stated in this issue: https://issuetracker.unity3d.com/issues/unityeditor-dot-initializeonload-calls-the-constructor-twice-when-the-editor-opens
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install ScriptableObjectFactory
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page