我正在使用带有可序列化对象的MonoBehavior来编辑编辑器中的数据。但是当我添加自定义编辑器时,更改后场景不会被标记为脏。所以我无法保存更改。在这种情况下,Undo.RecordObject似乎不起作用,因为它需要UnityEngine.Object类型的对象
我在下面添加了一个简单的例子
using UnityEngine;
public class DataManager : MonoBehaviour
{
public Data data;
}
using System;
[Serializable]
public class Data
{
public int num;
}
using UnityEditor;
[CustomEditor(typeof(DataManager))]
public class DataEditor : Editor
{
private DataManager dataManager;
public override void OnInspectorGUI()
{
dataManager = target as DataManager;
dataManager.data.num = EditorGUILayout.IntField(dataManager.data.num);
}
}
编辑:我找到的一个解决方案就是这个。但是这会让许多变量变得非常烦人。这个
应该有更好的方法using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine.SceneManagement;
[CustomEditor(typeof(DataManager))]
public class DataEditor : Editor
{
private DataManager dataManager;
public override void OnInspectorGUI()
{
dataManager = target as DataManager;
int num = EditorGUILayout.IntField(dataManager.data.num);
if (num != dataManager.data.num)
{
dataManager.data.num = num;
EditorSceneManager.MarkSceneDirty(SceneManager.GetActiveScene());
}
}
}