Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- [System.Serializable]
- [AddComponentMenu("NGUI/NData/FillAmout Binding")]
- public class NguiFillAmountBinding : NguiBinding
- {
- private EZData.Property<float> _property;
- private float _prevValue = -1.0f;
- private bool _ignoreChanges = false;
- private UIFilledSprite _UiFilledSpriteReceiver;
- public float Min = 0;
- public float Max = 1;
- private float DataToSlider(float data)
- {
- if (Mathf.Abs(Max - Min) < float.Epsilon)
- return 0.0f;
- return (data - Min) / (Max - Min);
- }
- private float SliderToData(float value)
- {
- if (Mathf.Abs(Max - Min) < float.Epsilon)
- return 0.0f;
- return Min + value * (Max - Min);
- }
- public override void Awake()
- {
- base.Awake();
- _UiFilledSpriteReceiver = gameObject.GetComponent<UIFilledSprite>();
- }
- public override void UpdateBinding()
- {
- _property = null;
- var context = GetContext();
- if (context == null)
- {
- Debug.LogWarning("NuiFillAmountBinding.UpdateBinding - context is null");
- return;
- }
- _property = context.FindProperty<float>(Path, this);
- if (_property != null)
- {
- _property.OnChange += OnChange;
- }
- OnChange();
- }
- void Update()
- {
- if (_UiFilledSpriteReceiver != null && _property != null)
- {
- if (_prevValue != _UiFilledSpriteReceiver.fillAmount)
- {
- _prevValue = _UiFilledSpriteReceiver.fillAmount;
- _ignoreChanges = true;
- _property.SetValue(SliderToData(_UiFilledSpriteReceiver.fillAmount));
- _ignoreChanges = false;
- }
- }
- }
- public void OnChange()
- {
- if (_ignoreChanges)
- return;
- if (_property == null || _UiFilledSpriteReceiver == null)
- return;
- _UiFilledSpriteReceiver.fillAmount = DataToSlider(_property.GetValue());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement