我正在制作一个自定义编辑器,使用两个GUI按钮实例化和销毁多个对象。在编辑模式下,一切正常,我可以实例化多个预制件,然后逐个销毁它们,从最后一次实例化到第一次,但是只要我按下#34;播放"并且"停止",我无法销毁任何先前实例化的预制件(在播放模式之前实例化)。我可以实例化新的预制件然后销毁它们,但是对于那些在我玩之前实例化的预制件 - 它们不会受到影响。
现在,我并不担心在播放模式下会发生这种情况,但我绝对希望从播放模式之前继续这一点。
每当实例化预制件时,我都会将其添加到堆栈并跟踪大小。在我点击播放后 - 停止,它似乎重置为0。
在浏览了关于类似问题的大量帖子并检查了Unity的API之后,我意识到我需要找到一种方法来保存自定义编辑器设置和变量,方法是将它们序列化或弄脏,我只是不知道怎么做到这一点。我已经尝试了这些帖子中的建议,但似乎没有任何工作在我的情况下(或者我做错了)。
我有一个Monobehaviour脚本和一个编辑器脚本。我会针对单一类型的对象发布片段,因为其他部分是相同的。
这是第一个:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable, ExecuteInEditMode]
public class ObjectControl: MonoBehaviour {
[SerializeField, HideInInspector]
public Stack<GameObject> undoStack = new Stack<GameObject>();
public GameObject instance;
public int undoStackSize;
public int objectSelectionIndex = 0;
public void placeObject()
{
switch (objectSelectionIndex)
{
case 1:
Debug.Log("Just received a number 1 from the editor");
GameObject object_A = Resources.Load("Prefabs/Object_A") as GameObject;
instance = Instantiate(object_A, this.transform.position, this.transform.rotation, this.transform);
undoStack.Push(instance);
undoStackSize = undoStack.Count;
break;
case 4:
Debug.Log("Just received a number 4 from the editor, deleting the object");
if (undoStack.Count > 0)
{
GameObject objToUndo = undoStack.Pop();
DestroyImmediate(objToUndo);
undoStackSize--;
}
else
{
Debug.Log("Stack is empty! Stack size is: " + undoStack.Count);
}
break;
}
}
编辑脚本:
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Collections.Generic;
[CustomEditor(typeof(ObjectControl)), CanEditMultipleObjects]
public class ObjectControlEditor : Editor
{
int objectSelectionToolbar = 0;
int numberOfPossibleUndo;
bool chooseOption = false;
bool objectSelectionFoldout = false;
public ObjectControl scriptTarget;
public void Awake()
{
scriptTarget = (ObjectControl)target;
}
public override void OnInspectorGUI()
{
DrawDefaultInspector();
GUI.changed = false;
chooseOption = EditorGUILayout.Foldout(chooseOption, "Choose a segment to add:");
if (chooseOption)
{
EditorGUILayout.BeginVertical();
stationSelectionFoldout = GUILayout.Toggle(stationSelectionFoldout, "" + (stationSelectionFoldout ? "▼ Object selection ▼" : "► Object selection ◄"), "Button", GUILayout.MaxWidth(Screen.width), GUILayout.Height(25));
if (objectSelectionFoldout)
{
GUILayout.Space(5); //Space before a text box
GUILayout.Box("Select lenght of the station:");
GUILayout.Space(5); //Space after a text box and before a toolbox
string[] objectSelectionToolbarOptions = new string[] { "Object A", "Object B", "Object C" };
stationSelectionToolbar = GUILayout.Toolbar(objectSelectionToolbar, objectSelectionToolbarOptions, GUILayout.MinWidth(Screen.width), GUILayout.Height(50));
GUILayout.Space(5);
RollerCoasterBuilder scriptTarget = (RollerCoasterBuilder)target;
numberOfPossibleUndo = scriptTarget.undoStackSize;
switch (objectSelectionToolbar)
{
case 0:
GUILayout.BeginHorizontal();
if (GUILayout.Button("Place selected object", GUILayout.Height(30)))
{
scriptTarget.objectSelectionIndex = 1;
scriptTarget.PlaceObject();
}
GUILayout.Space(5);
if (GUILayout.Button("Undo" + "(" + numberOfPossibleUndo + ")", GUILayout.Height(30)))
{
scriptTarget.objectSelectionIndex = 4;
scriptTarget.PlaceObject();
}
GUILayout.EndHorizontal();
break;
}
}
}
if (GUI.changed)
{
EditorUtility.SetDirty(target);
}
}
}
答案 0 :(得分:1)
Unity无法序列化堆栈数据类型。尝试将undoStack转换为Array []或List&lt;&gt;。您可以在此处查看可序列化类型的列表:https://docs.unity3d.com/ScriptReference/SerializeField.html
或者,如果Unity尚不支持,您可以编写自己的序列化版本的类型。见:http://answers.unity3d.com/questions/460727/how-to-serialize-dictionary-with-unity-serializati.html(第二个回复)。
另外,请务必阅读API中EditorUtility.SetDirty()页面的NOTE部分,以确保您当前的实现不会针对Unity的粒度运行。我无法链接它,因为我只有足够的代表发布2个链接...
答案 1 :(得分:0)
感谢Michael的意见。
我决定抛弃堆栈并继续从上到后逐个删除子对象,如本文所示:Simple way to Delete the Last Child of a GameObject
播放模式后没有更多问题:)
度过美好的一天!