Unity自定义编辑器字段值无法保存问题求助及代码示例需求
Hey, I’ve run into this exact issue before when building custom Unity editors! The problem here is almost always that you’re not working with Unity’s serialization system properly—direct variable access skips the save logic, and you need to let Unity track changes to keep values from resetting.
Here’s a complete, fixed example that handles saving your integer fields and array correctly:
using UnityEditor; using UnityEngine; [CustomEditor(typeof(YourTargetScript))] public class YourCustomEditor : Editor { private SerializedProperty addInteger; private SerializedProperty howMuchIntegers; private SerializedProperty intNames; private void OnEnable() { // Bind serialized properties to your script's fields (critical for saving!) addInteger = serializedObject.FindProperty("addInteger"); howMuchIntegers = serializedObject.FindProperty("howMuchIntegers"); intNames = serializedObject.FindProperty("intNames"); } public override void OnInspectorGUI() { // Sync the serialized object with the latest values from the target serializedObject.Update(); GUILayout.BeginHorizontal(); GUILayout.BeginVertical(); // Use the serialized version of Toggle EditorGUILayout.PropertyField(addInteger, new GUIContent("Integers")); // Only show integer controls if the toggle is enabled if (addInteger.boolValue) { EditorGUILayout.PropertyField(howMuchIntegers, new GUIContent("Integer Count")); // Auto-adjust array size using serialized properties (avoids resetting) if (intNames.arraySize != howMuchIntegers.intValue) { intNames.arraySize = howMuchIntegers.intValue; } // Draw array fields for each integer name for (int i = 0; i < intNames.arraySize; i++) { SerializedProperty nameProp = intNames.GetArrayElementAtIndex(i); EditorGUILayout.PropertyField(nameProp, new GUIContent($"Integer Name {i+1}")); } } GUILayout.EndVertical(); GUILayout.EndHorizontal(); // Push changes back to the target and mark it as dirty for saving serializedObject.ApplyModifiedProperties(); } }
And here’s the matching target script (attach this to a GameObject in your scene):
using UnityEngine; public class YourTargetScript : MonoBehaviour { // Mark fields as serializable so Unity can save them [SerializeField] private bool addInteger; [SerializeField] private int howMuchIntegers; [SerializeField] private string[] intNames; }
Key Fixes Breakdown:
- Serialized Properties Over Direct Variables: By using
SerializedObjectandSerializedProperty, you let Unity’s built-in serialization system track changes—direct variable access doesn’t trigger save logic, which is why your values were resetting. - Update/Apply Cycle:
serializedObject.Update()pulls the latest data from the target, andserializedObject.ApplyModifiedProperties()pushes changes back while marking the object as "dirty" (so Unity saves it). - Proper Array Management: Instead of creating a new array directly (
intNames = new string[howMuchIntegers];), we modify the serialized array’sarraySizeproperty—this keeps Unity’s serialization system in sync.
Give this a shot, and your values should persist properly now!
内容的提问来源于stack exchange,提问作者CodeLazarus
相关产品推荐
相关产品推荐

