ChivaExtensions.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using DG.Tweening;
  5. using System;
  6. public static class ChivaExtensions
  7. {
  8. public static void SetWidth(this RectTransform rectTrans, float width)
  9. {
  10. var size = rectTrans.sizeDelta;
  11. size.x = width;
  12. rectTrans.sizeDelta = size;
  13. }
  14. public static void SetHeight(this RectTransform rectTrans, float height)
  15. {
  16. var size = rectTrans.sizeDelta;
  17. size.y = height;
  18. rectTrans.sizeDelta = size;
  19. }
  20. public static void DoWidth(this RectTransform rectTrans, float width, float duration, TweenCallback callBack = null)
  21. {
  22. var size = rectTrans.sizeDelta;
  23. size.x = width;
  24. Tween tween = rectTrans.DOSizeDelta(size, duration);
  25. tween.onComplete += callBack;
  26. }
  27. public static void DoHeight(this RectTransform rectTrans, float height, float duration, TweenCallback callBack = null)
  28. {
  29. var size = rectTrans.sizeDelta;
  30. size.y = height;
  31. Tween tween = rectTrans.DOSizeDelta(size, duration);
  32. tween.onComplete += callBack;
  33. }
  34. public static void SetPositionX(this Transform t, float newX)
  35. {
  36. t.position = new Vector3(newX, t.position.y, t.position.z);
  37. }
  38. public static void SetPositionY(this Transform t, float newY)
  39. {
  40. t.position = new Vector3(t.position.x, newY, t.position.z);
  41. }
  42. public static void SetLocalPositionX(this Transform t, float newX)
  43. {
  44. t.localPosition = new Vector3(newX, t.localPosition.y, t.localPosition.z);
  45. }
  46. public static void SetLocalPositionY(this Transform t, float newY)
  47. {
  48. t.localPosition = new Vector3(t.localPosition.x, newY, t.localPosition.z);
  49. }
  50. public static void SetPositionZ(this Transform t, float newZ)
  51. {
  52. t.position = new Vector3(t.position.x, t.position.y, newZ);
  53. }
  54. public static void SetLocalPositionZ(this Transform t, float newZ)
  55. {
  56. t.localPosition = new Vector3(t.localPosition.x, t.localPosition.y, newZ);
  57. }
  58. public static void SetLocalScale(this Transform t, Vector3 newScale)
  59. {
  60. t.localScale = newScale;
  61. }
  62. public static void SetLocalScaleZero(this Transform t)
  63. {
  64. t.localScale = Vector3.zero;
  65. }
  66. public static float GetPositionX(this Transform t)
  67. {
  68. return t.position.x;
  69. }
  70. public static float GetPositionY(this Transform t)
  71. {
  72. return t.position.y;
  73. }
  74. public static float GetPositionZ(this Transform t)
  75. {
  76. return t.position.z;
  77. }
  78. public static float GetLocalPositionX(this Transform t)
  79. {
  80. return t.localPosition.x;
  81. }
  82. public static float GetLocalPositionY(this Transform t)
  83. {
  84. return t.localPosition.y;
  85. }
  86. public static float GetLocalPositionZ(this Transform t)
  87. {
  88. return t.localPosition.z;
  89. }
  90. public static bool HasRigidbody(this GameObject gobj)
  91. {
  92. return (gobj.GetComponent<Rigidbody>() != null);
  93. }
  94. public static bool HasAnimation(this GameObject gobj)
  95. {
  96. return (gobj.GetComponent<Animation>() != null);
  97. }
  98. public static void SetSpeed(this Animation anim, float newSpeed)
  99. {
  100. anim[anim.clip.name].speed = newSpeed;
  101. }
  102. public static Vector2 ToVector2(this Vector3 vec)
  103. {
  104. return new Vector2(vec.x, vec.y);
  105. }
  106. }