一般情况下:不要直接访问和更改 MonoBehaviour 实例的值!
正如您所指出的,您将不得不处理各种脏标记并自救。您在编辑器中重新打开场景时遇到的情况是,某些内容未正确标记为脏,因此未与场景一起保存。
总是通过SerializedPropertys 处理所有标记脏和保存,尤其是自动撤消/重做等:
[CustomEditor(typeof(test))]
public class testEditor : Editor
{
private SerializedProperty Data;
public void OnEnable()
{
Data = serializedObject.FindProperty(nameof(test.Data));
}
public override void OnInspectorGUI()
{
DrawDefaultInspector();
// load all current values of the properties in test into the SerializedProperty "clones"
serializedObject.Update();
if (GUILayout.Button("Add"))
{
// this simply adds a new entry to the list
// since the data and list are both serializable this already initializes them with values
Data.arraySize++;
// Actually the entire following block is redundant
// by using Data.arraySize++; the new added entry automatically
// is a full copy of the entry before!
// I just decided to add it as example how you would access further nested SerializedProperties
//// if there was an element before now there are two
//if (Data.arraySize >= 2)
//{
// // get the last added element
// var lastElement = Data.GetArrayElementAtIndex(Data.arraySize - 1);
// var beforeElement = Data.GetArrayElementAtIndex(Data.arraySize - 2);
// // deep clone the list
// var lastElementList = lastElement.FindPropertyRelative(nameof(data.list));
// var beforeElementList = beforeElement.FindPropertyRelative(nameof(data.list));
// lastElementList.arraySize = beforeElementList.arraySize;
// for (var i = 0; i < lastElementList.arraySize; i++)
// {
// lastElementList.GetArrayElementAtIndex(i).intValue = beforeElementList.GetArrayElementAtIndex(i).intValue;
// }
//}
}
if (GUILayout.Button("Clear"))
{
Data.arraySize = 0;
}
// write back the values of the SerializedProperty "clones" into the real properties of test
serializedObject.ApplyModifiedProperties();
}
}
这现在可以处理所有脏标记,正确保存场景,自动撤消/重做等,您不必再关心这些了。
然后是一点“专业”提示:使用ReorderableList!它的设置看起来有点棘手,但功能非常强大:除此之外,顾名思义,它允许您通过在 Inspector 中拖放来简单地重新排序元素,还允许从中间删除一个项目,这是不可能的普通列表抽屉。这完全取代了您的 Add 和 Clear 按钮:
using UnityEditor;
using UnityEditorInternal;
[CustomEditor(typeof(test))]
public class testEditor : Editor
{
private SerializedProperty Data;
private ReorderableList dataList;
public void OnEnable()
{
Data = serializedObject.FindProperty(nameof(test.Data));
// should the list
// | be reorderable by drag&drop of the entries?
// | | display a header for the list?
// | | | have an Add button?
// | | | | have a Remove button?
// v v v v
dataList = new ReorderableList(serializedObject, Data, true, true, true, true)
{
// what shall be displayed as header
drawHeaderCallback = rect => EditorGUI.LabelField(rect, Data.displayName),
elementHeightCallback = index =>
{
var element = Data.GetArrayElementAtIndex(index);
var elementList = element.FindPropertyRelative(nameof(data.list));
return EditorGUIUtility.singleLineHeight * (elementList.isExpanded ? elementList.arraySize + 4 : 3);
},
drawElementCallback = (rect, index, isFocused, isActive) =>
{
var element = Data.GetArrayElementAtIndex(index);
EditorGUI.LabelField(new Rect(rect.x,rect.y,rect.width,EditorGUIUtility.singleLineHeight), element.displayName);
// in order to print the list in the next line
rect.y += EditorGUIUtility.singleLineHeight;
var elementList = element.FindPropertyRelative(nameof(data.list));
EditorGUI.PropertyField(new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight * (elementList.isExpanded ? elementList.arraySize + 1 : 1)), elementList, true);
}
};
}
public override void OnInspectorGUI()
{
// load all current values of the properties in test into the SerializedProperty "clones"
serializedObject.Update();
dataList.DoLayoutList();
// write back the values of the SerializedProperty "clones" into the real properties of test
serializedObject.ApplyModifiedProperties();
}
}
注意如果不是这样的话:
testEditor 部分应该
- 要么放在名为
Editor 的文件夹中的不同脚本中
-
或者您应该将与 UnityEditor 命名空间相关的任何内容包装在预处理器中,例如
#if UNITY_EDITOR
using UnityEditor;
using UnityEditorInternal;
#endif
...
#if UNITY_EDITOR
[CustomEditor(typeof(test))]
public class testEditor : Editor
{
...
}
#endif
否则在构建应用程序时会出错,因为 UnityEditor 命名空间在构建中被剥离并且仅存在于 Unity 编辑器本身中。