Hello,
I want to create a “help component” for my work. Let’s see.
1.
It would be a class that inherits from MonoBehaviour.
But, because it’s not intented to be used in the build, I create the file in the Editor folder.
>> Is it ok?
2.
Now, I code this class with some editor elements. Let’s use these namespaces:
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
Now, let’s implement IPreprocessBuildWithReport. More info here: Unity - Scripting API: Build.IPreprocessBuildWithReport.OnPreprocessBuild
>> Is it ok to code MonoBehaviour class with that interface?
3.
At the end, let’s add a field:
[SerializeField] private int myTry;
Fine. Everything seems to be ok in editor.
Let’s build, for instance for Android.
Now, this warning message appears in the console pointing at the line of the variable declaration: "You are trying to create a MonoBehaviour using the ‘new’ keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all"
>> What’s the point?
Best regards
I don’t understand the question, but you cannot use new to create MonoBehaviours, that’s not gonna change.
Is this “help component for your work” intended to go to the customer in the final build? If so, get it out of the Editor folder; nothing in Editor goes to the customer.
If it’s help for you in the editor, it needs to be some form of Editor or Editor Window or Inspector or custom property drawer or something along those lines, with each one of them being a different way of extending the Unity editor.
And finally, what does any of this have to do with IPreProcessBuildWithReport??!!
1 Like
Thank you for your reply.
The component would not be in the final build.
So, do you mean that we can’t use components (meaning MonoBehaviour class) as a way to create editor tool?
IPreProcessBuildWithReport could be part of the editor tool. I guess I can use this interface to test things “before the build is started” as the Unity doc says.
Components slot into GameObjects, either in a scene or as an asset on disk (eg, prefab). They have a lifecycle tied closely to the scene and play/pause cycle in the editor.
If you need to stand up some tooling on IPreprocess, just do it with non-Monobehavior classes.
1 Like
I’m goint to make it that way. Thank you very much.