Advertisement
angrytroglodyte

Untitled

Nov 27th, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.06 KB | None | 0 0
  1. TestCase.cs
  2. --------
  3. using UnityEngine;
  4. using System.Collections;
  5. using System.ComponentModel;
  6.  
  7. public class TestCase : MonoBehaviour
  8. {
  9.         public Observable<Player> LocalPlayer = new Observable<Player> (new Player ());
  10.        
  11.         public Observable<int> TestInt = new Observable<int> (100);
  12.  
  13.         void Update ()
  14.         {
  15.                 TestInt.Value++;
  16.                 LocalPlayer.Value.Coins.Value++;
  17.         }
  18. }
  19.  
  20. [System.Serializable]
  21. public class Player
  22. {
  23.         public Observable<PlayerFriend> BestFriend = new Observable<PlayerFriend> (new PlayerFriend ());
  24.         public Observable<int> Coins = new Observable<int> (100);
  25. }
  26.  
  27. [System.Serializable]
  28. public class PlayerStats
  29. {
  30.         public Observable<int> Score;
  31. }
  32.  
  33. [System.Serializable]
  34. public class PlayerFriend
  35. {
  36.         public Observable<string> name;
  37.         public string id;
  38. }
  39.  
  40. //BIND LABEL
  41. //Set Propname to something, a la  TestCase.LocalPlayer.BestFriend.Score;
  42. //----------
  43. using UnityEngine;
  44. using System;
  45. using System.Collections;
  46. using System.Reflection;
  47. using UnityEngine.UI;
  48.  
  49. public class BindLabel : MonoBehaviour
  50. {
  51.     public Text bindToLabel;
  52.     public string format = "{0}";
  53.  
  54.     public MonoBehaviour bindObject;
  55.     public string propPath;
  56.  
  57.     void Start ()
  58.     {
  59.         var obs = LocateObservableForPropertyPath<int> (bindObject, propPath);
  60.  
  61.         obs.OnChanged.Add ((newVal) => {
  62.             bindToLabel.text = string.Format (format, newVal.ToString ());
  63.         });
  64.     }
  65.  
  66.     Observable<T> LocateObservableForPropertyPath<T> (object obj, string propPath)
  67.     {
  68.         System.Diagnostics.Debug.Assert (!string.IsNullOrEmpty (propPath));
  69.  
  70.         var propertyNames = propPath.Split ('.');
  71.  
  72.         var objType = obj.GetType ();
  73.         Debug.Log (objType);
  74.  
  75.         FieldInfo curPropInfo = obj.GetType ().GetField (propertyNames [0]);
  76.  
  77.         for (int i = 1; i < propertyNames.Length; i++) {
  78.  
  79.             if (curPropInfo.FieldType.IsGenericType) {
  80.                 var isObservable = curPropInfo.FieldType.GetGenericTypeDefinition () == typeof(Observable<>);
  81.                 if (isObservable) {
  82.                     //it's an observable, add an event handler so we can register vlaue changes up the chain...
  83.                     var obs = curPropInfo.GetValue (obj);
  84.                     var methodInfo = obs.GetType ().GetMethod ("AddOnChangedHandler");
  85.                     var actType = typeof(System.Action<>);
  86.                     var typeArgs = obs.GetType ().GetGenericArguments ();
  87.                     var formedAction = actType.MakeGenericType (typeArgs);
  88.                    
  89.                     var o = Activator.CreateInstance (formedAction);
  90.                        
  91.                     methodInfo.Invoke (obs, new object[] { OnObservableChanged });  //This will notify us of crap
  92.  
  93.                     var obsValueProperty = obs.GetType ().GetProperty ("Value");
  94.                     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
  95.                     obj = val;
  96.                     curPropInfo = val.GetType ().GetField (propertyNames [i]);
  97.  
  98.                     Debug.Log ("Obs value: " + val);
  99.                 }
  100.             } else {
  101.                 obj = curPropInfo.GetValue (obj);
  102.                 curPropInfo = curPropInfo.FieldType.GetField (propertyNames [i]);
  103.             }
  104.         }
  105.  
  106.         var retObs = curPropInfo.GetValue (obj);
  107.  
  108.         return retObs as Observable<T>;
  109.     }
  110.  
  111.     void OnObservableChanged ()
  112.     {
  113.  
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement