Version: 2022.2
public bool DeleteCommand ();

描述

Deletes the array element referenced by the SerializedProperty.

调用此函数后将无法再使用已序列化的属性。在这种情况下,必须创建新的迭代器。

See Also: DeleteArrayElementAtIndex

using UnityEngine;
using UnityEditor;

public class SerializePropertyDeleteCommandExample : ScriptableObject { public int[] m_Array = new int[] { 1, -1, -1, 3, -1, -1, 1, 3, -1 };

[MenuItem("Example/SerializedProperty/DeleteCommand Example")] static void MenuCallback() { var scriptableObject = ScriptableObject.CreateInstance<SerializePropertyDeleteCommandExample>();

using (var serializedObject = new SerializedObject(scriptableObject)) { SerializedProperty arrayProperty = serializedObject.FindProperty("m_Array");

int remaining = arrayProperty.arraySize; var arrayElement = remaining > 0 ? arrayProperty.GetArrayElementAtIndex(0) : null; while (remaining > 0) { if (arrayElement.intValue < 0) { // Use a copy of the SerializedProperty because it cannot be used after DeleteCommand is invoked. var elementToDelete = arrayElement.Copy(); elementToDelete.DeleteCommand();

// The next element, if any, is now at the index of the deleted item, and arrayElement // automatically points at it, hence we don't have to move ahead } else { arrayElement.Next(false); } --remaining; }

serializedObject.ApplyModifiedProperties(); Debug.Log("Cleaned array contents: " + string.Join(" ", scriptableObject.m_Array)); } } }