Unity 使用this关键字进行函数拓展 - GameObject

举报
CoderZ1010 发表于 2022/09/25 07:18:44 2022/09/25
【摘要】 Example: private void Start(){ var testComponent = gameObject .Activate() .Deactivate() .Name("") .Layer(0) .Layer("") .Tag...

Example:


  
  1. private void Start()
  2. {
  3. var testComponent = gameObject
  4. .Activate()
  5. .Deactivate()
  6. .Name("")
  7. .Layer(0)
  8. .Layer("")
  9. .Tag("")
  10. .GetComponentForcibly(typeof(LineRenderer));
  11. bool isActive = testComponent
  12. .Activate()
  13. .Deactivate()
  14. .Name("")
  15. .Layer(0)
  16. .Layer("")
  17. .Tag("")
  18. .IsActive();
  19. }

Extension:


  
  1. /// <summary>
  2. /// The extension of gameobject.
  3. /// </summary>
  4. public static class GameObjectExtension
  5. {
  6. /// <summary>
  7. /// Activate the gameobject.
  8. /// </summary>
  9. /// <param name="self"></param>
  10. /// <returns></returns>
  11. public static GameObject Activate(this GameObject self)
  12. {
  13. self.SetActive(true);
  14. return self;
  15. }
  16. /// <summary>
  17. /// Deactivate the gameobject.
  18. /// </summary>
  19. /// <param name="self"></param>
  20. /// <returns></returns>
  21. public static GameObject Deactivate(this GameObject self)
  22. {
  23. self.SetActive(false);
  24. return self;
  25. }
  26. /// <summary>
  27. /// Set name for the gameobject.
  28. /// </summary>
  29. /// <param name="self"></param>
  30. /// <param name="name"></param>
  31. /// <returns></returns>
  32. public static GameObject Name(this GameObject self, string name)
  33. {
  34. self.name = name;
  35. return self;
  36. }
  37. /// <summary>
  38. /// Set layer for the gameobject.
  39. /// </summary>
  40. /// <param name="self"></param>
  41. /// <param name="layer"></param>
  42. /// <returns></returns>
  43. public static GameObject Layer(this GameObject self, int layer)
  44. {
  45. self.layer = layer;
  46. return self;
  47. }
  48. /// <summary>
  49. /// Set layer for the gameobject.
  50. /// </summary>
  51. /// <param name="self"></param>
  52. /// <param name="layer"></param>
  53. /// <returns></returns>
  54. public static GameObject Layer(this GameObject self, string layer)
  55. {
  56. self.layer = LayerMask.NameToLayer(layer);
  57. return self;
  58. }
  59. /// <summary>
  60. /// Set tag for the gameobject.
  61. /// </summary>
  62. /// <param name="self"></param>
  63. /// <param name="tag"></param>
  64. /// <returns></returns>
  65. public static GameObject Tag(this GameObject self, string tag)
  66. {
  67. self.tag = tag;
  68. return self;
  69. }
  70. /// <summary>
  71. /// Get specified component, if it's null, add and return.
  72. /// </summary>
  73. /// <typeparam name="T"></typeparam>
  74. /// <param name="self"></param>
  75. /// <returns></returns>
  76. public static T GetComponentForcibly<T>(this GameObject self) where T : Component
  77. {
  78. var component = self.GetComponent<T>();
  79. return component ?? self.AddComponent<T>();
  80. }
  81. /// <summary>
  82. /// Get specified component, if it's null, add and return.
  83. /// </summary>
  84. /// <param name="self"></param>
  85. /// <param name="type"></param>
  86. /// <returns></returns>
  87. public static Component GetComponentForcibly(this GameObject self, Type type)
  88. {
  89. var component = self.GetComponent(type);
  90. return component ?? self.AddComponent(type);
  91. }
  92. /// <summary>
  93. /// Get the local active state of the compontent's gameObject.
  94. /// </summary>
  95. /// <typeparam name="T"></typeparam>
  96. /// <param name="self"></param>
  97. /// <returns></returns>
  98. public static bool IsActive<T>(this T self) where T : Component
  99. {
  100. return self.gameObject.activeSelf;
  101. }
  102. /// <summary>
  103. /// Activate the component's gameobject.
  104. /// </summary>
  105. /// <typeparam name="T"></typeparam>
  106. /// <param name="self"></param>
  107. /// <returns></returns>
  108. public static T Activate<T>(this T self) where T : Component
  109. {
  110. self.gameObject.SetActive(true);
  111. return self;
  112. }
  113. /// <summary>
  114. /// Deactivate the component's gameobject.
  115. /// </summary>
  116. /// <typeparam name="T"></typeparam>
  117. /// <param name="self"></param>
  118. /// <returns></returns>
  119. public static T Deactivate<T>(this T self) where T : Component
  120. {
  121. self.gameObject.SetActive(false);
  122. return self;
  123. }
  124. /// <summary>
  125. /// Set name for the component's gameobject.
  126. /// </summary>
  127. /// <typeparam name="T"></typeparam>
  128. /// <param name="self"></param>
  129. /// <param name="name"></param>
  130. /// <returns></returns>
  131. public static T Name<T>(this T self, string name) where T : Component
  132. {
  133. self.gameObject.name = name;
  134. return self;
  135. }
  136. /// <summary>
  137. /// Set layer for the component's gameobject.
  138. /// </summary>
  139. /// <typeparam name="T"></typeparam>
  140. /// <param name="self"></param>
  141. /// <param name="layer"></param>
  142. /// <returns></returns>
  143. public static T Layer<T>(this T self, int layer) where T : Component
  144. {
  145. self.gameObject.layer = layer;
  146. return self;
  147. }
  148. /// <summary>
  149. /// Set layer for the component's gameobject.
  150. /// </summary>
  151. /// <typeparam name="T"></typeparam>
  152. /// <param name="self"></param>
  153. /// <param name="layer"></param>
  154. /// <returns></returns>
  155. public static T Layer<T>(this T self, string layer) where T : Component
  156. {
  157. self.gameObject.layer = LayerMask.NameToLayer(layer);
  158. return self;
  159. }
  160. /// <summary>
  161. /// Set tag for the component's gameobject.
  162. /// </summary>
  163. /// <typeparam name="T"></typeparam>
  164. /// <param name="self"></param>
  165. /// <param name="tag"></param>
  166. /// <returns></returns>
  167. public static T Tag<T>(this T self, string tag) where T : Component
  168. {
  169. self.gameObject.tag = tag;
  170. return self;
  171. }
  172. }

文章来源: coderz.blog.csdn.net,作者:CoderZ1010,版权归原作者所有,如需转载,请联系作者。

原文链接:coderz.blog.csdn.net/article/details/109022581

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。