Having trouble finding the right property on a component cos theres just so many identical looking fields? Add some color to you inspectors! mark important property with a bright color so it always stands out, or supply a validation function so highlights show on values that would effect the current ingame logic!
// NOTE DONT put in an editor folder | |
using UnityEngine; | |
public class HighlightAttribute : PropertyAttribute | |
{ | |
public HighlightColor Color; | |
public string ValidateMethod; | |
public object Value; | |
public HighlightAttribute(HighlightColor color = HighlightColor.Yellow, string validateMethod = null, object value = null) | |
{ | |
this.Color = color; | |
this.ValidateMethod = validateMethod; | |
this.Value = value; | |
} | |
} | |
public enum HighlightColor | |
{ | |
Red, | |
Pink, | |
Orange, | |
Yellow, | |
Green, | |
Blue, | |
Violet, | |
White | |
} |
// NOTE put in a Editor folder | |
using UnityEngine; | |
using UnityEditor; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Reflection; | |
[CustomPropertyDrawer(typeof(HighlightAttribute))] | |
public class HighlightDrawer : PropertyDrawer | |
{ | |
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) | |
{ | |
var highlightAttribute = attribute as HighlightAttribute; | |
bool doHighlight = true; | |
// do we have a validation method | |
if (!string.IsNullOrEmpty(highlightAttribute.ValidateMethod)) | |
{ | |
var t = typeof(PlayerController); //property.serializedObject.targetObject.GetType(); | |
var m = t.GetMethod(highlightAttribute.ValidateMethod, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); | |
if (m != null) | |
{ | |
doHighlight = (bool)m.Invoke(property.serializedObject.targetObject, new[] { highlightAttribute.Value}); | |
} | |
else | |
{ | |
Debug.LogError("Invalid Validate function: " + highlightAttribute.ValidateMethod, property.serializedObject.targetObject); | |
} | |
} | |
if (doHighlight) | |
{ | |
// get the highlight color | |
var color = GetColor(highlightAttribute.Color); | |
// create a ractangle to draw the highlight to, slightly larger than our property | |
var padding = EditorGUIUtility.standardVerticalSpacing; | |
var highlightRect = new Rect(position.x - padding, position.y - padding, | |
position.width + (padding * 2), position.height + (padding * 2)); | |
// draw the highlight first | |
EditorGUI.DrawRect(highlightRect, color); | |
// make sure the propertys text is dark and easy to read over the bright highlight | |
var cc = GUI.contentColor; | |
GUI.contentColor = Color.black; | |
// draw the property ontop of the highlight | |
EditorGUI.PropertyField(position, property, label); | |
GUI.contentColor = cc; | |
} | |
else | |
{ | |
EditorGUI.PropertyField(position, property, label); | |
} | |
} | |
public override float GetPropertyHeight(SerializedProperty property, GUIContent label) | |
{ | |
return EditorGUI.GetPropertyHeight(property, label, true); | |
} | |
private Color GetColor(HighlightColor color) | |
{ | |
switch (color) | |
{ | |
case HighlightColor.Red: | |
return new Color32(255, 0, 63, 255); | |
case HighlightColor.Pink: | |
return new Color32(255, 66, 160, 255); | |
case HighlightColor.Orange: | |
return new Color32(255, 128, 0, 255); | |
case HighlightColor.Yellow: | |
return new Color32(255, 211, 0, 255); | |
case HighlightColor.Green: | |
return new Color32(102, 255, 0, 255); | |
case HighlightColor.Blue: | |
return new Color32(0, 135, 189, 255); | |
case HighlightColor.Violet: | |
return new Color32(127, 0, 255, 255); | |
default: | |
return Color.white; | |
} | |
} | |
} |
// Demo | |
using UnityEngine; | |
using System.Collections; | |
public class PlayerController : MonoBehaviour | |
{ | |
//[Highlight()] // add a yellow highlight | |
//[Highlight(HighlightColor.Green)] // add highlight with a colour you supply | |
//[Highlight(HighlightColor.Green, "ValidateHighlight")] // highlight with a validate function. pass in a string method name, the method must return a bool | |
//[Highlight(HighlightColor.Green, "ValidateHighlight", value)] // highlight with a validate function. pass in a string method name and one argument | |
[Highlight(HighlightColor.Green, "ValidateHighlight", PlayerState.Moving)] | |
public float moveSpeed; | |
[Highlight(HighlightColor.Red, "ValidateHighlight", PlayerState.Stopping)] | |
public float breakSpeed; | |
[Highlight(HighlightColor.Yellow, "ValidateHighlight", PlayerState.Jumping)] | |
public float jumpForce; | |
[ReadOnly] // see https://gist.github.com/LotteMakesStuff/c0a3b404524be57574ffa5f8270268ea for implementation | |
public PlayerState state; | |
bool ValidateHighlight(PlayerState checkState)//(PlayerState state) | |
{ | |
return state == checkState; | |
} | |
float timer = 0; | |
// Update is called once per frame | |
void Update () | |
{ | |
timer += Time.deltaTime; | |
state = PlayerState.Idle; | |
if (timer >= 1) | |
state = PlayerState.Moving; | |
if (timer >= 2) | |
state = PlayerState.Stopping; | |
if (timer >= 3) | |
state = PlayerState.Jumping; | |
if (timer >= 4) | |
timer = 0; | |
} | |
} | |
public enum PlayerState | |
{ | |
Idle, | |
Moving, | |
Stopping, | |
Jumping | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment