Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.UI;
- [ExecuteAlways]
- public class UIPalette : MonoBehaviour {
- public UIPaletteGroup[] colors = new UIPaletteGroup[0];
- void OnValidate() {
- foreach(var group in colors)
- group.Refresh();
- }
- }
- [System.Serializable]
- public class UIPaletteGroup {
- [SerializeField, HideInInspector]
- Color _color;
- [SerializeField, HideInInspector]
- Graphic[] _graphics = new Graphic[0];
- public void SetColor(Color color) {
- _color = color;
- Refresh();
- }
- public void Refresh() {
- foreach(var graphic in _graphics) {
- if(graphic != null)
- graphic.color = _color;
- }
- }
- }
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEditor;
- [CustomPropertyDrawer(typeof(UIPaletteGroup))]
- public class UIPaletteGroupEditor : PropertyDrawer {
- public override void OnGUI(Rect pos, SerializedProperty prop, GUIContent label) {
- var bgRect = pos;
- pos.height = EditorGUIUtility.singleLineHeight;
- label = EditorGUI.BeginProperty(pos, label, prop);
- var colorProperty = prop.FindPropertyRelative("_color");
- var colorValue = colorProperty.colorValue;
- EditorGUI.DrawRect(bgRect, colorValue);
- EditorGUI.indentLevel = 0;
- var contentPosition = EditorGUI.PrefixLabel(pos, new GUIContent(ColorUtility.ToHtmlStringRGBA(colorValue)));
- colorProperty.colorValue = EditorGUI.ColorField(contentPosition, colorValue);
- pos.y += pos.height;
- EditorGUI.EndProperty();
- var graphicsProperty = prop.FindPropertyRelative("_graphics");
- label = EditorGUI.BeginProperty(pos, label, graphicsProperty);
- contentPosition = EditorGUI.PrefixLabel(pos, new GUIContent("Graphics"));
- for(int i = 0; i < graphicsProperty.arraySize; i++) {
- var elementProperty = graphicsProperty.GetArrayElementAtIndex(i);
- elementProperty.objectReferenceValue = EditorGUI.ObjectField(contentPosition, elementProperty.objectReferenceValue, typeof(Graphic), true);
- }
- EditorGUI.EndProperty();
- }
- public override float GetPropertyHeight(SerializedProperty prop, GUIContent label) {
- SerializedObject childObj = prop.serializedObject;
- SerializedProperty ite = childObj.GetIterator();
- float totalHeight = EditorGUI.GetPropertyHeight(prop, label, true) + EditorGUIUtility.standardVerticalSpacing;
- while(ite.NextVisible(true)) {
- totalHeight += EditorGUI.GetPropertyHeight(ite, label, true) + EditorGUIUtility.standardVerticalSpacing;
- }
- return totalHeight;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement