所以在我的游戏中我有一个需要平滑移动的对象Vector3 fromPosition
to Vector3 toPosition
高速float speed
,然后返回到开始的地方。一切都非常简单,但是为了在设置关卡时尝试让生活变得更轻松,我决定为此脚本创建一个自定义检查器,其中的按钮允许我将目标位置设置为对象的当前位置,这样我就可以将其移动到到需要的位置并单击按钮,而不是输入所有坐标。我以为我已经一切正常,但后来开始看到一些非常奇怪的行为,经过一番尝试后,情况似乎如下:第一次使用按钮时一切都很好。此后每次使用该按钮时,检查器中的值都会正确更改,但点击“播放”后,值toPosition
and fromPosition
恢复到第一次使用该按钮时的状态。 (他们不会在停止时再次恢复)。但是,如果我手动输入这些值,它就可以正常工作。很奇怪,有人知道这里会发生什么吗?脚本和自定义检查器的代码如下。
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class MovingGrapple : MonoBehaviour
{
public Vector3 fromPosition;
public Vector3 toPosition;
public float speed;
Rigidbody thisBody;
Grapple player;
// Start is called before the first frame update
void Start()
{
thisBody = GetComponent<Rigidbody>();
player = GameObject.Find("Head").GetComponent<Grapple>();
}
private void FixedUpdate()
{
thisBody.MovePosition(Vector3.MoveTowards(transform.position, toPosition, Time.fixedDeltaTime * speed));
if(transform.position == toPosition)
{
transform.position = fromPosition;
if (player.activeTarget != null && GetComponentsInChildren<Transform>().Contains(player.activeTarget.transform))
{
player.BreakGrapple();
GameObject.Destroy(player.activeTarget);
}
}
}
public void SetFromPosition()
{
fromPosition = transform.position;
}
public void SetToPosition()
{
toPosition = transform.position;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(MovingGrapple))]
public class MovingGrappleInspector : Editor
{
public override void OnInspectorGUI()
{
DrawDefaultInspector();
MovingGrapple myTarget = (MovingGrapple)target;
if (GUILayout.Button("Set From position."))
{
myTarget.SetFromPosition();
}
if (GUILayout.Button("Set To position."))
{
myTarget.SetToPosition();
}
}
}
Thanks.
这不仅会在您按播放键时发生..您的更改是never saved!
如果可能的话,您永远不应该将编辑器脚本与直接访问target
除非你确切地知道自己在做什么!
您特别需要“手动”将更改的对象标记为dirty。否则,更改只是暂时的,直到您的对象再次反序列化(进入/退出 PlayMode 或重新加载场景或资产)。
你可以before更改添加了一个Undo.RecordObject https://docs.unity3d.com/ScriptReference/Undo.RecordObject.html
if (GUILayout.Button("Set From position."))
{
Undo.RecordObject(myTarget, "SetFromPosition");
myTarget.SetFromPosition();
}
if (GUILayout.Button("Set To position."))
{
Undo.RecordObject(myTarget, "SetToPosition");
myTarget.SetToPosition();
}
另外(在您的用例中听起来不太可能,但是)
重要的:要正确处理 objectToUndo 是 Prefab 实例的实例,PrefabUtility.RecordPrefabInstancePropertyModifications https://docs.unity3d.com/ScriptReference/PrefabUtility.RecordPrefabInstancePropertyModifications.html必须在之后调用RecordObject
.
一般来说,总是会经历SerializedProperty https://docs.unity3d.com/ScriptReference/SerializedProperty.html在可能的情况下,例如
[CustomEditor(typeof(MovingGrapple))]
public class MovingGrappleInspector : Editor
{
private SerializedProperty fromPosition;
private SerializedProperty toPosition;
private MovingGrapple myTarget
private void OnEnable()
{
fromPosition = serializedObject.FindProperty("fromPosition");
toPosition = serializedObject.FindProperty("toPosition");
myTarget = (MovingGrapple)target;
}
public override void OnInspectorGUI()
{
DrawDefaultInspector();
// This loads the current real values into the serialized properties
serializedObject.Update();
if (GUILayout.Button("Set From position."))
{
// Now go through the SerializedProperty
fromPosition.vector3Value = myTarget.transform.position;
}
if (GUILayout.Button("Set To position."))
{
toPosition.vector3Value = myTarget.transform.position;
}
// This writes back any changes properties into the actual component
// This also automatically handles all marking the scene and assets dirty -> saving
// And also handles proper Undo/Redo
serializedObject.ApplyModifiedProperties();
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)