- Home /
Scriptable Objects with editable functions
I have a simple ScriptableObject class like this one:
[CreateAssetMenu(fileName = "Upgrade", menuName = "Upgrade")]
public class Upgrade : ScriptableObject
{
public string Description;
public void OnAdd() { }
}
When I create a new SO from the editor menu I want to see an input field for the OnAdd function and be able to write different code for each instance IN THE INSPECTOR.
Is this (or something similar) posible to do with Unity and C# and how can I achieve it?
Answer by Captain_Pineapple · Apr 17 at 04:33 PM
In some way yes and no.
In a simple straight forward way as you want it no. But there are some workaround ways that could be used to generate a similar behaviour:
In general you'd need a text input field where you "Code". You can use runtime compilation methods to then take that string when the game is running and compile it into usable code that can be assigned to the OnAdd function. I must warn you here that using these methods is bad for performance as well as sercurity. See this reference on how to do this.
For now i will not go into further details about how else this could be solveable as this sounds a lot like a XY-Problem. Can you please read this reference and describe the problem that you are trying to solve here and why you want to be able to programm in the editor?
To elaborate, I want to create a "Designer Friendly" interface, as I call it, for implementing new upgrades for my game.
I image that you can click "two" buttons in the editor to create a new upgrade, write a short code snippet, attach it and you are good to go.
Interesting. My suggestion for this would be to simply write a good documentation with template classes for Upgrade components.
Something like:
public class MyNewFancyUpgrade : BaseUpgrade
{
public void override OnAdd()
{
//put behaviour here
}
}
Which then just has to be copied, given a name. Then you can add behaviour to the OnAdd function and can add this component somewhere.
All complex stuff can be given to the function using the BaseUpgrade function.
I think i understand why you want to choose this approach of yours but think about this: Would it really be easier to write a programm in a tiny inspector text field without any code highlighting, autocompletion and syntax/error checks which visual studio for example will give you? Sure it would be somewhat less effort since you don't have to open VS but everything else might be more complex in return. And the time you'd have to spend to write your custom solution and make sure that it really works flawlessly as well as secure and performant...
In case you still want to stick to your idea the best way would imo be to either stick to my first answer or write something that basically creates Monobehaviour classes for you. E.g. you add this base component which then has a "Add new Upgrade" button. When you click it the name from a text input is taken to create new .cs file filled with a template as i posted it above. Then you can have a larger field where someone can enter some code which you then can save into the OnAdd
function of your Upgrade component when some Save
button is clicked.
Your answer
Follow this Question
Related Questions
How do I associate my custom object for the serializedProperty objectReferenceValue 1 Answer
Scriptable Objects, how to force Include in Compile / Build without referencing it in the scene? 1 Answer
SceneView.onSceneGUIDelegate GUI sorting problem 1 Answer
Vector2 Editor Visualization 2 Answers
How to work with custom class (ScriptableObject) SerializedProperty? 1 Answer