안녕하세요, 사용자 정의 편집기 값의 필드를 콤보 상자에 설정하려는 경우 인터넷에서 코드를 받았지만 콤보 상자에서 변경 한 내용이 전혀 저장되지 않는다는 것을 깨달았습니다. "저장"버튼을 사용하여 serializedfield를 만들려고했지만 C # 및 Unity가 처음입니다. 방문 해주셔서 감사합니다.
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);
사용자 정의 편집기에서 속성을 업데이트하려면 SerializedProperty
. 이렇게 :
[CustomEditor(typeof(DoorScript))]
public class ButtonDropDownMenu : Editor
{
SerializedProperty index;
void OnEnable()
{
index = serializedObject.FindProperty("index");
}
...
그런 다음 serializedObject.ApplyModifiedProperties();
완료되면 전화를 걸고 있는지 확인하세요 .
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
...
index = EditorGUILayout.Popup(arrayLabel, script.index, script.options);
...
serializedObject.ApplyModifiedProperties();
}
도움이 되었기를 바랍니다! 이것이 작동하지 않으면 알려주세요
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다