Unity - 接口需要的公共浮点数没有出现在检查器中 - 浮点值{get;放; }

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

我有一个要在 Inspector 中编辑的浮点值。接口需要在类上使用该值。

接口要求我显式声明访问器

{ get; set; }
,但是当这些被显式设置时,该值不会出现在检查器中。

[SerializeField]
不成功。

public interface IValueInterface
{
    float value { get; set; }
}

using UnityEngine;
public class MyClass : MonoBehaviour, IValueInterface
{
    public float nourishmentValue { get; set; } = 0.1f;
}

c# unity3d interface
2个回答
0
投票

Unity 不在检查器中显示属性。相反,您可以使用支持字段:

using UnityEngine;
public class MyClass : MonoBehaviour, IValueInterface
{
    [SerializeField]
    private float NourishmentValue = 0.1f;
    public float nourishmentValue
    {
        get => NourishmentValue;
        set => NourishmentValue = value;
    }
}

其他类会使用属性

nourishmentValue
但实际值将存储在字段
NourishmentValue
.


0
投票

您还可以在属性前使用 -field:- 将属性 (SerializeField) 应用于支持字段(由 auto 属性创建的字段)。

using UnityEngine;
public class MyClass : MonoBehaviour, IValueInterface
{
   [field: SerializeField]
   public float nourishmentValue { get; set; } = 0.1f;
}
© www.soinside.com 2019 - 2024. All rights reserved.