为 ngui TweenPosition 添加 pingpongone
- //----------------------------------------------
- // NGUI: Next-Gen UI kit
- // Copyright © 2011-2015 Tasharen Entertainment
- //----------------------------------------------
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- /// <summary>
- /// Base class for all tweening operations.
- /// </summary>
- public abstract class UITweener : MonoBehaviour
- {
- /// <summary>
- /// Current tween that triggered the callback function.
- /// </summary>
- static public UITweener current;
- public enum Method
- {
- Linear,
- EaseIn,
- EaseOut,
- EaseInOut,
- BounceIn,
- BounceOut,
- }
- public enum Style
- {
- Once,
- Loop,
- PingPong,
- PingPongOne // by tin
- }
- /// <summary>
- /// Tweening method used.
- /// </summary>
- [HideInInspector]
- public Method method = Method.Linear;
- /// <summary>
- /// Does it play once? Does it loop?
- /// </summary>
- [HideInInspector]
- public Style style = Style.Once;
- /// <summary>
- /// Optional curve to apply to the tween's time factor value.
- /// </summary>
- [HideInInspector]
- public AnimationCurve animationCurve = new AnimationCurve(new Keyframe(0f, 0f, 0f, 1f), new Keyframe(1f, 1f, 1f, 0f));
- /// <summary>
- /// Whether the tween will ignore the timescale, making it work while the game is paused.
- /// </summary>
- [HideInInspector]
- public bool ignoreTimeScale = true;
- /// <summary>
- /// How long will the tweener wait before starting the tween?
- /// </summary>
- [HideInInspector]
- public float delay = 0f;
- /// <summary>
- /// How long is the duration of the tween?
- /// </summary>
- [HideInInspector]
- public float duration = 1f;
- /// <summary>
- /// Whether the tweener will use steeper curves for ease in / out style interpolation.
- /// </summary>
- [HideInInspector]
- public bool steeperCurves = false;
- /// <summary>
- /// Used by buttons and tween sequences. Group of '0' means not in a sequence.
- /// </summary>
- [HideInInspector]
- public int tweenGroup = 0;
- /// <summary>
- /// Event delegates called when the animation finishes.
- /// </summary>
- [HideInInspector]
- public List<EventDelegate> onFinished = new List<EventDelegate>();
- // Deprecated functionality, kept for backwards compatibility
- [HideInInspector] public GameObject eventReceiver;
- [HideInInspector] public string callWhenFinished;
- bool mStarted = false;
- float mStartTime = 0f;
- float mDuration = 0f;
- float mAmountPerDelta = 1000f;
- float mFactor = 0f;
- /// <summary>
- /// Amount advanced per delta time.
- /// </summary>
- public float amountPerDelta
- {
- get
- {
- if (mDuration != duration)
- {
- mDuration = duration;
- mAmountPerDelta = Mathf.Abs((duration > 0f) ? 1f / duration : 1000f) * Mathf.Sign(mAmountPerDelta);
- }
- return mAmountPerDelta;
- }
- }
- /// <summary>
- /// Tween factor, 0-1 range.
- /// </summary>
- public float tweenFactor { get { return mFactor; } set { mFactor = Mathf.Clamp01(value); } }
- /// <summary>
- /// Direction that the tween is currently playing in.
- /// </summary>
- public AnimationOrTween.Direction direction { get { return amountPerDelta < 0f ? AnimationOrTween.Direction.Reverse : AnimationOrTween.Direction.Forward; } }
- /// <summary>
- /// This function is called by Unity when you add a component. Automatically set the starting values for convenience.
- /// </summary>
- void Reset ()
- {
- if (!mStarted)
- {
- SetStartToCurrentValue();
- SetEndToCurrentValue();
- }
- }
- /// <summary>
- /// Update as soon as it's started so that there is no delay.
- /// </summary>
- protected virtual void Start () { Update(); }
- /// <summary>
- /// Update the tweening factor and call the virtual update function.
- /// </summary>
- void Update ()
- {
- float delta = ignoreTimeScale ? RealTime.deltaTime : Time.deltaTime;
- float time = ignoreTimeScale ? RealTime.time : Time.time;
- if (!mStarted)
- {
- mStarted = true;
- mStartTime = time + delay;
- }
- if (time < mStartTime) return;
- // Advance the sampling factor
- mFactor += amountPerDelta * delta;
- // Loop style simply resets the play factor after it exceeds 1.
- if (style == Style.Loop)
- {
- if (mFactor > 1f)
- {
- mFactor -= Mathf.Floor(mFactor);
- }
- }
- else if (style == Style.PingPong)
- {
- // Ping-pong style reverses the direction
- if (mFactor > 1f)
- {
- mFactor = 1f - (mFactor - Mathf.Floor(mFactor));
- mAmountPerDelta = -mAmountPerDelta;
- }
- else if (mFactor < 0f)
- {
- mFactor = -mFactor;
- mFactor -= Mathf.Floor(mFactor);
- mAmountPerDelta = -mAmountPerDelta;
- }
- }else if (style == Style.PingPongOne) // by tin
- {
- // Ping-pong style reverses the direction
- if (mFactor > 1f)
- {
- mFactor = 1f - (mFactor - Mathf.Floor(mFactor));
- mAmountPerDelta = -mAmountPerDelta;
- }
- else if (mFactor < 0f)
- {
- mFactor = -mFactor;
- mFactor -= Mathf.Floor(mFactor);
- mAmountPerDelta = -mAmountPerDelta;
- enabled = false;
- if (onFinished != null)
- {
- mTemp = onFinished;
- onFinished = new List<EventDelegate>();
- // Notify the listener delegates
- EventDelegate.Execute(mTemp);
- // Re-add the previous persistent delegates
- for (int i = 0; i < mTemp.Count; ++i)
- {
- EventDelegate ed = mTemp[i];
- if (ed != null && !ed.oneShot) EventDelegate.Add(onFinished, ed, ed.oneShot);
- }
- mTemp = null;
- }
- }
- }
- // If the factor goes out of range and this is a one-time tweening operation, disable the script
- if ((style == Style.Once) && (duration == 0f || mFactor > 1f || mFactor < 0f))
- {
- mFactor = Mathf.Clamp01(mFactor);
- Sample(mFactor, true);
- enabled = false;
- if (current == null)
- {
- UITweener before = current;
- current = this;
- if (onFinished != null)
- {
- mTemp = onFinished;
- onFinished = new List<EventDelegate>();
- // Notify the listener delegates
- EventDelegate.Execute(mTemp);
- // Re-add the previous persistent delegates
- for (int i = 0; i < mTemp.Count; ++i)
- {
- EventDelegate ed = mTemp[i];
- if (ed != null && !ed.oneShot) EventDelegate.Add(onFinished, ed, ed.oneShot);
- }
- mTemp = null;
- }
- // Deprecated legacy functionality support
- if (eventReceiver != null && !string.IsNullOrEmpty(callWhenFinished))
- eventReceiver.SendMessage(callWhenFinished, this, SendMessageOptions.DontRequireReceiver);
- current = before;
- }
- }
- else Sample(mFactor, false);
- }
- List<EventDelegate> mTemp = null;
- /// <summary>
- /// Convenience function -- set a new OnFinished event delegate (here for to be consistent with RemoveOnFinished).
- /// </summary>
- public void SetOnFinished (EventDelegate.Callback del) { EventDelegate.Set(onFinished, del); }
- /// <summary>
- /// Convenience function -- set a new OnFinished event delegate (here for to be consistent with RemoveOnFinished).
- /// </summary>
- public void SetOnFinished (EventDelegate del) { EventDelegate.Set(onFinished, del); }
- /// <summary>
- /// Convenience function -- add a new OnFinished event delegate (here for to be consistent with RemoveOnFinished).
- /// </summary>
- public void AddOnFinished (EventDelegate.Callback del) { EventDelegate.Add(onFinished, del); }
- /// <summary>
- /// Convenience function -- add a new OnFinished event delegate (here for to be consistent with RemoveOnFinished).
- /// </summary>
- public void AddOnFinished (EventDelegate del) { EventDelegate.Add(onFinished, del); }
- /// <summary>
- /// Remove an OnFinished delegate. Will work even while iterating through the list when the tweener has finished its operation.
- /// </summary>
- public void RemoveOnFinished (EventDelegate del)
- {
- if (onFinished != null) onFinished.Remove(del);
- if (mTemp != null) mTemp.Remove(del);
- }
- /// <summary>
- /// Mark as not started when finished to enable delay on next play.
- /// </summary>
- void OnDisable () { mStarted = false; }
- /// <summary>
- /// Sample the tween at the specified factor.
- /// </summary>
- public void Sample (float factor, bool isFinished)
- {
- // Calculate the sampling value
- float val = Mathf.Clamp01(factor);
- if (method == Method.EaseIn)
- {
- val = 1f - Mathf.Sin(0.5f * Mathf.PI * (1f - val));
- if (steeperCurves) val *= val;
- }
- else if (method == Method.EaseOut)
- {
- val = Mathf.Sin(0.5f * Mathf.PI * val);
- if (steeperCurves)
- {
- val = 1f - val;
- val = 1f - val * val;
- }
- }
- else if (method == Method.EaseInOut)
- {
- const float pi2 = Mathf.PI * 2f;
- val = val - Mathf.Sin(val * pi2) / pi2;
- if (steeperCurves)
- {
- val = val * 2f - 1f;
- float sign = Mathf.Sign(val);
- val = 1f - Mathf.Abs(val);
- val = 1f - val * val;
- val = sign * val * 0.5f + 0.5f;
- }
- }
- else if (method == Method.BounceIn)
- {
- val = BounceLogic(val);
- }
- else if (method == Method.BounceOut)
- {
- val = 1f - BounceLogic(1f - val);
- }
- // Call the virtual update
- OnUpdate((animationCurve != null) ? animationCurve.Evaluate(val) : val, isFinished);
- }
- /// <summary>
- /// Main Bounce logic to simplify the Sample function
- /// </summary>
- float BounceLogic (float val)
- {
- if (val < 0.363636f) // 0.363636 = (1/ 2.75)
- {
- val = 7.5685f * val * val;
- }
- else if (val < 0.727272f) // 0.727272 = (2 / 2.75)
- {
- val = 7.5625f * (val -= 0.545454f) * val + 0.75f; // 0.545454f = (1.5 / 2.75)
- }
- else if (val < 0.909090f) // 0.909090 = (2.5 / 2.75)
- {
- val = 7.5625f * (val -= 0.818181f) * val + 0.9375f; // 0.818181 = (2.25 / 2.75)
- }
- else
- {
- val = 7.5625f * (val -= 0.9545454f) * val + 0.984375f; // 0.9545454 = (2.625 / 2.75)
- }
- return val;
- }
- /// <summary>
- /// Play the tween.
- /// </summary>
- [System.Obsolete("Use PlayForward() instead")]
- public void Play () { Play(true); }
- /// <summary>
- /// Play the tween forward.
- /// </summary>
- public void PlayForward () { Play(true); }
- /// <summary>
- /// Play the tween in reverse.
- /// </summary>
- public void PlayReverse () { Play(false); }
- /// <summary>
- /// Manually activate the tweening process, reversing it if necessary.
- /// </summary>
- public void Play (bool forward)
- {
- mAmountPerDelta = Mathf.Abs(amountPerDelta);
- if (!forward) mAmountPerDelta = -mAmountPerDelta;
- enabled = true;
- Update();
- }
- /// <summary>
- /// Manually reset the tweener's state to the beginning.
- /// If the tween is playing forward, this means the tween's start.
- /// If the tween is playing in reverse, this means the tween's end.
- /// </summary>
- public void ResetToBeginning ()
- {
- mStarted = false;
- mFactor = (amountPerDelta < 0f) ? 1f : 0f;
- Sample(mFactor, false);
- }
- /// <summary>
- /// Manually start the tweening process, reversing its direction.
- /// </summary>
- public void Toggle ()
- {
- if (mFactor > 0f)
- {
- mAmountPerDelta = -amountPerDelta;
- }
- else
- {
- mAmountPerDelta = Mathf.Abs(amountPerDelta);
- }
- enabled = true;
- }
- /// <summary>
- /// Actual tweening logic should go here.
- /// </summary>
- abstract protected void OnUpdate (float factor, bool isFinished);
- /// <summary>
- /// Starts the tweening operation.
- /// </summary>
- static public T Begin<T> (GameObject go, float duration) where T : UITweener
- {
- T comp = go.GetComponent<T>();
- #if UNITY_FLASH
- if ((object)comp == null) comp = (T)go.AddComponent<T>();
- #else
- // Find the tween with an unset group ID (group ID of 0).
- if (comp != null && comp.tweenGroup != 0)
- {
- comp = null;
- T[] comps = go.GetComponents<T>();
- for (int i = 0, imax = comps.Length; i < imax; ++i)
- {
- comp = comps[i];
- if (comp != null && comp.tweenGroup == 0) break;
- comp = null;
- }
- }
- if (comp == null)
- {
- comp = go.AddComponent<T>();
- if (comp == null)
- {
- Debug.LogError("Unable to add " + typeof(T) + " to " + NGUITools.GetHierarchy(go), go);
- return null;
- }
- }
- #endif
- comp.mStarted = false;
- comp.duration = duration;
- comp.mFactor = 0f;
- comp.mAmountPerDelta = Mathf.Abs(comp.amountPerDelta);
- comp.style = Style.Once;
- comp.animationCurve = new AnimationCurve(new Keyframe(0f, 0f, 0f, 1f), new Keyframe(1f, 1f, 1f, 0f));
- comp.eventReceiver = null;
- comp.callWhenFinished = null;
- comp.enabled = true;
- return comp;
- }
- /// <summary>
- /// Set the 'from' value to the current one.
- /// </summary>
- public virtual void SetStartToCurrentValue () { }
- /// <summary>
- /// Set the 'to' value to the current one.
- /// </summary>
- public virtual void SetEndToCurrentValue () { }
- }
为 ngui TweenPosition 添加 pingpongone的更多相关文章
- NGUI 动态添加控件
本文链接地址: Unity3D NGUI动态创建按钮 本例仅以熟悉NGUI组件功能为目的,想快捷简便的创建按钮或其它游戏物体请参考 “Unity3D 动态实例化Prefab” 以动态创建服务器列表为例 ...
- NGUI之添加响应函数
public void ButtonNextClicked() { SelectIndex++; SelectIndex %= ; showGameObject(); } public void Bu ...
- Unity NGUI插件
前言:关于Unity中关于UI的插件,我们最开始学的是UGUI,是Unity自带的UI系统,而在Unity版本还没更新出UGUI之前,除了NGUI没有一个更好些的插件,所以人们不得不去选择NGUI去制 ...
- NGUI 层级关系控制
NGUI元素的遮挡情况是不依赖空间关系,所以在NGUI上添加特效有时候特别蛋疼,特别是美术同学还要依赖空间关系来控制特效效果,那先看看看NGUI的层级是怎么处理的,不过下面的描述都是针对单个相机下的P ...
- unity3D游戏开发十八之NGUI动画
我们先来看下帧动画,顾名思义,就是一帧帧的图片组成的动画,我们须要用到UISprite Animation组件,它的属性例如以下: Framerate:播放速率,也就是每秒钟播放的帧数 Name Pr ...
- NGUI插件的一个扩展---NGUI_HUD_Text
NGUI_HUD_Text扩展主要用于主角跟随和伤害/治疗的功能. 场景大概是这样的,我们希望有一个主角,在其头顶显示他的名字,在单击鼠标左键的时候显示红色的“-10”表示减少血量,单击鼠标右键的时候 ...
- 混用ngui和ugui渲染顺序问题
http://blog.csdn.net/xtxy/article/details/38332801 为NGUI panel 添加 sorting layer 接着上一篇文章的问题,看到了老外做的一个 ...
- Unity初探之黑暗之光(1)
Unity初探之黑暗之光(1) 1.镜头拉近 public float speed=10f;//镜头的移动速度 ;//镜头的结束位置 // Update is called once per fram ...
- Unity 黑暗之光 笔记 第一章
第一章 设计游戏开始进入场景 1.设置相机视野同步 选中要调整的相机 GameObject - Align With View(快捷键 Ctrl + Shift + F)
随机推荐
- ASP.NET Web API消息处理管道:Self Host下的消息处理管道[下篇]
ASP.NET Web API消息处理管道:Self Host下的消息处理管道[下篇] 我们知道ASP.NET Web API借助于HttpSelfHostServer以Self Host模式寄宿于当 ...
- WPF刷新界面之坎坷路
WPF刷新界面之坎坷路 项目需要一个硬件检测功能,需要用到界面刷新,刚开始想用个定时器,对检测过的硬设定时添加后刷新界面. 但是很遗憾,定时器并不能进行刷新.后台检测List数据里面已经添加了很多了很 ...
- Python学习入门基础教程(learning Python)--6.3 Python的list切片高级
上节"6.2 Python的list访问索引和切片"主要学习了Python下的List的访问技术:索引和切片的基础知识,这节将就List的索引index和切片Slice知识点做进一 ...
- data矩阵poj 2778 DNA Sequence
最近研究data矩阵,稍微总结一下,以后继续补充: ac自动机处理字符串,dp计算谜底,用矩阵来减速 每日一道理 巴尔扎克说过“不幸,是天才的进升阶梯,信徒的洗礼之水,弱者的无底深渊”.风雨过 ...
- C语言之if和switch的分别
If和switch的互换规则 用if语句能实现的东西,用switch也可以实现 用switch语句能实现的东西,用if也可以实现 1.如果是判断范围的时候,用switch不太好做,没法直接做 2.如果 ...
- 微信小程序开发系列(一)小程序开发初体验
开发小程序所需的基本技能 关于小程序的介绍和使用场景这里不作介绍,这个系列的文章会一步一步地带领大家快速地学习和掌握小程序的开发. 关于还没有接触过小程序的开发者来说,最关心的问题无非就是,开发小 ...
- ruby web性能响应时间
可以统计单个web页面加载时间. require 'watir-webdriver' require 'watir-webdriver-performance' b = Watir::Browser. ...
- AutoLayout 之NSLayoutConstraint
这次主要讲的用代码来设置AutoLayout,为实现添加autoLayout视图主要介绍使用如下该方法,调用方法:- (void)awakeFromNib {} +(instancetype)cons ...
- 修改/etc/resolv.conf又恢复到原来的状态?[转]
新装一台机器环境为服务器主板,双网卡,系统为CentOS5.4 ,eth0为内网ip,eth1为公网ip.但是由于在本地测试,设置的内网ip,域名服务器同样使用的是上海本地的域名解析,没有问题,可以上 ...
- Nuget 学习三
后期管理: 登录 nuget 官网 https://www.nuget.org/ 可以搜索到自己的包: 点击进入,可进一步操作 如果你需要给自己的类型新增其他功能,或者修改之前的bug(反正就是修改代 ...