我的自定义编辑器找不到我要查找的字段
My custom editor doesn't find the fields I'm looking for
void OnEnable()
{
iteminfo = (ItemInfo)target;
itemName = serializedObject.FindProperty(iteminfo.Name);
}
public override void OnInspectorGUI()
{
serializedObject.Update();
**EditorGUILayout.PropertyField(itemName);**
serializedObject.ApplyModifiedProperties();
}
它说有一个空引用,我找不到更改它的地方。
这是其他脚本
public class ItemInfo : MonoBehaviour
{
public Item Item_Info;
public enum ItemType { Weapon, Armor, Potion, Other };
public ItemType itemtype;
public int damage;
public int Defense;
public int heal_value;
private Inventory playerinventory;
}
然后是项目脚本
public class Item
{
public string Name, Description;
public bool stackable;
public int Value, weight;
public string ModelPath;
}
我正在尝试获取
中的“名称”
public class Item
这是您想要执行的操作:
首先,如果不是这样的话,请 Item
class [Serializable]
.
然后有例如这位编辑
[CustomEditor(typeof(ItemInfo))]
public class ItemInfoEditor : Editor
{
private SerializedProperty item;
private SerializedProperty itemType;
private SerializedProperty damage;
private SerializedProperty defense;
private SerializedProperty heal;
private ItemInfo itemInfo;
void OnEnable()
{
itemInfo = (ItemInfo)target;
// Link up the properties via the according field names
item = serializedObject.FindProperty(nameof(ItemInfo.Item_Info));
itemType = serializedObject.FindProperty(nameof(ItemInfo.itemtype));
damage = serializedObject.FindProperty(nameof(ItemInfo.damage));
defense = serializedObject.FindProperty(nameof(ItemInfo.Defense));
heal = serializedObject.FindProperty(nameof(ItemInfo.heal_value));
}
public override void OnInspectorGUI()
{
// just draw the default script field
DrawScriptField();
serializedObject.Update();
// draw the Item field itself
EditorGUILayout.PropertyField(item, true);
// draw the item type dropdown
EditorGUILayout.PropertyField(itemType);
// according to the selected value draw the according field(s)
switch ((ItemInfo.ItemType)itemType.intValue)
{
case ItemInfo.ItemType.Weapon:
EditorGUILayout.PropertyField(damage);
break;
case ItemInfo.ItemType.Armor:
EditorGUILayout.PropertyField(defense);
break;
case ItemInfo.ItemType.Potion:
EditorGUILayout.PropertyField(heal);
break;
case ItemInfo.ItemType.Other:
EditorGUILayout.PropertyField(damage);
EditorGUILayout.PropertyField(heal);
EditorGUILayout.PropertyField(defense);
break;
}
serializedObject.ApplyModifiedProperties();
}
private void DrawScriptField()
{
EditorGUI.BeginDisabledGroup(true);
EditorGUILayout.ObjectField("Script", MonoScript.FromMonoBehaviour(itemInfo), typeof(ItemInfo), false);
EditorGUI.EndDisabledGroup();
EditorGUILayout.Space();
}
}
void OnEnable()
{
iteminfo = (ItemInfo)target;
itemName = serializedObject.FindProperty(iteminfo.Name);
}
public override void OnInspectorGUI()
{
serializedObject.Update();
**EditorGUILayout.PropertyField(itemName);**
serializedObject.ApplyModifiedProperties();
}
它说有一个空引用,我找不到更改它的地方。
这是其他脚本
public class ItemInfo : MonoBehaviour
{
public Item Item_Info;
public enum ItemType { Weapon, Armor, Potion, Other };
public ItemType itemtype;
public int damage;
public int Defense;
public int heal_value;
private Inventory playerinventory;
}
然后是项目脚本
public class Item
{
public string Name, Description;
public bool stackable;
public int Value, weight;
public string ModelPath;
}
我正在尝试获取
中的“名称”public class Item
这是您想要执行的操作:
首先,如果不是这样的话,请 Item
class [Serializable]
.
然后有例如这位编辑
[CustomEditor(typeof(ItemInfo))]
public class ItemInfoEditor : Editor
{
private SerializedProperty item;
private SerializedProperty itemType;
private SerializedProperty damage;
private SerializedProperty defense;
private SerializedProperty heal;
private ItemInfo itemInfo;
void OnEnable()
{
itemInfo = (ItemInfo)target;
// Link up the properties via the according field names
item = serializedObject.FindProperty(nameof(ItemInfo.Item_Info));
itemType = serializedObject.FindProperty(nameof(ItemInfo.itemtype));
damage = serializedObject.FindProperty(nameof(ItemInfo.damage));
defense = serializedObject.FindProperty(nameof(ItemInfo.Defense));
heal = serializedObject.FindProperty(nameof(ItemInfo.heal_value));
}
public override void OnInspectorGUI()
{
// just draw the default script field
DrawScriptField();
serializedObject.Update();
// draw the Item field itself
EditorGUILayout.PropertyField(item, true);
// draw the item type dropdown
EditorGUILayout.PropertyField(itemType);
// according to the selected value draw the according field(s)
switch ((ItemInfo.ItemType)itemType.intValue)
{
case ItemInfo.ItemType.Weapon:
EditorGUILayout.PropertyField(damage);
break;
case ItemInfo.ItemType.Armor:
EditorGUILayout.PropertyField(defense);
break;
case ItemInfo.ItemType.Potion:
EditorGUILayout.PropertyField(heal);
break;
case ItemInfo.ItemType.Other:
EditorGUILayout.PropertyField(damage);
EditorGUILayout.PropertyField(heal);
EditorGUILayout.PropertyField(defense);
break;
}
serializedObject.ApplyModifiedProperties();
}
private void DrawScriptField()
{
EditorGUI.BeginDisabledGroup(true);
EditorGUILayout.ObjectField("Script", MonoScript.FromMonoBehaviour(itemInfo), typeof(ItemInfo), false);
EditorGUI.EndDisabledGroup();
EditorGUILayout.Space();
}
}