Gameplay Ingredients是一组用于 Unity 游戏的运行时和编辑器工具:一组脚本的集合,可在制作游戏和原型时简化简单的任务。
您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

76 行
3.1 KiB

using UnityEngine;
using UnityEditor;
namespace NaughtyAttributes.Editor
{
[PropertyDrawer(typeof(MinMaxSliderAttribute))]
public class MinMaxSliderPropertyDrawer : PropertyDrawer
{
public override void DrawProperty(SerializedProperty property)
{
EditorDrawUtility.DrawHeader(property);
MinMaxSliderAttribute minMaxSliderAttribute = PropertyUtility.GetAttribute<MinMaxSliderAttribute>(property);
if (property.propertyType == SerializedPropertyType.Vector2)
{
Rect controlRect = EditorGUILayout.GetControlRect();
float labelWidth = EditorGUIUtility.labelWidth;
float floatFieldWidth = EditorGUIUtility.fieldWidth;
float sliderWidth = controlRect.width - labelWidth - 2f * floatFieldWidth;
float sliderPadding = 5f;
Rect labelRect = new Rect(
controlRect.x,
controlRect.y,
labelWidth,
controlRect.height);
Rect sliderRect = new Rect(
controlRect.x + labelWidth + floatFieldWidth + sliderPadding,
controlRect.y,
sliderWidth - 2f * sliderPadding,
controlRect.height);
Rect minFloatFieldRect = new Rect(
controlRect.x + labelWidth,
controlRect.y,
floatFieldWidth,
controlRect.height);
Rect maxFloatFieldRect = new Rect(
controlRect.x + labelWidth + floatFieldWidth + sliderWidth,
controlRect.y,
floatFieldWidth,
controlRect.height);
// Draw the label
EditorGUI.LabelField(labelRect, property.displayName);
// Draw the slider
EditorGUI.BeginChangeCheck();
Vector2 sliderValue = property.vector2Value;
EditorGUI.MinMaxSlider(sliderRect, ref sliderValue.x, ref sliderValue.y, minMaxSliderAttribute.MinValue, minMaxSliderAttribute.MaxValue);
sliderValue.x = EditorGUI.FloatField(minFloatFieldRect, sliderValue.x);
sliderValue.x = Mathf.Clamp(sliderValue.x, minMaxSliderAttribute.MinValue, Mathf.Min(minMaxSliderAttribute.MaxValue, sliderValue.y));
sliderValue.y = EditorGUI.FloatField(maxFloatFieldRect, sliderValue.y);
sliderValue.y = Mathf.Clamp(sliderValue.y, Mathf.Max(minMaxSliderAttribute.MinValue, sliderValue.x), minMaxSliderAttribute.MaxValue);
if (EditorGUI.EndChangeCheck())
{
property.vector2Value = sliderValue;
}
}
else
{
string warning = minMaxSliderAttribute.GetType().Name + " can be used only on Vector2 fields";
EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, context: PropertyUtility.GetTargetObject(property));
EditorDrawUtility.DrawPropertyField(property);
}
}
}
}