1. //----------------------------------------------
  2. // NGUI: Next-Gen UI kit
  3. // Copyright © 2011-2015 Tasharen Entertainment
  4. //----------------------------------------------
  5.  
  6. using UnityEngine;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9.  
  10. /// <summary>
  11. /// Base class for all tweening operations.
  12. /// </summary>
  13.  
  14. public abstract class UITweener : MonoBehaviour
  15. {
  16. /// <summary>
  17. /// Current tween that triggered the callback function.
  18. /// </summary>
  19.  
  20. static public UITweener current;
  21.  
  22. public enum Method
  23. {
  24. Linear,
  25. EaseIn,
  26. EaseOut,
  27. EaseInOut,
  28. BounceIn,
  29. BounceOut,
  30. }
  31.  
  32. public enum Style
  33. {
  34. Once,
  35. Loop,
  36. PingPong,
  37. PingPongOne // by tin
  38. }
  39.  
  40. /// <summary>
  41. /// Tweening method used.
  42. /// </summary>
  43.  
  44. [HideInInspector]
  45. public Method method = Method.Linear;
  46.  
  47. /// <summary>
  48. /// Does it play once? Does it loop?
  49. /// </summary>
  50.  
  51. [HideInInspector]
  52. public Style style = Style.Once;
  53.  
  54. /// <summary>
  55. /// Optional curve to apply to the tween's time factor value.
  56. /// </summary>
  57.  
  58. [HideInInspector]
  59. public AnimationCurve animationCurve = new AnimationCurve(new Keyframe(0f, 0f, 0f, 1f), new Keyframe(1f, 1f, 1f, 0f));
  60.  
  61. /// <summary>
  62. /// Whether the tween will ignore the timescale, making it work while the game is paused.
  63. /// </summary>
  64.  
  65. [HideInInspector]
  66. public bool ignoreTimeScale = true;
  67.  
  68. /// <summary>
  69. /// How long will the tweener wait before starting the tween?
  70. /// </summary>
  71.  
  72. [HideInInspector]
  73. public float delay = 0f;
  74.  
  75. /// <summary>
  76. /// How long is the duration of the tween?
  77. /// </summary>
  78.  
  79. [HideInInspector]
  80. public float duration = 1f;
  81.  
  82. /// <summary>
  83. /// Whether the tweener will use steeper curves for ease in / out style interpolation.
  84. /// </summary>
  85.  
  86. [HideInInspector]
  87. public bool steeperCurves = false;
  88.  
  89. /// <summary>
  90. /// Used by buttons and tween sequences. Group of '0' means not in a sequence.
  91. /// </summary>
  92.  
  93. [HideInInspector]
  94. public int tweenGroup = 0;
  95.  
  96. /// <summary>
  97. /// Event delegates called when the animation finishes.
  98. /// </summary>
  99.  
  100. [HideInInspector]
  101. public List<EventDelegate> onFinished = new List<EventDelegate>();
  102.  
  103. // Deprecated functionality, kept for backwards compatibility
  104. [HideInInspector] public GameObject eventReceiver;
  105. [HideInInspector] public string callWhenFinished;
  106.  
  107. bool mStarted = false;
  108. float mStartTime = 0f;
  109. float mDuration = 0f;
  110. float mAmountPerDelta = 1000f;
  111. float mFactor = 0f;
  112.  
  113. /// <summary>
  114. /// Amount advanced per delta time.
  115. /// </summary>
  116.  
  117. public float amountPerDelta
  118. {
  119. get
  120. {
  121. if (mDuration != duration)
  122. {
  123. mDuration = duration;
  124. mAmountPerDelta = Mathf.Abs((duration > 0f) ? 1f / duration : 1000f) * Mathf.Sign(mAmountPerDelta);
  125. }
  126. return mAmountPerDelta;
  127. }
  128. }
  129.  
  130. /// <summary>
  131. /// Tween factor, 0-1 range.
  132. /// </summary>
  133.  
  134. public float tweenFactor { get { return mFactor; } set { mFactor = Mathf.Clamp01(value); } }
  135.  
  136. /// <summary>
  137. /// Direction that the tween is currently playing in.
  138. /// </summary>
  139.  
  140. public AnimationOrTween.Direction direction { get { return amountPerDelta < 0f ? AnimationOrTween.Direction.Reverse : AnimationOrTween.Direction.Forward; } }
  141.  
  142. /// <summary>
  143. /// This function is called by Unity when you add a component. Automatically set the starting values for convenience.
  144. /// </summary>
  145.  
  146. void Reset ()
  147. {
  148. if (!mStarted)
  149. {
  150. SetStartToCurrentValue();
  151. SetEndToCurrentValue();
  152. }
  153. }
  154.  
  155. /// <summary>
  156. /// Update as soon as it's started so that there is no delay.
  157. /// </summary>
  158.  
  159. protected virtual void Start () { Update(); }
  160.  
  161. /// <summary>
  162. /// Update the tweening factor and call the virtual update function.
  163. /// </summary>
  164.  
  165. void Update ()
  166. {
  167. float delta = ignoreTimeScale ? RealTime.deltaTime : Time.deltaTime;
  168. float time = ignoreTimeScale ? RealTime.time : Time.time;
  169.  
  170. if (!mStarted)
  171. {
  172. mStarted = true;
  173. mStartTime = time + delay;
  174. }
  175.  
  176. if (time < mStartTime) return;
  177.  
  178. // Advance the sampling factor
  179. mFactor += amountPerDelta * delta;
  180.  
  181. // Loop style simply resets the play factor after it exceeds 1.
  182. if (style == Style.Loop)
  183. {
  184. if (mFactor > 1f)
  185. {
  186. mFactor -= Mathf.Floor(mFactor);
  187. }
  188. }
  189. else if (style == Style.PingPong)
  190. {
  191. // Ping-pong style reverses the direction
  192. if (mFactor > 1f)
  193. {
  194. mFactor = 1f - (mFactor - Mathf.Floor(mFactor));
  195. mAmountPerDelta = -mAmountPerDelta;
  196. }
  197. else if (mFactor < 0f)
  198. {
  199. mFactor = -mFactor;
  200. mFactor -= Mathf.Floor(mFactor);
  201. mAmountPerDelta = -mAmountPerDelta;
  202. }
  203. }else if (style == Style.PingPongOne) // by tin
  204. {
  205. // Ping-pong style reverses the direction
  206. if (mFactor > 1f)
  207. {
  208. mFactor = 1f - (mFactor - Mathf.Floor(mFactor));
  209. mAmountPerDelta = -mAmountPerDelta;
  210.  
  211. }
  212. else if (mFactor < 0f)
  213. {
  214. mFactor = -mFactor;
  215. mFactor -= Mathf.Floor(mFactor);
  216. mAmountPerDelta = -mAmountPerDelta;
  217.  
  218. enabled = false;
  219. if (onFinished != null)
  220. {
  221. mTemp = onFinished;
  222. onFinished = new List<EventDelegate>();
  223.  
  224. // Notify the listener delegates
  225. EventDelegate.Execute(mTemp);
  226.  
  227. // Re-add the previous persistent delegates
  228. for (int i = 0; i < mTemp.Count; ++i)
  229. {
  230. EventDelegate ed = mTemp[i];
  231. if (ed != null && !ed.oneShot) EventDelegate.Add(onFinished, ed, ed.oneShot);
  232. }
  233. mTemp = null;
  234. }
  235.  
  236. }
  237.  
  238. }
  239.  
  240. // If the factor goes out of range and this is a one-time tweening operation, disable the script
  241. if ((style == Style.Once) && (duration == 0f || mFactor > 1f || mFactor < 0f))
  242. {
  243. mFactor = Mathf.Clamp01(mFactor);
  244. Sample(mFactor, true);
  245. enabled = false;
  246.  
  247. if (current == null)
  248. {
  249. UITweener before = current;
  250. current = this;
  251.  
  252. if (onFinished != null)
  253. {
  254. mTemp = onFinished;
  255. onFinished = new List<EventDelegate>();
  256.  
  257. // Notify the listener delegates
  258. EventDelegate.Execute(mTemp);
  259.  
  260. // Re-add the previous persistent delegates
  261. for (int i = 0; i < mTemp.Count; ++i)
  262. {
  263. EventDelegate ed = mTemp[i];
  264. if (ed != null && !ed.oneShot) EventDelegate.Add(onFinished, ed, ed.oneShot);
  265. }
  266. mTemp = null;
  267. }
  268.  
  269. // Deprecated legacy functionality support
  270. if (eventReceiver != null && !string.IsNullOrEmpty(callWhenFinished))
  271. eventReceiver.SendMessage(callWhenFinished, this, SendMessageOptions.DontRequireReceiver);
  272.  
  273. current = before;
  274. }
  275. }
  276. else Sample(mFactor, false);
  277. }
  278.  
  279. List<EventDelegate> mTemp = null;
  280.  
  281. /// <summary>
  282. /// Convenience function -- set a new OnFinished event delegate (here for to be consistent with RemoveOnFinished).
  283. /// </summary>
  284.  
  285. public void SetOnFinished (EventDelegate.Callback del) { EventDelegate.Set(onFinished, del); }
  286.  
  287. /// <summary>
  288. /// Convenience function -- set a new OnFinished event delegate (here for to be consistent with RemoveOnFinished).
  289. /// </summary>
  290.  
  291. public void SetOnFinished (EventDelegate del) { EventDelegate.Set(onFinished, del); }
  292.  
  293. /// <summary>
  294. /// Convenience function -- add a new OnFinished event delegate (here for to be consistent with RemoveOnFinished).
  295. /// </summary>
  296.  
  297. public void AddOnFinished (EventDelegate.Callback del) { EventDelegate.Add(onFinished, del); }
  298.  
  299. /// <summary>
  300. /// Convenience function -- add a new OnFinished event delegate (here for to be consistent with RemoveOnFinished).
  301. /// </summary>
  302.  
  303. public void AddOnFinished (EventDelegate del) { EventDelegate.Add(onFinished, del); }
  304.  
  305. /// <summary>
  306. /// Remove an OnFinished delegate. Will work even while iterating through the list when the tweener has finished its operation.
  307. /// </summary>
  308.  
  309. public void RemoveOnFinished (EventDelegate del)
  310. {
  311. if (onFinished != null) onFinished.Remove(del);
  312. if (mTemp != null) mTemp.Remove(del);
  313. }
  314.  
  315. /// <summary>
  316. /// Mark as not started when finished to enable delay on next play.
  317. /// </summary>
  318.  
  319. void OnDisable () { mStarted = false; }
  320.  
  321. /// <summary>
  322. /// Sample the tween at the specified factor.
  323. /// </summary>
  324.  
  325. public void Sample (float factor, bool isFinished)
  326. {
  327. // Calculate the sampling value
  328. float val = Mathf.Clamp01(factor);
  329.  
  330. if (method == Method.EaseIn)
  331. {
  332. val = 1f - Mathf.Sin(0.5f * Mathf.PI * (1f - val));
  333. if (steeperCurves) val *= val;
  334. }
  335. else if (method == Method.EaseOut)
  336. {
  337. val = Mathf.Sin(0.5f * Mathf.PI * val);
  338.  
  339. if (steeperCurves)
  340. {
  341. val = 1f - val;
  342. val = 1f - val * val;
  343. }
  344. }
  345. else if (method == Method.EaseInOut)
  346. {
  347. const float pi2 = Mathf.PI * 2f;
  348. val = val - Mathf.Sin(val * pi2) / pi2;
  349.  
  350. if (steeperCurves)
  351. {
  352. val = val * 2f - 1f;
  353. float sign = Mathf.Sign(val);
  354. val = 1f - Mathf.Abs(val);
  355. val = 1f - val * val;
  356. val = sign * val * 0.5f + 0.5f;
  357. }
  358. }
  359. else if (method == Method.BounceIn)
  360. {
  361. val = BounceLogic(val);
  362. }
  363. else if (method == Method.BounceOut)
  364. {
  365. val = 1f - BounceLogic(1f - val);
  366. }
  367.  
  368. // Call the virtual update
  369. OnUpdate((animationCurve != null) ? animationCurve.Evaluate(val) : val, isFinished);
  370. }
  371.  
  372. /// <summary>
  373. /// Main Bounce logic to simplify the Sample function
  374. /// </summary>
  375.  
  376. float BounceLogic (float val)
  377. {
  378. if (val < 0.363636f) // 0.363636 = (1/ 2.75)
  379. {
  380. val = 7.5685f * val * val;
  381. }
  382. else if (val < 0.727272f) // 0.727272 = (2 / 2.75)
  383. {
  384. val = 7.5625f * (val -= 0.545454f) * val + 0.75f; // 0.545454f = (1.5 / 2.75)
  385. }
  386. else if (val < 0.909090f) // 0.909090 = (2.5 / 2.75)
  387. {
  388. val = 7.5625f * (val -= 0.818181f) * val + 0.9375f; // 0.818181 = (2.25 / 2.75)
  389. }
  390. else
  391. {
  392. val = 7.5625f * (val -= 0.9545454f) * val + 0.984375f; // 0.9545454 = (2.625 / 2.75)
  393. }
  394. return val;
  395. }
  396.  
  397. /// <summary>
  398. /// Play the tween.
  399. /// </summary>
  400.  
  401. [System.Obsolete("Use PlayForward() instead")]
  402. public void Play () { Play(true); }
  403.  
  404. /// <summary>
  405. /// Play the tween forward.
  406. /// </summary>
  407.  
  408. public void PlayForward () { Play(true); }
  409.  
  410. /// <summary>
  411. /// Play the tween in reverse.
  412. /// </summary>
  413.  
  414. public void PlayReverse () { Play(false); }
  415.  
  416. /// <summary>
  417. /// Manually activate the tweening process, reversing it if necessary.
  418. /// </summary>
  419.  
  420. public void Play (bool forward)
  421. {
  422. mAmountPerDelta = Mathf.Abs(amountPerDelta);
  423. if (!forward) mAmountPerDelta = -mAmountPerDelta;
  424. enabled = true;
  425. Update();
  426. }
  427.  
  428. /// <summary>
  429. /// Manually reset the tweener's state to the beginning.
  430. /// If the tween is playing forward, this means the tween's start.
  431. /// If the tween is playing in reverse, this means the tween's end.
  432. /// </summary>
  433.  
  434. public void ResetToBeginning ()
  435. {
  436. mStarted = false;
  437. mFactor = (amountPerDelta < 0f) ? 1f : 0f;
  438. Sample(mFactor, false);
  439. }
  440.  
  441. /// <summary>
  442. /// Manually start the tweening process, reversing its direction.
  443. /// </summary>
  444.  
  445. public void Toggle ()
  446. {
  447. if (mFactor > 0f)
  448. {
  449. mAmountPerDelta = -amountPerDelta;
  450. }
  451. else
  452. {
  453. mAmountPerDelta = Mathf.Abs(amountPerDelta);
  454. }
  455. enabled = true;
  456. }
  457.  
  458. /// <summary>
  459. /// Actual tweening logic should go here.
  460. /// </summary>
  461.  
  462. abstract protected void OnUpdate (float factor, bool isFinished);
  463.  
  464. /// <summary>
  465. /// Starts the tweening operation.
  466. /// </summary>
  467.  
  468. static public T Begin<T> (GameObject go, float duration) where T : UITweener
  469. {
  470. T comp = go.GetComponent<T>();
  471. #if UNITY_FLASH
  472. if ((object)comp == null) comp = (T)go.AddComponent<T>();
  473. #else
  474. // Find the tween with an unset group ID (group ID of 0).
  475. if (comp != null && comp.tweenGroup != 0)
  476. {
  477. comp = null;
  478. T[] comps = go.GetComponents<T>();
  479. for (int i = 0, imax = comps.Length; i < imax; ++i)
  480. {
  481. comp = comps[i];
  482. if (comp != null && comp.tweenGroup == 0) break;
  483. comp = null;
  484. }
  485. }
  486.  
  487. if (comp == null)
  488. {
  489. comp = go.AddComponent<T>();
  490.  
  491. if (comp == null)
  492. {
  493. Debug.LogError("Unable to add " + typeof(T) + " to " + NGUITools.GetHierarchy(go), go);
  494. return null;
  495. }
  496. }
  497. #endif
  498. comp.mStarted = false;
  499. comp.duration = duration;
  500. comp.mFactor = 0f;
  501. comp.mAmountPerDelta = Mathf.Abs(comp.amountPerDelta);
  502. comp.style = Style.Once;
  503. comp.animationCurve = new AnimationCurve(new Keyframe(0f, 0f, 0f, 1f), new Keyframe(1f, 1f, 1f, 0f));
  504. comp.eventReceiver = null;
  505. comp.callWhenFinished = null;
  506. comp.enabled = true;
  507. return comp;
  508. }
  509.  
  510. /// <summary>
  511. /// Set the 'from' value to the current one.
  512. /// </summary>
  513.  
  514. public virtual void SetStartToCurrentValue () { }
  515.  
  516. /// <summary>
  517. /// Set the 'to' value to the current one.
  518. /// </summary>
  519.  
  520. public virtual void SetEndToCurrentValue () { }
  521. }

为 ngui TweenPosition 添加 pingpongone的更多相关文章

  1. NGUI 动态添加控件

    本文链接地址: Unity3D NGUI动态创建按钮 本例仅以熟悉NGUI组件功能为目的,想快捷简便的创建按钮或其它游戏物体请参考 “Unity3D 动态实例化Prefab” 以动态创建服务器列表为例 ...

  2. NGUI之添加响应函数

    public void ButtonNextClicked() { SelectIndex++; SelectIndex %= ; showGameObject(); } public void Bu ...

  3. Unity NGUI插件

    前言:关于Unity中关于UI的插件,我们最开始学的是UGUI,是Unity自带的UI系统,而在Unity版本还没更新出UGUI之前,除了NGUI没有一个更好些的插件,所以人们不得不去选择NGUI去制 ...

  4. NGUI 层级关系控制

    NGUI元素的遮挡情况是不依赖空间关系,所以在NGUI上添加特效有时候特别蛋疼,特别是美术同学还要依赖空间关系来控制特效效果,那先看看看NGUI的层级是怎么处理的,不过下面的描述都是针对单个相机下的P ...

  5. unity3D游戏开发十八之NGUI动画

    我们先来看下帧动画,顾名思义,就是一帧帧的图片组成的动画,我们须要用到UISprite Animation组件,它的属性例如以下: Framerate:播放速率,也就是每秒钟播放的帧数 Name Pr ...

  6. NGUI插件的一个扩展---NGUI_HUD_Text

    NGUI_HUD_Text扩展主要用于主角跟随和伤害/治疗的功能. 场景大概是这样的,我们希望有一个主角,在其头顶显示他的名字,在单击鼠标左键的时候显示红色的“-10”表示减少血量,单击鼠标右键的时候 ...

  7. 混用ngui和ugui渲染顺序问题

    http://blog.csdn.net/xtxy/article/details/38332801 为NGUI panel 添加 sorting layer 接着上一篇文章的问题,看到了老外做的一个 ...

  8. Unity初探之黑暗之光(1)

    Unity初探之黑暗之光(1) 1.镜头拉近 public float speed=10f;//镜头的移动速度 ;//镜头的结束位置 // Update is called once per fram ...

  9. Unity 黑暗之光 笔记 第一章

    第一章 设计游戏开始进入场景 1.设置相机视野同步 选中要调整的相机 GameObject - Align With View(快捷键 Ctrl + Shift + F)

随机推荐

  1. ASP.NET Web API消息处理管道:Self Host下的消息处理管道[下篇]

    ASP.NET Web API消息处理管道:Self Host下的消息处理管道[下篇] 我们知道ASP.NET Web API借助于HttpSelfHostServer以Self Host模式寄宿于当 ...

  2. WPF刷新界面之坎坷路

    WPF刷新界面之坎坷路 项目需要一个硬件检测功能,需要用到界面刷新,刚开始想用个定时器,对检测过的硬设定时添加后刷新界面. 但是很遗憾,定时器并不能进行刷新.后台检测List数据里面已经添加了很多了很 ...

  3. Python学习入门基础教程(learning Python)--6.3 Python的list切片高级

    上节"6.2 Python的list访问索引和切片"主要学习了Python下的List的访问技术:索引和切片的基础知识,这节将就List的索引index和切片Slice知识点做进一 ...

  4. data矩阵poj 2778 DNA Sequence

    最近研究data矩阵,稍微总结一下,以后继续补充: ac自动机处理字符串,dp计算谜底,用矩阵来减速     每日一道理 巴尔扎克说过“不幸,是天才的进升阶梯,信徒的洗礼之水,弱者的无底深渊”.风雨过 ...

  5. C语言之if和switch的分别

    If和switch的互换规则 用if语句能实现的东西,用switch也可以实现 用switch语句能实现的东西,用if也可以实现 1.如果是判断范围的时候,用switch不太好做,没法直接做 2.如果 ...

  6. 微信小程序开发系列(一)小程序开发初体验

    开发小程序所需的基本技能   关于小程序的介绍和使用场景这里不作介绍,这个系列的文章会一步一步地带领大家快速地学习和掌握小程序的开发. 关于还没有接触过小程序的开发者来说,最关心的问题无非就是,开发小 ...

  7. ruby web性能响应时间

    可以统计单个web页面加载时间. require 'watir-webdriver' require 'watir-webdriver-performance' b = Watir::Browser. ...

  8. AutoLayout 之NSLayoutConstraint

    这次主要讲的用代码来设置AutoLayout,为实现添加autoLayout视图主要介绍使用如下该方法,调用方法:- (void)awakeFromNib {} +(instancetype)cons ...

  9. 修改/etc/resolv.conf又恢复到原来的状态?[转]

    新装一台机器环境为服务器主板,双网卡,系统为CentOS5.4 ,eth0为内网ip,eth1为公网ip.但是由于在本地测试,设置的内网ip,域名服务器同样使用的是上海本地的域名解析,没有问题,可以上 ...

  10. Nuget 学习三

    后期管理: 登录 nuget 官网 https://www.nuget.org/ 可以搜索到自己的包: 点击进入,可进一步操作 如果你需要给自己的类型新增其他功能,或者修改之前的bug(反正就是修改代 ...