PropertyBinding.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using UnityEngine;
  3. [ExecuteInEditMode]
  4. [AddComponentMenu("NGUI/Internal/Property Binding")]
  5. public class PropertyBinding : MonoBehaviour
  6. {
  7. private void Start()
  8. {
  9. this.UpdateTarget();
  10. if (this.update == PropertyBinding.UpdateCondition.OnStart)
  11. {
  12. base.enabled = false;
  13. }
  14. }
  15. private void Update()
  16. {
  17. if (this.update == PropertyBinding.UpdateCondition.OnUpdate)
  18. {
  19. this.UpdateTarget();
  20. }
  21. }
  22. private void LateUpdate()
  23. {
  24. if (this.update == PropertyBinding.UpdateCondition.OnLateUpdate)
  25. {
  26. this.UpdateTarget();
  27. }
  28. }
  29. private void FixedUpdate()
  30. {
  31. if (this.update == PropertyBinding.UpdateCondition.OnFixedUpdate)
  32. {
  33. this.UpdateTarget();
  34. }
  35. }
  36. private void OnValidate()
  37. {
  38. if (this.source != null)
  39. {
  40. this.source.Reset();
  41. }
  42. if (this.target != null)
  43. {
  44. this.target.Reset();
  45. }
  46. }
  47. [ContextMenu("Update Now")]
  48. public void UpdateTarget()
  49. {
  50. if (this.source != null && this.target != null && this.source.isValid && this.target.isValid)
  51. {
  52. if (this.direction == PropertyBinding.Direction.SourceUpdatesTarget)
  53. {
  54. this.target.Set(this.source.Get());
  55. }
  56. else if (this.direction == PropertyBinding.Direction.TargetUpdatesSource)
  57. {
  58. this.source.Set(this.target.Get());
  59. }
  60. else if (this.source.GetPropertyType() == this.target.GetPropertyType())
  61. {
  62. object obj = this.source.Get();
  63. if (this.mLastValue == null || !this.mLastValue.Equals(obj))
  64. {
  65. this.mLastValue = obj;
  66. this.target.Set(obj);
  67. }
  68. else
  69. {
  70. obj = this.target.Get();
  71. if (!this.mLastValue.Equals(obj))
  72. {
  73. this.mLastValue = obj;
  74. this.source.Set(obj);
  75. }
  76. }
  77. }
  78. }
  79. }
  80. public PropertyReference source;
  81. public PropertyReference target;
  82. public PropertyBinding.Direction direction;
  83. public PropertyBinding.UpdateCondition update = PropertyBinding.UpdateCondition.OnUpdate;
  84. public bool editMode = true;
  85. private object mLastValue;
  86. public enum UpdateCondition
  87. {
  88. OnStart,
  89. OnUpdate,
  90. OnLateUpdate,
  91. OnFixedUpdate
  92. }
  93. public enum Direction
  94. {
  95. SourceUpdatesTarget,
  96. TargetUpdatesSource,
  97. BiDirectional
  98. }
  99. }