In Unity C#, I have a base class Baker, it’s derived from ScriptableObject, and I’m deriving from it CakeBaker, BreadBaker, PieBaker.
ScriptableObjects can only be created statically, using CreateAsset()
This leads me to the situation where each of my bakers now contain an identical method CreateBaker(), with the only difference being the Type.
statics and generics don’t mix, and a static method works on the class it’s declared in, not any class derived from it.
I thought about C/C++ style Macros, but AFAICS there’s no equivalent in C#.
How do I avoid repeating myself (and all that it entails) in this situation?
it’s a small method, and there are currently not too many bakers, so I’ve just copied the method and gotten on with it, but I wondered if the hive mind would have a better idea.
public static void CreateAsset(bool updateAssetDatabase = true)
{
var instance = CreateInstance();
// Save the newly created instance to the AssetDatabase if it does not exist
AssetDatabase.CreateAsset(instance, assetPath);
if (updateAssetDatabase)
{
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
}
public static Baker CreateInstance()
{
return CreateInstance<PieBaker>();
}