Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- TestCase.cs
- --------
- using UnityEngine;
- using System.Collections;
- using System.ComponentModel;
- public class TestCase : MonoBehaviour
- {
- public Observable<Player> LocalPlayer = new Observable<Player> (new Player ());
- public Observable<int> TestInt = new Observable<int> (100);
- void Update ()
- {
- TestInt.Value++;
- LocalPlayer.Value.Coins.Value++;
- }
- }
- [System.Serializable]
- public class Player
- {
- public Observable<PlayerFriend> BestFriend = new Observable<PlayerFriend> (new PlayerFriend ());
- public Observable<int> Coins = new Observable<int> (100);
- }
- [System.Serializable]
- public class PlayerStats
- {
- public Observable<int> Score;
- }
- [System.Serializable]
- public class PlayerFriend
- {
- public Observable<string> name;
- public string id;
- }
- //BIND LABEL
- //Set Propname to something, a la TestCase.LocalPlayer.BestFriend.Score;
- //----------
- using UnityEngine;
- using System;
- using System.Collections;
- using System.Reflection;
- using UnityEngine.UI;
- public class BindLabel : MonoBehaviour
- {
- public Text bindToLabel;
- public string format = "{0}";
- public MonoBehaviour bindObject;
- public string propPath;
- void Start ()
- {
- var obs = LocateObservableForPropertyPath<int> (bindObject, propPath);
- obs.OnChanged.Add ((newVal) => {
- bindToLabel.text = string.Format (format, newVal.ToString ());
- });
- }
- Observable<T> LocateObservableForPropertyPath<T> (object obj, string propPath)
- {
- System.Diagnostics.Debug.Assert (!string.IsNullOrEmpty (propPath));
- var propertyNames = propPath.Split ('.');
- var objType = obj.GetType ();
- Debug.Log (objType);
- FieldInfo curPropInfo = obj.GetType ().GetField (propertyNames [0]);
- for (int i = 1; i < propertyNames.Length; i++) {
- if (curPropInfo.FieldType.IsGenericType) {
- var isObservable = curPropInfo.FieldType.GetGenericTypeDefinition () == typeof(Observable<>);
- if (isObservable) {
- //it's an observable, add an event handler so we can register vlaue changes up the chain...
- var obs = curPropInfo.GetValue (obj);
- var methodInfo = obs.GetType ().GetMethod ("AddOnChangedHandler");
- var actType = typeof(System.Action<>);
- var typeArgs = obs.GetType ().GetGenericArguments ();
- var formedAction = actType.MakeGenericType (typeArgs);
- var o = Activator.CreateInstance (formedAction);
- methodInfo.Invoke (obs, new object[] { OnObservableChanged }); //This will notify us of crap
- var obsValueProperty = obs.GetType ().GetProperty ("Value");
- var val = obsValueProperty.GetValue (obs, null); //find the value of this observable we're observing - this is the object we'll reflect off of next
- obj = val;
- curPropInfo = val.GetType ().GetField (propertyNames [i]);
- Debug.Log ("Obs value: " + val);
- }
- } else {
- obj = curPropInfo.GetValue (obj);
- curPropInfo = curPropInfo.FieldType.GetField (propertyNames [i]);
- }
- }
- var retObs = curPropInfo.GetValue (obj);
- return retObs as Observable<T>;
- }
- void OnObservableChanged ()
- {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement