为什么在运行游戏时门锁状态会被覆盖并设置为false?

时间:2019-03-25 18:19:42

标签: c# unity3d

当游戏已经在运行时,它运行良好。我可以更改每个门的锁状态。但是在运行游戏之前,我将一个或多个门设置为锁定状态(已选中将标志设置为true),然后运行游戏,它将所有门都更改为假解锁状态。

我希望如果我在运行游戏之前也设置了门锁状态,它将在游戏运行时保持此更改。

第一个脚本只有一行:

using UnityEditor;

[CustomEditor(typeof(DoorsLockManager))]
public class DoorsLockManagerEditor : Editor
{
    DoorsLockManager _manager;

    public override void OnInspectorGUI()
    {
        _manager = (DoorsLockManager)target;

        base.OnInspectorGUI();

        if (_manager.Doors.Count <= 0)
            return;
        for (var i = 0; i < _manager.Doors.Count; i++)
        {
            if (_manager.Doors[i] == null)
                continue;

            _manager.Doors[i].GetLockState = EditorGUILayout.Toggle("Door " + i + " Lockstate", _manager.Doors[i].GetLockState);
        }
    }
}

第二个脚本控制着门:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System;

public class HoriDoorManager : MonoBehaviour
{
    public List<DoorHori> doors = new List<DoorHori>(); 
    public bool doorLockState;

    private void Awake()
    {
        if (transform.parent != null)
        {
            Transform parent = transform.parent;
            var children = parent.GetComponentsInChildren<Transform>();

            if (children != null)
            {
                foreach (Transform door in children)
                {
                    if (door.name == "Door_Left" || door.name == "Door_Right")
                        doors.Add(door.GetComponent<DoorHori>());
                }
            }
            //ColorDoors(Color.red, Color.green, doorLockState);
        }
    }

    void OnTriggerEnter()
    {
        if (doorLockState == false)
        {
            if (doors != null)
            {
               for(int i =0; i < doors.Count; i++)
                {
                    doors[i].OpenDoor();
                }
            }
        }
    }

    private void ColorDoors(Color red, Color green, bool state)
    {
        List<Transform> children = new List<Transform>();

        for (int i = 0; i < doors.Count; i++)
        {
            foreach (Transform child in doors[i].GetComponentsInChildren<Transform>())
            {
                if (child == doors[i].transform)
                    continue;

                var renderer = child.GetComponent<Renderer>();
                renderer.material.shader = Shader.Find("Unlit/ShieldFX");

                if(state == true)
                {
                    renderer.material.SetColor("_MainColor", red);
                }
                else
                {
                    renderer.material.SetColor("_MainColor", green);
                }
            }
        }
    }

    public bool GetLockState
    {
        get { return doorLockState; }
        set { doorLockState = value; }
    }
}

最后一个脚本附加到每个门上:

import pandas as pd

2 个答案:

答案 0 :(得分:2)

属性不可序列化

您根本不应该在编辑器脚本中进行更改,因为它们没有序列化=>从不保存,并且始终具有默认值。

如果它是属性之类的

,它将起作用
public bool GetLockState
{
    get { return doorLockState; }
    set { doorLockState = value; }
}

如果您的doorLockState仍然是public,为什么还要使用它?

问题在于,这不会将已更改的字段doorLockState标记为“脏”。

但是,由于以下原因,您不应该使用the other answer建议的EditorUtility.SetDirty

  

从5.3开始,随着多场景编辑的引入,该功能不再用于修改场景中的对象。相反,应该在更改对象之前使用Undo.RecordObject。这会将对象的场景标记为,并在编辑器中提供撤消条目。'


相反,您必须在编辑脚本中更改doorLockState的值。

如果在private位置,则必须添加[SerializeField]

[SerializeField] private bool doorLockState;

但是您的情况似乎仍然是public,因此它会自动序列化。

始终更适合使用SerializedPropertyPropertyField,而不是直接在编辑器脚本中操纵值!

一开始它看起来更复杂,但这会自动处理诸如“撤消/重做”之类的内容并自动将其标记为脏内容(从而保存更改),因此您无需执行其他任何操作:

[CustomEditor(typeof(DoorsLockManager))]
public class DoorsLockManagerEditor : Editor
{
    private SerializedProperty _doors;

    private void OnEnable()
    {
        _doors = serializedObject.FindProperty("Doors");
    }

    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();

        for (int i = 0; i < _doors.arraySize; i++)
        {
            var door = _doors.GetArrayElementAtIndex(i);

            // if door == null the script itself has an error since it can't even find the SerializedProperty
            if (door == null)
            {
                EditorGUILayout.HelpBox("There was an error in the editor script!\nPlease check the log", MessageType.Error);
                Debug.LogError("Couldn't get door property", target);
                return;
            }

            if (door.objectReferenceValue == null) continue;

            // FindPropertyRelative seems not to work for MonoBehaviour classes
            // so we have to use this hack around
            var serializedDoor = new SerializedObject(door.objectReferenceValue);

            // If it's public no worry anyway
            // If it's private still works since we made it a SerializeField
            var lockState = serializedDoor.FindProperty("doorLockState");

            // Fetch current values into the serialized "copy"
            serializedDoor.Update();

            if (lockState == null)
            {
                EditorGUILayout.HelpBox("There was an error in the editor script!\nPlease check the log", MessageType.Error);
                Debug.LogError("Couldn't get lockState property", target);
                return;
            }

            // for the PropertyField there is 
            // no return value since it automatically uses
            // the correct drawer for the according property
            // and directly changes it's value
            EditorGUILayout.PropertyField(lockState, new GUIContent("Door " + i + " Lockstate"));

            // or alternatively
            //lockState.boolValue = EditorGUILayout.Toggle("Door " + i + " Lockstate", lockState.boolValue);

            // Write back changes, mark as dirty if changed
            // and add a Undo history entry
            serializedDoor.ApplyModifiedProperties();
        }
    }
}

enter image description here

(当然,值只会更新OnInspectorGUI,因此如果您打开了多个“检查器”选项卡,则只有将其悬停时才会更新)

答案 1 :(得分:0)

您是否在 DoorsLockManagerEditor 中尝试过EditorUtility.SetDirty

似乎编辑器没有保留初始值。