public object CreateInstance(AdviceArgs adviceArgs) { MyRangeAttribute clone = (MyRangeAttribute)this.MemberwiseClone(); clone.instance = adviceArgs.Instance; return(clone); }
// 重写OnGUI的方法(坐标,SerializedProperty 序列化属性,显示的文字) public override void OnGUI(Rect position, SerializedProperty property, GUIContent lable) { // attribute 是PropertyAttribute类中的一个属性 // 调用MyRangeAttribute中的最大和最小值还有文字信息,用于绘制时候的显示 MyRangeAttribute range = attribute as MyRangeAttribute; // 判断传进来的值类型 if (property.propertyType == SerializedPropertyType.Float) { EditorGUI.Slider(position, property, range.min, range.max, range.label); } else if (property.propertyType == SerializedPropertyType.Integer) { EditorGUI.IntSlider(position, property, (int)range.min, (int)range.max, range.label); } }
// 在给定的矩形内绘制属性 public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { //首先获取该特性,因为它包含滑动条的范围 MyRangeAttribute range = (MyRangeAttribute)attribute; // 现在根据属性是浮点值还是整数来确定将属性绘制为 Slider 还是 IntSlider。 if (property.propertyType == SerializedPropertyType.Float) { EditorGUI.Slider(position, property, range.min, range.max, label); } else if (property.propertyType == SerializedPropertyType.Integer) { EditorGUI.IntSlider(position, property, (int)range.min, (int)range.max, label); } else { EditorGUI.LabelField(position, label.text, "Use MyRange with float or int."); } }
// Draw the property inside the given rect public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { // First get the attribute since it contains the range for the slider MyRangeAttribute range = (MyRangeAttribute)attribute; // Now draw the property as a Slider or an IntSlider based on whether it's a float or Integer if (property.propertyType == SerializedPropertyType.Float) { EditorGUI.Slider(position, property, range.min, range.max, label); } else if (property.propertyType == SerializedPropertyType.Integer) { EditorGUI.Slider(position, property, (int)range.min, (int)range.max, label); } else { EditorGUI.LabelField(position, label.text, "Use MyRange with float or int."); } }