AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
GradientDefaultAttribute.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using System;
5 using System.Reflection;
6 using UnityEngine;
7 #if UNITY_EDITOR
8 using UnityEditor;
9 #endif
10 
11 namespace HoloToolkit.Unity
12 {
13  // Adds a 'default' button to a color gradient that will supply default color values
14  [AttributeUsage(AttributeTargets.Field)]
16  {
17  // Used because you can't pass colors as attribute vars :/
18  public enum ColorEnum
19  {
20  Black,
21  Blue,
22  Clear,
23  Cyan,
24  Gray,
25  Green,
26  Magenta,
27  Red,
28  White,
29  Yellow
30  }
31 
32  public GradientDefaultAttribute(ColorEnum startColor, ColorEnum endColor, float startAlpha = 1f, float endAlpha = 1f)
33  {
34  this.startColor = GetColor(startColor);
35  this.endColor = GetColor(endColor);
36  this.startColor.a = startAlpha;
37  this.endColor.a = endAlpha;
38  }
39 
40 #if UNITY_EDITOR
41  public override void DrawEditor(UnityEngine.Object target, FieldInfo field, SerializedProperty property)
42  {
43  Gradient gradientValue = field.GetValue(target) as Gradient;
44 
45  if (gradientValue == null || gradientValue.colorKeys == null || gradientValue.colorKeys.Length == 0)
46  gradientValue = GetDefault();
47 
48  EditorGUILayout.BeginHorizontal();
49  EditorGUILayout.PropertyField(property);
50  if (GUILayout.Button("Default"))
51  {
52  gradientValue = GetDefault();
53  }
54  EditorGUILayout.EndHorizontal();
55 
56  field.SetValue(target, gradientValue);
57  }
58 
59  public override void DrawEditor(UnityEngine.Object target, PropertyInfo prop)
60  {
61  throw new NotImplementedException();
62  }
63 #endif
64 
65  private Gradient GetDefault()
66  {
67  Gradient gradient = new Gradient();
68  GradientColorKey[] colorKeys = new GradientColorKey[2] {
69  new GradientColorKey(startColor, 0f),
70  new GradientColorKey(endColor, 1f)
71  };
72  GradientAlphaKey[] alphaKeys = new GradientAlphaKey[2]
73  {
74  new GradientAlphaKey(startColor.a, 0f),
75  new GradientAlphaKey(endColor.a, 0f),
76  };
77  gradient.SetKeys(colorKeys, alphaKeys);
78  return gradient;
79  }
80 
81  private Color startColor;
82  private Color endColor;
83 
84  private static Color GetColor(ColorEnum color)
85  {
86  switch (color)
87  {
88  case ColorEnum.Black:
89  return Color.black;
90  case ColorEnum.Blue:
91  return Color.blue;
92  case ColorEnum.Clear:
93  default:
94  return Color.clear;
95  case ColorEnum.Cyan:
96  return Color.cyan;
97  case ColorEnum.Gray:
98  return Color.gray;
99  case ColorEnum.Green:
100  return Color.green;
101  case ColorEnum.Magenta:
102  return Color.magenta;
103  case ColorEnum.Red:
104  return Color.red;
105  case ColorEnum.White:
106  return Color.white;
107  case ColorEnum.Yellow:
108  return Color.yellow;
109  }
110  }
111  }
112 }
GradientDefaultAttribute(ColorEnum startColor, ColorEnum endColor, float startAlpha=1f, float endAlpha=1f)