简体   繁体   English

在 Unity3D 中创建自定义检查器

[英]Creating a custom Inspector in Unity3D

I'm trying to create a custom Inspector.我正在尝试创建一个自定义检查器。 Even if I change the values in the custom inspector, the values are being reset automatically.即使我在自定义检查器中更改了值,这些值也会自动重置。

Following is my Custom Inspector,以下是我的自定义检查器,

[CustomEditor(typeof(MovePlayer))]
public class CustomInspector : Editor
{
    
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();

        MovePlayer move = (MovePlayer) target;
        GUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("ISpeed: ");
        move.speed = EditorGUILayout.FloatField(move.speed);
        GUILayout.EndHorizontal(); 
    }
}

The MovePlayer class implements MonoBehavior and IMovePlayer MovePlayer 类实现MonoBehaviorIMovePlayer

public class MovePlayer : MonoBehaviour, IMovePlayer
{
    public float speed { set; get; } = 2f;

}

The interface is as follows,界面如下,

public interface IMovePlayer
{
   
    float speed { set; get; }
}

Unfortunately, the values are being reset everytime even if I change the values in the Inspector.不幸的是,即使我在 Inspector 中更改了值,每次也会重置这些值。 what am I missing?我错过了什么?

Speed is a property not a field.速度是一个属性而不是一个领域。 Try changing:尝试改变:

 public float speed { set; get; } = 2f;

to

public float _speed = 2f;
public float speed { 
     set { _speed = value; }
     get => _speed; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM