【发布时间】:2020-04-16 06:56:50
【问题描述】:
您好,我正在尝试保存自定义编辑器值的字段设置在组合框中,我从 Internet 获取代码,但我意识到在组合框中所做的更改根本没有保存,我尝试使用“保存”按钮,我尝试制作序列化字段,但我是 C# 和统一的新手。感谢您的光临,祝您有美好的一天。
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(DoorScript))]
[System.Serializable]
public class ButtonDropDownMenu : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
DoorScript script = (DoorScript)target;
GUIContent arrayLabel = new GUIContent("Color");
script.index = EditorGUILayout.Popup(arrayLabel, script.index, script.options);
/*serializedObject.FindProperty("index").intValue = script.index = EditorGUILayout.Popup(arrayLabel, script.index, script.options);
script.index = EditorGUILayout.Popup(arrayLabel, script.index, script.options);
var properlyToSave = serializedObject.FindProperty("Index");
EditorGUILayout.PropertyField(properlyToSave);
serializedObject.ApplyModifiedProperties();¨*/
}
}
using UnityEngine;
using UnityEngine.Experimental.Rendering.Universal;
public class DoorScript : MonoBehaviour
{
private SpriteRenderer myColor;
public Light2D doorLight;
public Light2D doorPLight;
[HideInInspector][SerializeField]
public int index = 0;
[HideInInspector][SerializeField]
public string[] options = new string[]
{
"White",
"Red",
"Blue",
"Cyan"
};
private void Awake()
{
myColor = GetComponent<SpriteRenderer>();
ColorChanger();
}
void Start()
{
}
private void ColorChanger()
{
if (index <= 0)
{
DoorsLights(Color.grey);
}
else if (index == 1)
{
DoorsLights(Color.red);
}
else if (index == 2)
{
DoorsLights(Color.blue);
}
else if (index >= 3)
{
DoorsLights(Color.green);
}
}
private void DoorsLights(Color color)
{
myColor.color = color;
doorLight.color = color;
doorPLight.color = color;
}
}
编辑:
我工作完美,我做了改变而不是
index = EditorGUILayout.Popup(arrayLabel, script.index, script.options);
我用过
index.intValue = EditorGUILayout.Popup(arrayLabel, script.index, script.options);
【问题讨论】:
-
当你取消注释
serializedObject.ApplyModifiedProperties();时它仍然没有保存吗...那行应该是必要的 -
我会再试一次,请稍等编辑:还没有工作