Advertisement
Guest User

NguiLocalScaleBinding

a guest
May 30th, 2012
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.43 KB | None | 0 0
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5.  
  6. [System.Serializable]
  7. [AddComponentMenu("NGUI/NData/NguiLocalScaleBinding Binding")]
  8. public class NguiLocalScaleBinding : NguiBinding
  9. {
  10.     private EZData.Property<Vector3> _property;
  11.    
  12.     private Vector3 _prevValue = new Vector3();
  13.     private bool _ignoreChanges = false;
  14.    
  15.     private Vector3 _transformReceiver;
  16.            
  17.     public override void Awake()
  18.     {
  19.         base.Awake();
  20.        
  21.         _transformReceiver = gameObject.transform.localScale;
  22.     }
  23.    
  24.     public override void UpdateBinding()
  25.     {
  26.         _property = null;
  27.            
  28.         var context = GetContext();
  29.         if (context == null)
  30.         {
  31.             Debug.LogWarning("NguiLocalScaleBinding.UpdateBinding - context is null");
  32.             return;
  33.         }
  34.        
  35.         _property = context.FindProperty<Vector3>(Path, this);
  36.        
  37.         if (_property != null)
  38.         {
  39.             _property.OnChange += OnChange;
  40.         }
  41.         OnChange();
  42.     }
  43.    
  44.     void Update()
  45.     {
  46.         if (_transformReceiver != null && _property != null)
  47.         {
  48.             if (_prevValue != _transformReceiver)
  49.             {
  50.                 _prevValue = _transformReceiver;
  51.                 _ignoreChanges = true;
  52.                 _property.SetValue(_transformReceiver);
  53.                 _ignoreChanges = false;
  54.             }
  55.         }
  56.     }
  57.    
  58.     public void OnChange()
  59.     {
  60.         if (_ignoreChanges)
  61.             return;
  62.        
  63.         if (_property == null || _transformReceiver == null)
  64.             return;
  65.        
  66.         _transformReceiver = _property.GetValue();
  67.         gameObject.transform.localScale = _transformReceiver;
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement