Getting error in the editor MissingMethodException: Default constructor not found. what is that meaning? and how to resolve it?

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

Getting error in the editor MissingMethodException: Default constructor not found. what is that meaning? and how to resolve it?

问题

I have this script:

using UnityEngine;

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

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

and this

using UnityEditor;
using UnityEngine;

[CustomPropertyDrawer(typeof(GapRangeAttribute))]
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;
    }
}

and using it in mono script:

using UnityEditor;
using UnityEngine;

public class CubeSpawner : MonoBehaviour
{
    public GameObject cubePrefab;
    public LayerMask terrainLayer;
    [GapRange(0, 100, 0.1f)]
    public float gap = 0f;
}

and using the gap variable in some places in the mono script.
I'm not getting any errors in the Visual Studio, only in the editor.

MissingMethodException: Default constructor not found for type GapRangeDrawer

英文:

I have this script :

using UnityEngine;

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

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

and this

using UnityEditor;
using UnityEngine;

[CustomPropertyDrawer(typeof(GapRangeAttribute))]
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;
    }
}

and using it in mono script:

using UnityEditor;
using UnityEngine;

public class CubeSpawner : MonoBehaviour
{
    public GameObject cubePrefab;
    public LayerMask terrainLayer;
    [GapRange(0, 100, 0.1f)]
    public float gap = 0f;

and using the gap variable in some places in the mono script.
i'm not getting any errors in the visual studio only in the editor.

MissingMethodException: Default constructor not found for type GapRangeDrawer

答案1

得分: 2

Unity想要创建一个GapRangeDrawer的实例,但不知道如何使用你的构造函数,即,传递哪些值作为参数。

为它添加一个没有参数的构造函数,也就是所谓的默认构造函数。

public GapRangeDrawer()
    : this(0, 100, 0.1f) // 将默认值传递给其他构造函数
{
}

你仍然可以在你的代码中使用具有3个参数的构造函数。

英文:

Unity wants to create an instance of GapRangeDrawer but does not know how to use your constructor, i.e., which values to pass as argument.

Add it a constructor with no parameters, i.e., a so called default constructor.

public GapRangeDrawer()
    : this(0, 100, 0.1f) // pass default values to the other constructor
{
}

You can still use the constructor with 3 parameters in your code.

huangapple
  • 本文由 发表于 2023年6月30日 03:56:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76584251.html
匿名

发表评论

匿名网友

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

确定