- Home /
Make custom editor for generic class to apply to all current and future child classes
Hello, I've made some classes as follows:
public abstract class Item : MonoBehaviour
public abstract class HeldItem : Item
public class Weapon : HeldItem
public class Axe : Weapon
public class Sword : Weapon
public class Shield : HeldItem
I have made a custom editor as follows:
[CustomEditor(typeof(Item))]//customizes the item class in the inspector
public class itemCustomInspector : Editor
{
private PrefabManager pfManager;
bool heldFoldout;
public override void OnInspectorGUI()
{
EditorGUI.BeginChangeCheck();
Item itemScript = (Item)target;
itemScript.itemType = (itemTypes)EditorGUILayout.EnumPopup("Item Type:", itemScript.itemType);
itemScript.friendlyName = EditorGUILayout.TextField("Friendly Name:", itemScript.friendlyName);
itemScript.description = EditorGUILayout.TextField("Description:", itemScript.description);
itemScript.icon = (Sprite)EditorGUILayout.ObjectField("Icon", itemScript.icon, typeof(Sprite), allowSceneObjects: false);
#region prefab selection
pfManager = GameObject.Find("GameManager").GetComponent<PrefabManager>();
if (pfManager)
{
string[] pfabNamesArray = new string[pfManager.prefabs.Count];
short i = 0;
foreach (PrefabManager.prefab pfab in pfManager.prefabs)
{
pfabNamesArray[i] = pfab.prefabName;
i++;
}
int prefabSelectionIndex = System.Array.IndexOf(pfabNamesArray, itemScript.prefabName);
EditorGUILayout.LabelField("Prefab Manager Prefab Name:");
prefabSelectionIndex = EditorGUILayout.Popup(prefabSelectionIndex, pfabNamesArray);
itemScript.prefabName = pfabNamesArray[prefabSelectionIndex];
}
else //if prefab manager can't be located, insert a text field to still allow prefab selection
{
itemScript.prefabName = EditorGUILayout.TextField(new GUIContent("Prefab Name:", "The name of the item exactly as listed in the prefab manager. Used for loading inventory from save."), itemScript.prefabName);
}
#endregion
if (EditorGUI.EndChangeCheck())
{
Undo.RecordObject(target, "Changed Item Properties");
EditorUtility.SetDirty(itemScript);
UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(itemScript.gameObject.scene);
}
}
}
*/
#endregion
My question is, how can I set up my custom editor script so that anything inheriting the "Item" class is affected by the custom editor, even future classes that I may not know about yet that someone else may create? Right now, in the editor, if I put in an axe, sword, etc., it doesn't inherit the editor as I hoped it would.
Answer by Michael_Berna · Dec 01, 2020 at 03:17 AM
After leaving the problem, returning, and doing more research with different wording, I discovered one answer that worked for me. It is very simple.
if you add true at the end, it is inherited by any class below it, so any current or future subclasses will inherit this custom editor.
Working solution:
[CustomEditor(typeof(Item),true)]
original:
[CustomEditor(typeof(Item))]
Your answer
Follow this Question
Related Questions
Where can I view the code created by the editor? 1 Answer
Multiple Cars not working 1 Answer
Editor Script for selecting class property for saving 1 Answer
Distribute terrain in zones 3 Answers
Get list of all "Action" classes 1 Answer