PlayIdleAnimations.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. [AddComponentMenu("NGUI/Examples/Play Idle Animations")]
  6. public class PlayIdleAnimations : MonoBehaviour
  7. {
  8. private void Start()
  9. {
  10. this.mAnim = base.GetComponentInChildren<Animation>();
  11. if (this.mAnim == null)
  12. {
  13. Debug.LogWarning(NGUITools.GetHierarchy(base.gameObject) + " has no Animation component");
  14. UnityEngine.Object.Destroy(this);
  15. }
  16. else
  17. {
  18. IEnumerator enumerator = this.mAnim.GetEnumerator();
  19. try
  20. {
  21. while (enumerator.MoveNext())
  22. {
  23. object obj = enumerator.Current;
  24. AnimationState animationState = (AnimationState)obj;
  25. if (animationState.clip.name == "idle")
  26. {
  27. animationState.layer = 0;
  28. this.mIdle = animationState.clip;
  29. this.mAnim.Play(this.mIdle.name);
  30. }
  31. else if (animationState.clip.name.StartsWith("idle"))
  32. {
  33. animationState.layer = 1;
  34. this.mBreaks.Add(animationState.clip);
  35. }
  36. }
  37. }
  38. finally
  39. {
  40. IDisposable disposable;
  41. if ((disposable = (enumerator as IDisposable)) != null)
  42. {
  43. disposable.Dispose();
  44. }
  45. }
  46. if (this.mBreaks.Count == 0)
  47. {
  48. UnityEngine.Object.Destroy(this);
  49. }
  50. }
  51. }
  52. private void Update()
  53. {
  54. if (this.mNextBreak < Time.time)
  55. {
  56. if (this.mBreaks.Count == 1)
  57. {
  58. AnimationClip animationClip = this.mBreaks[0];
  59. this.mNextBreak = Time.time + animationClip.length + UnityEngine.Random.Range(5f, 15f);
  60. this.mAnim.CrossFade(animationClip.name);
  61. }
  62. else
  63. {
  64. int num = UnityEngine.Random.Range(0, this.mBreaks.Count - 1);
  65. if (this.mLastIndex == num)
  66. {
  67. num++;
  68. if (num >= this.mBreaks.Count)
  69. {
  70. num = 0;
  71. }
  72. }
  73. this.mLastIndex = num;
  74. AnimationClip animationClip2 = this.mBreaks[num];
  75. this.mNextBreak = Time.time + animationClip2.length + UnityEngine.Random.Range(2f, 8f);
  76. this.mAnim.CrossFade(animationClip2.name);
  77. }
  78. }
  79. }
  80. private Animation mAnim;
  81. private AnimationClip mIdle;
  82. private List<AnimationClip> mBreaks = new List<AnimationClip>();
  83. private float mNextBreak;
  84. private int mLastIndex;
  85. }