ExtensionTransform.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. public static class ExtensionTransform
  6. {
  7. public static void SetX(this Transform tra, float x)
  8. {
  9. tra.position = new Vector3(x, tra.position.y, tra.position.z);
  10. }
  11. public static void SetY(this Transform tra, float y)
  12. {
  13. tra.position = new Vector3(tra.position.x, y, tra.position.z);
  14. }
  15. public static void SetZ(this Transform tra, float z)
  16. {
  17. tra.position = new Vector3(tra.position.x, tra.position.y, z);
  18. }
  19. public static void SetLX(this Transform tra, float x)
  20. {
  21. tra.localPosition = new Vector3(x, tra.localPosition.y, tra.localPosition.z);
  22. }
  23. public static void SetLY(this Transform tra, float y)
  24. {
  25. tra.localPosition = new Vector3(tra.localPosition.x, y, tra.localPosition.z);
  26. }
  27. public static void SetLZ(this Transform tra, float z)
  28. {
  29. tra.localPosition = new Vector3(tra.localPosition.x, tra.localPosition.y, z);
  30. }
  31. public static void SetEX(this Transform tra, float x)
  32. {
  33. tra.eulerAngles = new Vector3(x, tra.eulerAngles.y, tra.eulerAngles.z);
  34. }
  35. public static void SetEY(this Transform tra, float y)
  36. {
  37. tra.eulerAngles = new Vector3(tra.eulerAngles.x, y, tra.eulerAngles.z);
  38. }
  39. public static void SetEZ(this Transform tra, float z)
  40. {
  41. tra.eulerAngles = new Vector3(tra.eulerAngles.x, tra.eulerAngles.y, z);
  42. }
  43. public static Vector3 GetScale(this Transform tra)
  44. {
  45. Vector3 scale = tra.localScale;
  46. while (tra.parent != null)
  47. {
  48. float x = scale.x * tra.parent.localScale.x;
  49. float y = scale.y * tra.parent.localScale.y;
  50. float z = scale.z * tra.parent.localScale.z;
  51. scale = new Vector3(x, y, z);
  52. tra = tra.parent;
  53. }
  54. return scale;
  55. }
  56. public static Component AddComponent(this Transform tra, Type type)
  57. {
  58. return tra.gameObject.AddComponent(type);
  59. }
  60. public static Dictionary<string, Transform> Compile(this Transform tra)
  61. {
  62. Dictionary<string, Transform> dic = new Dictionary<string, Transform>();
  63. Transform[] transforms = tra.GetComponentsInChildren<Transform>(true);
  64. for (int i = 0; i < transforms.Length; i++)
  65. {
  66. if (dic.ContainsKey(transforms[i].name))
  67. {
  68. throw new Exception(transforms[i].name);
  69. }
  70. else
  71. {
  72. dic.Add(transforms[i].name, transforms[i]);
  73. }
  74. }
  75. return dic;
  76. }
  77. public static Dictionary<string, Transform> Compile(this Transform tra, Dictionary<string, Transform> dic)
  78. {
  79. Transform[] transforms = tra.GetComponentsInChildren<Transform>(true);
  80. for (int i = 0; i < transforms.Length; i++)
  81. {
  82. if (dic.ContainsKey(transforms[i].name))
  83. {
  84. throw new Exception(transforms[i].name);
  85. }
  86. else
  87. {
  88. dic.Add(transforms[i].name, transforms[i]);
  89. }
  90. }
  91. return dic;
  92. }
  93. }