UIButtonScale.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. using UnityEngine;
  3. [AddComponentMenu("NGUI/Interaction/Button Scale")]
  4. public class UIButtonScale : MonoBehaviour
  5. {
  6. private void Start()
  7. {
  8. if (!this.mStarted)
  9. {
  10. this.mStarted = true;
  11. if (this.tweenTarget == null)
  12. {
  13. this.tweenTarget = base.transform;
  14. }
  15. this.mScale = this.tweenTarget.localScale;
  16. }
  17. }
  18. private void OnEnable()
  19. {
  20. if (this.mStarted)
  21. {
  22. this.OnHover(UICamera.IsHighlighted(base.gameObject));
  23. }
  24. }
  25. private void OnDisable()
  26. {
  27. if (this.mStarted && this.tweenTarget != null)
  28. {
  29. TweenScale component = this.tweenTarget.GetComponent<TweenScale>();
  30. if (component != null)
  31. {
  32. component.value = this.mScale;
  33. component.enabled = false;
  34. }
  35. }
  36. }
  37. private void OnPress(bool isPressed)
  38. {
  39. if (base.enabled)
  40. {
  41. if (!this.mStarted)
  42. {
  43. this.Start();
  44. }
  45. TweenScale.Begin(this.tweenTarget.gameObject, this.duration, (!isPressed) ? ((!UICamera.IsHighlighted(base.gameObject)) ? this.mScale : Vector3.Scale(this.mScale, this.hover)) : Vector3.Scale(this.mScale, this.pressed)).method = UITweener.Method.EaseInOut;
  46. }
  47. }
  48. private void OnHover(bool isOver)
  49. {
  50. if (base.enabled)
  51. {
  52. if (!this.mStarted)
  53. {
  54. this.Start();
  55. }
  56. TweenScale.Begin(this.tweenTarget.gameObject, this.duration, (!isOver) ? this.mScale : Vector3.Scale(this.mScale, this.hover)).method = UITweener.Method.EaseInOut;
  57. }
  58. }
  59. private void OnSelect(bool isSelected)
  60. {
  61. if (base.enabled && (!isSelected || UICamera.currentScheme == UICamera.ControlScheme.Controller))
  62. {
  63. this.OnHover(isSelected);
  64. }
  65. }
  66. public Transform tweenTarget;
  67. public Vector3 hover = new Vector3(1.1f, 1.1f, 1.1f);
  68. public Vector3 pressed = new Vector3(1.05f, 1.05f, 1.05f);
  69. public float duration = 0.2f;
  70. private Vector3 mScale;
  71. private bool mStarted;
  72. }