- Home /
Run method of editor script when application is playing
I have an editor script that extends the inspector of a scriptable object. I have a created a UnitTest method that checks that all values in the fields have been assigned properly. I would like to run this method when application starts (play button is pressed). Note that the script (of course) is not connected with any gameobjects and i would prefer if i did not need to run the method via another Monobehavior script inside my scenes. Also because there are many instances of these scriptable objects that my script is running on, solutions that include "static" properties/methods can not work. Thanks in advance
Answer by Bunny83 · Jan 31, 2020 at 03:14 PM
I see several issues here. First of all while creating unit test are great, automatically launching them every time you enter play mode seems to be total overkill.
The next issue and the most important one is that scriptable objects are actual instances which might live just in memory or serialized somewhere in your project. When you create a custom inspector for your scriptable object, that is only actually used when you inspect one of your scriptable object instances in the inspector. So it's completely unclear what instances you actually want to "check".
To give an analogy think of a custom inspector to be a program like MSPaint. You might open / edit / view all *.bmp files with that program. So when you "open" such a file your OS will actually start paint with the file you want to edit. However there is no way to automatically run paint for every possible bmp file you have somewhere on your HDD. This is pretty much the same in Unity.
So without some clear restrictions and clarification of your requirement we could only say: What you're asking is invalid / not possible. Apart from that the term Unit test specifically is about verifying code / classes. It seems you just want to validate serialized data. Since you have a custom inspector you should validate your data before you store / serialize it.
If you use a scriptable object to hold various references to assets and you just want to validate that they are all assigned, you can do this in the OnEnable callback inside the ScriptableObject. Anything else would just result in an unnecessary event nightmare and just overcomplicates everything.