Well, you can make an Editor class for it, but Unity can't instantiate a generic one, when you instantiate a generic class, you must specify the generic type, as example, when you try do do this C# will throw an error: List<> l = new List<>();. And there is no way for Unity to safely assume or guess what T should be.
You can get it to work by creating a custom editor for each T, so that Unity can instantiate the custom editor where T is known, see the other answer of Ed Marthy on how to do this
We can also make a regular Editor, we just can't cast target to MonoBase<T> because we don't know T, And if you did know for sure that T is Example in this case, you should just write a custom editor for Example instead of MonoBase<> in the first place...
The closest thing you can cast 'target' to is the (Non-generic) type that MonoBase<> inherrits (MonoBehaviour in your example).
[CustomEditor(typeof(MonoBase<>), true)] // <-- the 'true' is important, it makes sure that classes that inherrit MonoBase use this editor aswell
public class ExampleEditor : Editor
{
public override void OnInspectorGUI()
{
if (GUILayout.Button("Build Object"))
{
MonoBehaviour possible = (MonoBehaviour)target;
Debug.Log(possible.name);
}
}
}
In your case, because the T in MonoBase<T> has to be a MonoBase<T> itself. You really can't cast to anything MonoBase. If the where clause of T would be something like where T : MonoBehaviour instead of where T : MonoBase<T>, and you would be willing to use an interface rather then abstract class, then you could make it so you can cast target to IMonoBase<MonoBehaviour>. Using the answer to this question.
public interface IMonoBase<out T> where T : MonoBehaviour
{
IMonoBase<T> ExampleMethod();
}
public abstract class MonoBase<T> : MonoBehaviour, IMonoBase<T> where T : MonoBehaviour
{
public abstract IMonoBase<T> ExampleMethod();
}
public class Example : MonoBase<Example>
{
public override IMonoBase<Example> ExampleMethod()
{
return this;
}
}
Editor script:
[CustomEditor(typeof(MonoBase<>), true)]
public class ExampleEditor : Editor
{
public override void OnInspectorGUI()
{
if (GUILayout.Button("Build Object"))
{
IMonoBase<MonoBehaviour> test = (IMonoBase<MonoBehaviour>)target;
IMonoBase<MonoBehaviour> example = test.ExampleMethod();
Debug.Log(example is IMonoBase<Example>);
}
}
}