Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

ScriptableObject: Support undocumented event function void OnValidate() #79

Closed
sindrijo opened this issue Dec 7, 2016 · 5 comments
Closed
Assignees

Comments

@sindrijo
Copy link

sindrijo commented Dec 7, 2016

Support for the event function void OnValidate() on ScriptableObject is missing, probably because it undocumented.

@citizenmatt
Copy link
Member

Do you have any details on who invokes this method? It would be nice to work out visibility (public private), static modifiers and parameters to be sure I've added the function correctly.

@CapnRat
Copy link

CapnRat commented Dec 13, 2016

OnValidate's behavior on ScriptableObject is the same as on MonoBehaviour. OnValidate is a non-static method which is invoked from native and isn't picky about visibility (it'll get invoked regardless of visibility). OnValidate is different from the other magic methods in that it only gets invoked from the Editor. A good practice is to wrap OnValidate with #if UNITY_EDITOR so that the function can be stripped out for deployment.

To expand on ScriptableObject a bit. Native Unity code doesn't distinguish between MonoBehaviour and ScriptableObject. They are the same native type (it's confusing, I know, and everyone gets tripped up by it that looks at the native code base). The only difference is in how the object is used, MonoBehaviours live on GameObjects, while ScriptableObjects can live alone. So in general, all magic methods that would make sense without a GameObject context should work and be implemented for ScriptableObjects. Off the top of my head this includes, Awake, OnEnable, OnDisable, OnDestroy, OnValidate, and Reset, but there could be more.

@citizenmatt
Copy link
Member

Great info, thanks! I like the idea of wrapping editor specific functions in #if UNITY_EDITOR. I can make that an option for the generate dialog. Are there any other editor specific functions?

I've already got Awake, OnEnable, OnDisable and OnDestroy as documented messages for ScriptableObject, but I don't have Reset, so I'll add that, too. Is there an easy way of getting a list of other magic methods that I should include for ScriptableObject?

@CapnRat
Copy link

CapnRat commented Dec 13, 2016

Unfortunately, not really a good way to get a list. Either manually try out each MonoBehaviour event to see if it works, or someone (like myself) with source will need to dig into it and see which ones are actually used for ScriptableObject. I'll see if I can dig into it sometime this week.

@citizenmatt
Copy link
Member

Any info you can throw our way about the messages would be great - I'm currently scraping several versions of the docs to build the data I'm using, and that doesn't include info on what's only used by the editor, or what messages can also be coroutines.

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

3 participants