我有这个脚本:
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;
}
}
还有这个
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;
}
}
并在单声道脚本中使用它:
using UnityEditor;
using UnityEngine;
public class CubeSpawner : MonoBehaviour
{
public GameObject cubePrefab;
public LayerMask terrainLayer;
[GapRange(0, 100, 0.1f)]
public float gap = 0f;
并在单声道脚本中的某些地方使用差距变量。我在Visual Studio中没有任何错误,只是在编辑器中。
MissingMethodException:找不到类型GapRangeDrawer的默认构造函数
1条答案
按热度按时间u5i3ibmn1#
Unity希望创建
GapRangeDrawer
的示例,但不知道如何使用构造函数,即传递哪些值作为参数。给它添加一个没有参数的构造函数,即所谓的默认构造函数。
您仍然可以在代码中使用带3个参数的构造函数。