英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论