赞
踩
常用参数 | 描述 |
---|---|
CreatePropertyGUI | 使用 UI Toolkit 为属性创建自定义 GUI。 |
GetPropertyHeight | 重载此方法可指定此字段的 GUI 的高度(以像素为单位)。 |
OnGUI | 重写此方法,为属性创建自己的基于 IMGUI 的 GUI。 |
using UnityEngine;
using UnityEditor;
[CustomPropertyDrawer(typeof(string))]
public class StringPropertyDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
Rect btnRect = new Rect(position);
position.width -= 60;
btnRect.x += btnRect.width - 60;
btnRect.width = 60;
EditorGUI.BeginProperty(position, label, property);
EditorGUI.PropertyField(position, property, true);
if (GUI.Button(btnRect, "select"))
{
string path = property.stringValue;
string selectStr = EditorUtility.OpenFilePanel("选择文件", path, "");
if (!string.IsNullOrEmpty(selectStr))
{
property.stringValue = selectStr;
}
}
EditorGUI.EndProperty();
}
}
using UnityEngine;
public class Test : MonoBehaviour
{
public string str;
}
using UnityEngine;
public class Test : MonoBehaviour
{
[Range(0.0F, 10.0F)]
public float myFloat = 0.0F;
}
using UnityEngine;
public class RangeAttribute : PropertyAttribute
{
public float min;
public float max;
public RangeAttribute(float min, float max)
{
this.min = min;
this.max = max;
}
}
using UnityEngine;
using UnityEditor;
using System;
[CustomPropertyDrawer(typeof(RangeAttribute))]
public class RangeDrawer : PropertyDrawer
{
// 在给定的矩形内绘制属性
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
// 首先获取属性,因为它包含滑块的范围
RangeAttribute range = attribute as RangeAttribute;
// 现在,根据属性是浮点数还是整数,将属性绘制为 Slider 或 IntSlider。
if (property.propertyType == SerializedPropertyType.Float)
{
EditorGUI.Slider(new Rect(position.x, position.y, position.width * 0.8f, position.height), property, range.min, range.max);
EditorGUI.LabelField(new Rect(position.x + position.width * 0.8f, position.y, position.width - (position.x + position.width * 0.8f), position.height), "滑到了" + property.floatValue);
}
else if (property.propertyType == SerializedPropertyType.Integer)
EditorGUI.IntSlider(position, property, Convert.ToInt32(range.min), Convert.ToInt32(range.max), label);
else
EditorGUI.LabelField(position, label.text, "将 Range 与 float 或 int 一起使用。");
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。