场景重新加载后 Unity 引用中断

问题描述 投票:0回答:2

我在统一中遇到了一个奇怪的问题,一旦我重新加载场景,引用就会不断中断,我试图了解到底发生了什么,但没有运气。 我制作了一个脚本来复制您可以在下面找到的问题。

当我通过更改大小编辑最后一个数据元素“列表”时,更改会反映在其他数据对象列表上,因为它们被视为引用。
enter image description here

如果我重新加载场景,更改将不再像以前那样反映,这次它的行为就像副本而不是引用。
有人可以帮我弄清楚发生了什么事吗?
enter image description here

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class test : MonoBehaviour
{

    public List<data> Data = new List<data>();
}
[System.Serializable]
public class data
{

    public List<int> list = new List<int>();
}
[CustomEditor(typeof(test))]
public class testEditor:Editor
{
    test test;
    public void OnEnable()
    {
        test = (test)target;
    }
    public override void OnInspectorGUI()
    {
        DrawDefaultInspector();

        if (GUILayout.Button("Add"))
        {
            data data = new data();
            if (test.Data.Count >= 1) data.list = test.Data[test.Data.Count - 1].list;
            test.Data.Add(data);

            EditorUtility.SetDirty(test);
        }
        if (GUILayout.Button("Clear"))
        {
            test.Data.Clear();

            EditorUtility.SetDirty(test);
        }
    }
}
c# unity-game-engine
2个回答
2
投票

一般来说:不要直接访问和更改

MonoBehaviour
实例的值!

正如您所指出的,您将必须处理所有类型的脏标记并拯救自己。在编辑器中重新打开场景时,您会遇到什么情况,某些内容没有正确标记为脏,因此没有与场景一起保存。


始终宁愿通过

SerializedProperty
来处理所有脏标记和保存,尤其是自动撤消/重做等:

[CustomEditor(typeof(test))]
public class testEditor : Editor
{
    private SerializedProperty Data;

    public void OnEnable()
    {
        Data = serializedObject.FindProperty(nameof(test.Data));
    }

    public override void OnInspectorGUI()
    {
        DrawDefaultInspector();

        // load all current values of the properties in test into the SerializedProperty "clones"
        serializedObject.Update();

        if (GUILayout.Button("Add"))
        {
            // this simply adds a new entry to the list
            // since the data and list are both serializable this already initializes them with values
            Data.arraySize++;


            // Actually the entire following block is redundant 
            // by using Data.arraySize++; the new added entry automatically 
            // is a full copy of the entry before!
            // I just decided to add it as example how you would access further nested SerializedProperties

            //// if there was an element before now there are two
            //if (Data.arraySize >= 2)
            //{
            //    // get the last added element
            //    var lastElement = Data.GetArrayElementAtIndex(Data.arraySize - 1);
            //    var beforeElement = Data.GetArrayElementAtIndex(Data.arraySize - 2);

            //    // deep clone the list
            //    var lastElementList = lastElement.FindPropertyRelative(nameof(data.list));
            //    var beforeElementList = beforeElement.FindPropertyRelative(nameof(data.list));

            //    lastElementList.arraySize = beforeElementList.arraySize;
            //    for (var i = 0; i < lastElementList.arraySize; i++)
            //    {
            //        lastElementList.GetArrayElementAtIndex(i).intValue = beforeElementList.GetArrayElementAtIndex(i).intValue;
            //    }
            //}
        }

        if (GUILayout.Button("Clear"))
        {
            Data.arraySize = 0;
        }

        // write back the values of the SerializedProperty "clones" into the real properties of test
        serializedObject.ApplyModifiedProperties();
    }
}

现在可以处理所有脏标记、正确保存场景、自动撤消/重做等,您不必再关心这些了。


还有一点“专业”提示:使用

ReorderableList
!它的设置看起来有点棘手,但功能极其强大:正如名称所示,它允许您通过在检查器中拖放来简单地重新排序元素,还允许从中间删除项目,这是不可能的普通列表抽屉。这完全取代了您的
Add
Clear
按钮:

using UnityEditor;
using UnityEditorInternal;

[CustomEditor(typeof(test))]
public class testEditor : Editor
{
    private SerializedProperty Data;
    private ReorderableList dataList;

    public void OnEnable()
    {
        Data = serializedObject.FindProperty(nameof(test.Data));

        //                                 should the list
        //                                                     | be reorderable by drag&drop of the entries?
        //                                                     |     | display a header for the list?
        //                                                     |     |     | have an Add button?
        //                                                     |     |     |     | have a Remove button?
        //                                                     v     v     v     v
        dataList = new ReorderableList(serializedObject, Data, true, true, true, true)
        {
            // what shall be displayed as header
            drawHeaderCallback = rect => EditorGUI.LabelField(rect, Data.displayName),

            elementHeightCallback = index =>
            {
                var element = Data.GetArrayElementAtIndex(index);
                var elementList = element.FindPropertyRelative(nameof(data.list));
                return EditorGUIUtility.singleLineHeight * (elementList.isExpanded ? elementList.arraySize + 4 : 3);
            },

            drawElementCallback = (rect, index, isFocused, isActive) =>
            {
                var element = Data.GetArrayElementAtIndex(index);

                EditorGUI.LabelField(new Rect(rect.x,rect.y,rect.width,EditorGUIUtility.singleLineHeight), element.displayName);
                // in order to print the list in the next line
                rect.y += EditorGUIUtility.singleLineHeight;

                var elementList = element.FindPropertyRelative(nameof(data.list));
                EditorGUI.PropertyField(new Rect(rect.x, rect.y, rect.width,  EditorGUIUtility.singleLineHeight * (elementList.isExpanded ? elementList.arraySize + 1 : 1)), elementList, true);
            }
        };
    }

    public override void OnInspectorGUI()
    {
        // load all current values of the properties in test into the SerializedProperty "clones"
        serializedObject.Update();

        dataList.DoLayoutList();

        // write back the values of the SerializedProperty "clones" into the real properties of test
        serializedObject.ApplyModifiedProperties();
    }
}


注意 如果情况并非如此:

testEditor
部分应该

  • 可以放置在名为
    Editor
  • 的文件夹中的不同脚本中
  • 或者您应该将与

    UnityEditor
    命名空间相关的任何内容包装在预处理器中,例如

    #if UNITY_EDITOR
    using UnityEditor;
    using UnityEditorInternal;
    #endif
    
    ...
    
    #if UNITY_EDITOR
    [CustomEditor(typeof(test))]
    public class testEditor : Editor
    {
        ...
    }
    #endif
    

否则,在构建应用程序时您会遇到错误,因为

UnityEditor
命名空间在构建中被剥离,并且仅存在于 Unity 编辑器本身内部。


0
投票

我面临类似的错误,适合我的解决方法是尝试使用单例模式;

您需要使用单例模式使您的脚本在内存中唯一:

public static MainManager m_Instance;
public static MainManager Instance
{
    get
    {
        if (m_Instance != null)
            return m_Instance;
        m_Instance = FindObjectOfType<MainManager>();
        DontDestroyOnLoad(m_Instance);
        return m_Instance;
    }
}

这意味着与您的脚本相关的属性将保留

private void Awake()
{
    var obj = MainManager.Instance;
}

我希望它会有用。

我附上参考资料:

https://learn.unity.com/tutorial/como-implementar-la-continuacion-de-datos-entre-escenas?uv=2020.3&pathwayId=62b2353fedbc2a0e190d25c3&missionId=61a586b8edbc2a107c10d9c8#62c5b816edbc2a1 e2176ceb8

https://gamedevbeginner.com/singletons-in-unity-the-right-way/

© www.soinside.com 2019 - 2024. All rights reserved.