为什么我无法从Mono脚本中访问我在PropertyDrawer脚本中创建的新属性?

huangapple go评论68阅读模式
英文:

Why I cant access a new attribute I created in a propertydrawer script from mono script?

问题

using UnityEditor;
using UnityEngine;

public class GapRangeDrawer : PropertyDrawer
{
    float minRange;
    float maxRange;
    float sensitivity;

    public GapRangeDrawer(float minRange, float maxRange, float sensitivity)
    {
        this.minRange = minRange;
        this.maxRange = maxRange;
        this.sensitivity = sensitivity;
    }

    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        if (property.propertyType == SerializedPropertyType.Float)
        {
            EditorGUI.BeginProperty(position, label, property);
            EditorGUI.Slider(position, property, minRange, maxRange, label);
            property.floatValue = RoundValue(property.floatValue);
            EditorGUI.EndProperty();
        }
        else
        {
            EditorGUI.LabelField(position, label.text, "只能用于浮点数属性!");
        }
    }

    private float RoundValue(float value)
    {
        return Mathf.Round(value / sensitivity) * sensitivity;
    }
}

在一个脚本中:

public class CubeSpawner : MonoBehaviour
{
    public GameObject cubePrefab;
    [GapRangeDrawer(0.1f, 10.0f, 0.01f)]
    public float gap;
}

但是 GapRangeDrawer 还不存在。

我尝试创建了自己的属性,但无法访问它。我想要创建一个自定义的范围属性。

PropertyDrawer 脚本位于 Assets > Editor 文件夹下,但我仍然无法访问新的属性。

英文:
using UnityEditor;
using UnityEngine;

public class GapRangeDrawer : PropertyDrawer
{
    float minRange;
    float maxRange;
    float sensitivity;

    public GapRangeDrawer(float minRange, float maxRange, float sensitivity)
    {
        this.minRange = minRange;
        this.maxRange = maxRange;
        this.sensitivity = sensitivity;
    }

    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        if (property.propertyType == SerializedPropertyType.Float)
        {
            EditorGUI.BeginProperty(position, label, property);
            EditorGUI.Slider(position, property, minRange, maxRange, label);
            property.floatValue = RoundValue(property.floatValue);
            EditorGUI.EndProperty();
        }
        else
        {
            EditorGUI.LabelField(position, label.text, "Use [GapRangeDrawer] with float values only!");
        }
    }

    private float RoundValue(float value)
    {
        return Mathf.Round(value / sensitivity) * sensitivity;
    }
}

then in a mono script:

public class CubeSpawner : MonoBehaviour
{
    public GameObject cubePrefab;
    [Gap

but GapRangeDrawer is not exist.

i tried to create my own attribute but I can't access it.
i wanted to make a custom range attribute.

the propertydrawer script is under Assets > Editor folder but still I can't access the new attribute.

答案1

得分: 2

CustomPropertyDrawer属性应用于你的PropertyDrawer类。确保此类定义在一个名为Editor的文件夹内

    [CustomPropertyDrawer(typeof(GapRangeAttribute))]
    public class GapRangeDrawer : PropertyDrawer
    {
        ...
    }

你还需要创建一个`PropertyAttribute`。Unity在[Property Drawer文档页面](https://docs.unity3d.com/ScriptReference/PropertyDrawer.html)中提供了一个示例。

你的属性将类似于这样。

    public class GapRangeAttribute : PropertyAttribute
    {
        float minRange;
        float maxRange;
        float sensitivity;

        public GapRangeAttribute(float minRange, float maxRange, float sensitivity)
        {
            this.minRange = minRange;
            this.maxRange = maxRange;
            this.sensitivity = sensitivity;
        }
    }

现在你可以按照你的意图使用它。

    [GapRange(0, 100, 3)]
    public float someValue;
英文:

Apply the CustomPropertyDrawer attribute to your PropertyDrawer class. Make sure this class is defined inside of an Editor folder.

[CustomPropertyDrawer(typeof(GapRangeAttribute))]
public class GapRangeDrawer : PropertyDrawer
{
    ...
}

You also need to create a PropertyAttribute. Unity provides an example further down the Property Drawer Documentation Page.

Your attribute will look something like this.

public class GapRangeAttribute : PropertyAttribute
{
    float minRange;
    float maxRange;
    float sensitivity;

    public GapRangeAttribute(float minRange, float maxRange, float sensitivity)
    {
        this.minRange = minRange;
        this.maxRange = maxRange;
        this.sensitivity = sensitivity;
    }
}

Now you can use it in the way you intended.

[GapRange(0, 100, 3)]
public float someValue;

huangapple
  • 本文由 发表于 2023年6月29日 05:41:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76576881.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定