Advertisement
Guest User

UIPalette

a guest
Dec 12th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3.  
  4. [ExecuteAlways]
  5. public class UIPalette : MonoBehaviour {
  6.  
  7. public UIPaletteGroup[] colors = new UIPaletteGroup[0];
  8.  
  9. void OnValidate() {
  10. foreach(var group in colors)
  11. group.Refresh();
  12. }
  13. }
  14.  
  15. [System.Serializable]
  16. public class UIPaletteGroup {
  17. [SerializeField, HideInInspector]
  18. Color _color;
  19. [SerializeField, HideInInspector]
  20. Graphic[] _graphics = new Graphic[0];
  21. public void SetColor(Color color) {
  22. _color = color;
  23. Refresh();
  24. }
  25. public void Refresh() {
  26. foreach(var graphic in _graphics) {
  27. if(graphic != null)
  28. graphic.color = _color;
  29. }
  30. }
  31. }
  32.  
  33. using UnityEngine;
  34. using UnityEngine.UI;
  35. using UnityEditor;
  36.  
  37. [CustomPropertyDrawer(typeof(UIPaletteGroup))]
  38. public class UIPaletteGroupEditor : PropertyDrawer {
  39. public override void OnGUI(Rect pos, SerializedProperty prop, GUIContent label) {
  40. var bgRect = pos;
  41. pos.height = EditorGUIUtility.singleLineHeight;
  42. label = EditorGUI.BeginProperty(pos, label, prop);
  43. var colorProperty = prop.FindPropertyRelative("_color");
  44. var colorValue = colorProperty.colorValue;
  45. EditorGUI.DrawRect(bgRect, colorValue);
  46. EditorGUI.indentLevel = 0;
  47. var contentPosition = EditorGUI.PrefixLabel(pos, new GUIContent(ColorUtility.ToHtmlStringRGBA(colorValue)));
  48. colorProperty.colorValue = EditorGUI.ColorField(contentPosition, colorValue);
  49. pos.y += pos.height;
  50. EditorGUI.EndProperty();
  51.  
  52. var graphicsProperty = prop.FindPropertyRelative("_graphics");
  53. label = EditorGUI.BeginProperty(pos, label, graphicsProperty);
  54. contentPosition = EditorGUI.PrefixLabel(pos, new GUIContent("Graphics"));
  55. for(int i = 0; i < graphicsProperty.arraySize; i++) {
  56. var elementProperty = graphicsProperty.GetArrayElementAtIndex(i);
  57. elementProperty.objectReferenceValue = EditorGUI.ObjectField(contentPosition, elementProperty.objectReferenceValue, typeof(Graphic), true);
  58. }
  59. EditorGUI.EndProperty();
  60. }
  61.  
  62. public override float GetPropertyHeight(SerializedProperty prop, GUIContent label) {
  63. SerializedObject childObj = prop.serializedObject;
  64. SerializedProperty ite = childObj.GetIterator();
  65.  
  66. float totalHeight = EditorGUI.GetPropertyHeight(prop, label, true) + EditorGUIUtility.standardVerticalSpacing;
  67. while(ite.NextVisible(true)) {
  68. totalHeight += EditorGUI.GetPropertyHeight(ite, label, true) + EditorGUIUtility.standardVerticalSpacing;
  69. }
  70. return totalHeight;
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement