Created
December 28, 2014 13:09
-
-
Save mandarinx/00e59ad6a7cd3e82cb47 to your computer and use it in GitHub Desktop.
Listen for changes on int, float and bool variables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
namespace MyGame.Core { | |
[System.Serializable] | |
public class Bool : System.Object { | |
public delegate void UpdateDelegate(bool val); | |
public UpdateDelegate OnUpdate; | |
public bool Value; | |
public Bool(bool val) { | |
Value = val; | |
} | |
public static implicit operator bool(Bool val) { | |
return val.Value; | |
} | |
public static implicit operator Bool(bool val) { | |
return new Bool(val); | |
} | |
override public string ToString() { | |
return Value.ToString(); | |
} | |
public static implicit operator string(Bool x) { | |
return x.Value ? "True" : "False"; | |
} | |
public virtual bool Set(bool val) { | |
Value = val; | |
if (OnUpdate != null) { | |
OnUpdate(Value); | |
} | |
return Value; | |
} | |
public static bool operator ==(Bool x, Bool y) { | |
return x.Value == y.Value ? true : false; | |
} | |
public static bool operator !=(Bool x, Bool y) { | |
return x.Value != y.Value ? true : false; | |
} | |
public static Bool operator !(Bool x) { | |
return new Bool(x.Value ? false : true); | |
} | |
public override bool Equals(object o) { | |
try { | |
return (bool) (this == (Bool) o); | |
} catch { | |
return false; | |
} | |
} | |
public override int GetHashCode() { | |
return Value ? 1 : 0; | |
} | |
public void ClearListeners() { | |
if (OnUpdate == null) { | |
return; | |
} | |
foreach (UpdateDelegate d in OnUpdate.GetInvocationList()) { | |
OnUpdate -= (UpdateDelegate)d; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEditor; | |
using MyGame.Core; | |
using System.Collections; | |
using System.Collections.Generic; | |
namespace MyGame { | |
[CustomPropertyDrawer(typeof(Bool))] | |
public class VariablePropertyDrawer : PropertyDrawer { | |
override public void OnGUI(Rect pos, | |
SerializedProperty prop, | |
GUIContent label) { | |
EditorGUI.BeginProperty(pos, label, prop); | |
int ctrlID = GUIUtility.GetControlID(FocusType.Passive); | |
pos = EditorGUI.PrefixLabel(pos, ctrlID, label); | |
int indent = EditorGUI.indentLevel; | |
EditorGUI.indentLevel = 0; | |
Rect r = new Rect(pos.x, pos.y, pos.width, pos.height); | |
EditorGUI.PropertyField(r, | |
prop.FindPropertyRelative("Value"), | |
GUIContent.none); | |
EditorGUI.indentLevel = indent; | |
EditorGUI.EndProperty(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's an example of how to use this setup.
You would setup the player class with a health property like this:
Notice the capital F in Float. This is the wrapper class and NOT the default float value type.
In a separate class you would add bindings between the UI and the player's health property like this:
The UIBindings class hooks up the data source to the UI element by itself. As you can see, the code for updating the player's health and the player class is kept nice and clean with code only relevant for the player's state. UIBindings listens for changes in the Float and keeps the UI updated.
Note that you can force and update of the UI before changes has been made in the player's health by calling HealthUpdate(player.health);