Unity Color PropertyDrawer 如何使Color编辑更加便捷

您所在的位置:网站首页 unity怎么编辑代码 Unity Color PropertyDrawer 如何使Color编辑更加便捷

Unity Color PropertyDrawer 如何使Color编辑更加便捷

2023-04-09 18:59| 来源: 网络整理| 查看: 265

文章目录 简介 实现 Custom Property Drawer 16进制颜色转Color值

简介

在Inspector检视面板编辑Color值时,如果我们想要使用16进制颜色或修改其Alpha透明值时,需要打开Color窗口进行修改:

Color 窗口

本文通过为Color类自定义PropertyDrawer来实现更便捷的颜色值修改,如下图所示,在Color编辑后面增加了16进制颜色和Alpha透明值字段的绘制:

hexadecimal & alpha

实现 Custom Property Drawer

创建ColorPropertyDrawer类继承PropertyDrawer并为其添加CustomPropertyDrawer特性,重写OnGUI函数:

using UnityEngine; using UnityEditor; [CustomPropertyDrawer(typeof(Color))] public class ColorPropertyDrawer : PropertyDrawer { public override void OnGUI(Rect position, SerializedProperty property, GUIContent title) { base.OnGUI(position, property, title); } }

在OnGUI函数中自定义Color属性的绘制方法。

16进制颜色转Color值

通过EditorGUI中TextField为16进制颜色数据提供编辑输入,并将输入的16进制颜色数据转换为Color值,转换方法如下:

//16进制颜色转Color值 private static Color FromHex(string hexValue, float alpha) { if (string.IsNullOrEmpty(hexValue)) return Color.clear; if (hexValue[0] == '#') hexValue = hexValue.TrimStart('#'); if (hexValue.Length > 6) hexValue = hexValue.Remove(6, hexValue.Length - 6); int value = int.Parse(hexValue, NumberStyles.HexNumber); int r = value >> 16 & 255; int g = value >> 8 & 255; int b = value & 255; return new Color(r / 255f, g / 255f, b / 255f, alpha); }

完整代码:

using UnityEngine; using UnityEditor; using System.Globalization; [CustomPropertyDrawer(typeof(Color))] public class ColorPropertyDrawer : PropertyDrawer { private const float spacing = 5f; private const float hexWidth = 60f; private const float alphaWidth = 32f; public override void OnGUI(Rect position, SerializedProperty property, GUIContent title) { title = EditorGUI.BeginProperty(position, title, property); position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), title); var indent = EditorGUI.indentLevel; EditorGUI.indentLevel = 0; float colorWidth = position.width - hexWidth - spacing - alphaWidth - spacing; Color newColor = EditorGUI.ColorField(new Rect(position.x, position.y, colorWidth, position.height), property.colorValue); if (!newColor.Equals(property.colorValue)) property.colorValue = newColor; string hex = EditorGUI.TextField(new Rect(position.x + colorWidth + spacing, position.y, hexWidth, position.height), ColorUtility.ToHtmlStringRGB(property.colorValue)); try { newColor = FromHex(hex, property.colorValue.a); if (!newColor.Equals(property.colorValue)) property.colorValue = newColor; } finally { } float newAlpha = EditorGUI.Slider(new Rect(position.x + colorWidth + hexWidth + (spacing * 2f), position.y, alphaWidth, position.height), property.colorValue.a, 0f, 1f); if (!newAlpha.Equals(property.colorValue.a)) property.colorValue = new Color(property.colorValue.r, property.colorValue.g, property.colorValue.b, newAlpha); EditorGUI.indentLevel = indent; EditorGUI.EndProperty(); } //16进制颜色转Color值 private static Color FromHex(string hexValue, float alpha) { if (string.IsNullOrEmpty(hexValue)) return Color.clear; if (hexValue[0] == '#') hexValue = hexValue.TrimStart('#'); if (hexValue.Length > 6) hexValue = hexValue.Remove(6, hexValue.Length - 6); int value = int.Parse(hexValue, NumberStyles.HexNumber); int r = value >> 16 & 255; int g = value >> 8 & 255; int b = value & 255; return new Color(r / 255f, g / 255f, b / 255f, alpha); } }


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3