Witam. Próbuję napisać uniwersalny Editor Script dla wszystkich obiektów MonoBehaviour. Oto skrypt:
[CustomEditor(typeof(PlayerHealth))]
public class MyInspector : Editor {
public override void OnInspectorGUI () {
base.OnInspectorGUI ();
List<MethodInfo> methods = new List<MethodInfo> (((PlayerHealth)target).GetType ().GetMethods ());
methods = RemoveObjectsFromList<MethodInfo> (new MonoBehaviour ().GetType ().GetMethods (), methods);
for (int i = 0; i < methods.Count; i++) {
Debug.Log (methods[i].Name);
EditorGUILayout.LabelField (methods[i].Name);
}
}
List<T> RemoveObjectsFromList<T> (T[] toRemove, List<T> fromWhereRemove) {
foreach (var item in toRemove) {
fromWhereRemove.Remove (item);
}
return fromWhereRemove;
}
}
Problemy są dwa, a mianowicie:
1. Skrypt działa tylko dla PlayerHealth, bo po wstawieniu MonoBeahaviour nie działa.
2. Jak uzyskać tymczasowy obiekt MonoBehaviour ponieważ użycie new MonoBehaviour wypisuje takie ostrzeżenie:
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
Dziękuję za wszelką pomoc.