Advertisement
Guest User

NguiFillAmountBinding

a guest
May 30th, 2012
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.82 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/FillAmout Binding")]
  8. public class NguiFillAmountBinding : NguiBinding
  9. {
  10.     private EZData.Property<float> _property;
  11.    
  12.     private float _prevValue = -1.0f;
  13.     private bool _ignoreChanges = false;
  14.    
  15.     private UIFilledSprite _UiFilledSpriteReceiver;
  16.    
  17.     public float Min = 0;
  18.     public float Max = 1;
  19.    
  20.     private float DataToSlider(float data)
  21.     {
  22.         if (Mathf.Abs(Max - Min) < float.Epsilon)
  23.             return 0.0f;
  24.        
  25.         return (data - Min) / (Max - Min);
  26.     }
  27.    
  28.     private float SliderToData(float value)
  29.     {
  30.         if (Mathf.Abs(Max - Min) < float.Epsilon)
  31.             return 0.0f;
  32.        
  33.         return Min + value * (Max - Min);
  34.     }
  35.    
  36.     public override void Awake()
  37.     {
  38.         base.Awake();
  39.        
  40.         _UiFilledSpriteReceiver = gameObject.GetComponent<UIFilledSprite>();
  41.     }
  42.    
  43.     public override void UpdateBinding()
  44.     {
  45.         _property = null;
  46.            
  47.         var context = GetContext();
  48.         if (context == null)
  49.         {
  50.             Debug.LogWarning("NuiFillAmountBinding.UpdateBinding - context is null");
  51.             return;
  52.         }
  53.        
  54.         _property = context.FindProperty<float>(Path, this);
  55.        
  56.         if (_property != null)
  57.         {
  58.             _property.OnChange += OnChange;
  59.         }
  60.         OnChange();
  61.     }
  62.    
  63.     void Update()
  64.     {
  65.         if (_UiFilledSpriteReceiver != null && _property != null)
  66.         {
  67.             if (_prevValue != _UiFilledSpriteReceiver.fillAmount)
  68.             {
  69.                 _prevValue = _UiFilledSpriteReceiver.fillAmount;
  70.                 _ignoreChanges = true;
  71.                 _property.SetValue(SliderToData(_UiFilledSpriteReceiver.fillAmount));
  72.                 _ignoreChanges = false;
  73.             }
  74.         }
  75.     }
  76.    
  77.     public void OnChange()
  78.     {
  79.         if (_ignoreChanges)
  80.             return;
  81.        
  82.         if (_property == null || _UiFilledSpriteReceiver == null)
  83.             return;
  84.        
  85.         _UiFilledSpriteReceiver.fillAmount = DataToSlider(_property.GetValue());
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement