I'm building an Unity extension that needs Scriptable objects. When using Scriptable objects the user must instantiate a class inheriting from ScriptableObject using ScriptableObject.createInstance(). So, if we need to supply more information to the instace to fully initialize it we're forced to use a 2 step intialization model.
I would like to know best practices when initializing an object using 2 steps (Creation using constructor and Initialization using Init() method).
The main problem I have found is that as you need to specialize Init() method in derived clases to accomodate initialization to the specialized object, you get more and more Init() methods because signatures do not match due to the different parameters used.
I have come across 2 solutions to this problem:
1) Hide parent methods and throwing an exception for the Init() methods that do not fully instantiate the specialized type.
2) Use a wrapper class that just exposes the valid Initialization methods.
Both of these seem cumbersome to me. And this is why I would like to hear how other people have overcome this problem.
An example of the problem for clarifying:
public class Class1 : ScriptableObject
{
public virtual void Init()
{
Debug.Log("Class1::Init()");
}
}
public class Class2 : Class1
{
int myParameter;
public virtual void Init(int parameter)
{
Debug.Log("Class3::Init()");
}
}
//I could now do something like this. And the Class2 object
//Would contain an invalid state because myParameter wasn't initialized
Class2 c= ScritableObject.CreateInstance<Class2>();
c.Init();