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

举报
CoderZ1010 发表于 2022/09/25 04:32:06 2022/09/25
【摘要】 Example: private void Start(){ (transform as RectTransform) .AnchoredPosition(Vector2.zero) .AnchoredPosition(0f, 0f) .AnchoredPositionX(0f) .Anc...

Example:


  
  1. private void Start()
  2. {
  3. (transform as RectTransform)
  4. .AnchoredPosition(Vector2.zero)
  5. .AnchoredPosition(0f, 0f)
  6. .AnchoredPositionX(0f)
  7. .AnchoredPositionY(0f)
  8. .OffsetMax(Vector2.zero)
  9. .OffsetMax(0f, 0f)
  10. .OffsetMaxX(0f)
  11. .OffsetMaxY(0f)
  12. .OffsetMin(Vector2.zero)
  13. .OffsetMin(0f, 0f)
  14. .OffsetMinX(0f)
  15. .OffsetMinY(0f)
  16. .AnchoredPosition3D(Vector2.zero)
  17. .AnchoredPosition3D(0f, 0f)
  18. .AnchoredPosition3DX(0f)
  19. .AnchoredPosition3DY(0f)
  20. .AnchorMin(Vector2.zero)
  21. .AnchorMin(0f, 0f)
  22. .AnchorMinX(0f)
  23. .AnchorMinY(0f)
  24. .AnchorMax(Vector2.zero)
  25. .AnchorMax(0f, 0f)
  26. .AnchorMaxX(0f)
  27. .AnchorMaxY(0f)
  28. .Pivot(Vector2.zero)
  29. .Pivot(0f, 0f)
  30. .PivotX(0f)
  31. .PivotY(0f)
  32. .SizeDelta(Vector2.zero)
  33. .SizeDelta(0f, 0f)
  34. .SizeDeltaX(0f)
  35. .SizeDeltaY(0f)
  36. .SetSizeWidth(0f)
  37. .SetSizeHeight(0f);
  38. }

Extension:


  
  1. /// <summary>
  2. /// The extension of rect transform.
  3. /// </summary>
  4. public static class RectTransformExtension
  5. {
  6. #region AnchoredPosition
  7. /// <summary>
  8. /// Set the anchored position.
  9. /// </summary>
  10. /// <param name="self"></param>
  11. /// <param name="anchoredPosition"></param>
  12. /// <returns></returns>
  13. public static RectTransform AnchoredPosition(this RectTransform self, Vector3 anchoredPosition)
  14. {
  15. self.anchoredPosition = anchoredPosition;
  16. return self;
  17. }
  18. /// <summary>
  19. /// Set the anchored position.
  20. /// </summary>
  21. /// <param name="self"></param>
  22. /// <param name="x"></param>
  23. /// <param name="y"></param>
  24. /// <returns></returns>
  25. public static RectTransform AnchoredPosition(this RectTransform self, float x, float y)
  26. {
  27. Vector2 anchoredPosition = self.anchoredPosition;
  28. anchoredPosition.x = x;
  29. anchoredPosition.y = y;
  30. self.anchoredPosition = anchoredPosition;
  31. return self;
  32. }
  33. /// <summary>
  34. /// Set the anchored position x value.
  35. /// </summary>
  36. /// <param name="self"></param>
  37. /// <param name="x"></param>
  38. /// <returns></returns>
  39. public static RectTransform AnchoredPositionX(this RectTransform self, float x)
  40. {
  41. Vector2 anchoredPosition = self.anchoredPosition;
  42. anchoredPosition.x = x;
  43. self.anchoredPosition = anchoredPosition;
  44. return self;
  45. }
  46. /// <summary>
  47. /// Set the anchored position y value.
  48. /// </summary>
  49. /// <param name="self"></param>
  50. /// <param name="y"></param>
  51. /// <returns></returns>
  52. public static RectTransform AnchoredPositionY(this RectTransform self, float y)
  53. {
  54. Vector2 anchoredPosition = self.anchoredPosition;
  55. anchoredPosition.y = y;
  56. self.anchoredPosition = anchoredPosition;
  57. return self;
  58. }
  59. #endregion
  60. #region OffsetMax
  61. /// <summary>
  62. /// Set the offset max.
  63. /// </summary>
  64. /// <param name="self"></param>
  65. /// <param name="offsetMax"></param>
  66. /// <returns></returns>
  67. public static RectTransform OffsetMax(this RectTransform self, Vector2 offsetMax)
  68. {
  69. self.offsetMax = offsetMax;
  70. return self;
  71. }
  72. /// <summary>
  73. /// Set the offset max.
  74. /// </summary>
  75. /// <param name="self"></param>
  76. /// <param name="x"></param>
  77. /// <param name="y"></param>
  78. /// <returns></returns>
  79. public static RectTransform OffsetMax(this RectTransform self, float x, float y)
  80. {
  81. Vector2 offsetMax = self.offsetMax;
  82. offsetMax.x = x;
  83. offsetMax.y = y;
  84. self.offsetMax = offsetMax;
  85. return self;
  86. }
  87. /// <summary>
  88. /// Set the offset max x value.
  89. /// </summary>
  90. /// <param name="self"></param>
  91. /// <param name="x"></param>
  92. /// <returns></returns>
  93. public static RectTransform OffsetMaxX(this RectTransform self, float x)
  94. {
  95. Vector2 offsetMax = self.offsetMax;
  96. offsetMax.x = x;
  97. self.offsetMax = offsetMax;
  98. return self;
  99. }
  100. /// <summary>
  101. /// Set the offset max y value.
  102. /// </summary>
  103. /// <param name="self"></param>
  104. /// <param name="y"></param>
  105. /// <returns></returns>
  106. public static RectTransform OffsetMaxY(this RectTransform self, float y)
  107. {
  108. Vector2 offsetMax = self.offsetMax;
  109. offsetMax.y = y;
  110. self.offsetMax = offsetMax;
  111. return self;
  112. }
  113. #endregion
  114. #region OffsetMin
  115. /// <summary>
  116. /// Set the offset min.
  117. /// </summary>
  118. /// <param name="self"></param>
  119. /// <param name="offsetMin"></param>
  120. /// <returns></returns>
  121. public static RectTransform OffsetMin(this RectTransform self, Vector2 offsetMin)
  122. {
  123. self.offsetMin = offsetMin;
  124. return self;
  125. }
  126. /// <summary>
  127. /// Set the offset min.
  128. /// </summary>
  129. /// <param name="self"></param>
  130. /// <param name="x"></param>
  131. /// <param name="y"></param>
  132. /// <returns></returns>
  133. public static RectTransform OffsetMin(this RectTransform self, float x, float y)
  134. {
  135. Vector2 offsetMin = self.offsetMin;
  136. offsetMin.x = x;
  137. offsetMin.y = y;
  138. self.offsetMin = offsetMin;
  139. return self;
  140. }
  141. /// <summary>
  142. /// Set the offset min x value.
  143. /// </summary>
  144. /// <param name="self"></param>
  145. /// <param name="x"></param>
  146. /// <returns></returns>
  147. public static RectTransform OffsetMinX(this RectTransform self, float x)
  148. {
  149. Vector2 offsetMin = self.offsetMin;
  150. offsetMin.x = x;
  151. self.offsetMin = offsetMin;
  152. return self;
  153. }
  154. /// <summary>
  155. /// Set the offset min y value.
  156. /// </summary>
  157. /// <param name="self"></param>
  158. /// <param name="y"></param>
  159. /// <returns></returns>
  160. public static RectTransform OffsetMinY(this RectTransform self, float y)
  161. {
  162. Vector2 offsetMin = self.offsetMin;
  163. offsetMin.y = y;
  164. self.offsetMin = offsetMin;
  165. return self;
  166. }
  167. #endregion
  168. #region AnchoredPosition3D
  169. /// <summary>
  170. /// Set the anchored position 3d.
  171. /// </summary>
  172. /// <param name="self"></param>
  173. /// <param name="anchoredPosition3D"></param>
  174. /// <returns></returns>
  175. public static RectTransform AnchoredPosition3D(this RectTransform self, Vector2 anchoredPosition3D)
  176. {
  177. self.anchoredPosition3D = anchoredPosition3D;
  178. return self;
  179. }
  180. /// <summary>
  181. /// Set the anchored position 3d.
  182. /// </summary>
  183. /// <param name="self"></param>
  184. /// <param name="x"></param>
  185. /// <param name="y"></param>
  186. /// <returns></returns>
  187. public static RectTransform AnchoredPosition3D(this RectTransform self, float x, float y)
  188. {
  189. Vector2 anchoredPosition3D = self.anchoredPosition3D;
  190. anchoredPosition3D.x = x;
  191. anchoredPosition3D.y = y;
  192. self.anchoredPosition3D = anchoredPosition3D;
  193. return self;
  194. }
  195. /// <summary>
  196. /// Set the anchored position 3d x value.
  197. /// </summary>
  198. /// <param name="self"></param>
  199. /// <param name="x"></param>
  200. /// <returns></returns>
  201. public static RectTransform AnchoredPosition3DX(this RectTransform self, float x)
  202. {
  203. Vector2 anchoredPosition3D = self.anchoredPosition3D;
  204. anchoredPosition3D.x = x;
  205. self.anchoredPosition3D = anchoredPosition3D;
  206. return self;
  207. }
  208. /// <summary>
  209. /// Set the anchored position 3d y value.
  210. /// </summary>
  211. /// <param name="self"></param>
  212. /// <param name="y"></param>
  213. /// <returns></returns>
  214. public static RectTransform AnchoredPosition3DY(this RectTransform self, float y)
  215. {
  216. Vector2 anchoredPosition3D = self.anchoredPosition3D;
  217. anchoredPosition3D.y = y;
  218. self.anchoredPosition3D = anchoredPosition3D;
  219. return self;
  220. }
  221. #endregion
  222. #region AnchorMin
  223. /// <summary>
  224. /// Set the anchor min.
  225. /// </summary>
  226. /// <param name="self"></param>
  227. /// <param name="anchorMin"></param>
  228. /// <returns></returns>
  229. public static RectTransform AnchorMin(this RectTransform self, Vector2 anchorMin)
  230. {
  231. self.anchorMin = anchorMin;
  232. return self;
  233. }
  234. /// <summary>
  235. /// Set the anchor min.
  236. /// </summary>
  237. /// <param name="self"></param>
  238. /// <param name="x"></param>
  239. /// <param name="y"></param>
  240. /// <returns></returns>
  241. public static RectTransform AnchorMin(this RectTransform self, float x, float y)
  242. {
  243. Vector2 anchorMin = self.anchorMin;
  244. anchorMin.x = x;
  245. anchorMin.y = y;
  246. self.anchorMin = anchorMin;
  247. return self;
  248. }
  249. /// <summary>
  250. /// Set the anchor min x value.
  251. /// </summary>
  252. /// <param name="self"></param>
  253. /// <param name="x"></param>
  254. /// <returns></returns>
  255. public static RectTransform AnchorMinX(this RectTransform self, float x)
  256. {
  257. Vector2 anchorMin = self.anchorMin;
  258. anchorMin.x = x;
  259. self.anchorMin = anchorMin;
  260. return self;
  261. }
  262. /// <summary>
  263. /// Set the anchor min y value.
  264. /// </summary>
  265. /// <param name="self"></param>
  266. /// <param name="y"></param>
  267. /// <returns></returns>
  268. public static RectTransform AnchorMinY(this RectTransform self, float y)
  269. {
  270. Vector2 anchorMin = self.anchorMin;
  271. anchorMin.y = y;
  272. self.anchorMin = anchorMin;
  273. return self;
  274. }
  275. #endregion
  276. #region AnchorMax
  277. /// <summary>
  278. /// Set the anchor max.
  279. /// </summary>
  280. /// <param name="self"></param>
  281. /// <param name="anchorMax"></param>
  282. /// <returns></returns>
  283. public static RectTransform AnchorMax(this RectTransform self, Vector2 anchorMax)
  284. {
  285. self.anchorMax = anchorMax;
  286. return self;
  287. }
  288. /// <summary>
  289. /// Set the anchor max.
  290. /// </summary>
  291. /// <param name="self"></param>
  292. /// <param name="x"></param>
  293. /// <param name="y"></param>
  294. /// <returns></returns>
  295. public static RectTransform AnchorMax(this RectTransform self, float x, float y)
  296. {
  297. Vector2 anchorMax = self.anchorMax;
  298. anchorMax.x = x;
  299. anchorMax.y = y;
  300. self.anchorMax = anchorMax;
  301. return self;
  302. }
  303. /// <summary>
  304. /// Set the anchor max x value.
  305. /// </summary>
  306. /// <param name="self"></param>
  307. /// <param name="x"></param>
  308. /// <returns></returns>
  309. public static RectTransform AnchorMaxX(this RectTransform self, float x)
  310. {
  311. Vector2 anchorMax = self.anchorMax;
  312. anchorMax.x = x;
  313. self.anchorMax = anchorMax;
  314. return self;
  315. }
  316. /// <summary>
  317. /// Set the anchor max y value.
  318. /// </summary>
  319. /// <param name="self"></param>
  320. /// <param name="y"></param>
  321. /// <returns></returns>
  322. public static RectTransform AnchorMaxY(this RectTransform self, float y)
  323. {
  324. Vector2 anchorMax = self.anchorMax;
  325. anchorMax.y = y;
  326. self.anchorMax = anchorMax;
  327. return self;
  328. }
  329. #endregion
  330. #region Pivot
  331. /// <summary>
  332. /// Set the pivot.
  333. /// </summary>
  334. /// <param name="self"></param>
  335. /// <param name="pivot"></param>
  336. /// <returns></returns>
  337. public static RectTransform Pivot(this RectTransform self, Vector2 pivot)
  338. {
  339. self.pivot = pivot;
  340. return self;
  341. }
  342. /// <summary>
  343. /// Set the pivot.
  344. /// </summary>
  345. /// <param name="self"></param>
  346. /// <param name="x"></param>
  347. /// <param name="y"></param>
  348. /// <returns></returns>
  349. public static RectTransform Pivot(this RectTransform self, float x, float y)
  350. {
  351. Vector2 pivot = self.pivot;
  352. pivot.x = x;
  353. pivot.y = y;
  354. self.pivot = pivot;
  355. return self;
  356. }
  357. /// <summary>
  358. /// Set the pivot x value.
  359. /// </summary>
  360. /// <param name="self"></param>
  361. /// <param name="x"></param>
  362. /// <returns></returns>
  363. public static RectTransform PivotX(this RectTransform self, float x)
  364. {
  365. Vector2 pivot = self.pivot;
  366. pivot.x = x;
  367. self.pivot = pivot;
  368. return self;
  369. }
  370. /// <summary>
  371. /// Set the pivot y value.
  372. /// </summary>
  373. /// <param name="self"></param>
  374. /// <param name="y"></param>
  375. /// <returns></returns>
  376. public static RectTransform PivotY(this RectTransform self, float y)
  377. {
  378. Vector2 pivot = self.pivot;
  379. pivot.y = y;
  380. self.pivot = pivot;
  381. return self;
  382. }
  383. #endregion
  384. #region SizeDelta
  385. /// <summary>
  386. /// Set the size delta.
  387. /// </summary>
  388. /// <param name="self"></param>
  389. /// <param name="sizeDelta"></param>
  390. /// <returns></returns>
  391. public static RectTransform SizeDelta(this RectTransform self, Vector2 sizeDelta)
  392. {
  393. self.sizeDelta = sizeDelta;
  394. return self;
  395. }
  396. /// <summary>
  397. /// Set the size delta.
  398. /// </summary>
  399. /// <param name="self"></param>
  400. /// <param name="x"></param>
  401. /// <param name="y"></param>
  402. /// <returns></returns>
  403. public static RectTransform SizeDelta(this RectTransform self, float x, float y)
  404. {
  405. Vector2 sizeDelta = self.sizeDelta;
  406. sizeDelta.x = x;
  407. sizeDelta.y = y;
  408. self.sizeDelta = sizeDelta;
  409. return self;
  410. }
  411. /// <summary>
  412. /// Set the size delta x value.
  413. /// </summary>
  414. /// <param name="self"></param>
  415. /// <param name="x"></param>
  416. /// <returns></returns>
  417. public static RectTransform SizeDeltaX(this RectTransform self, float x)
  418. {
  419. Vector2 sizeDelta = self.sizeDelta;
  420. sizeDelta.x = x;
  421. self.sizeDelta = sizeDelta;
  422. return self;
  423. }
  424. /// <summary>
  425. /// Set the size delta y value.
  426. /// </summary>
  427. /// <param name="self"></param>
  428. /// <param name="y"></param>
  429. /// <returns></returns>
  430. public static RectTransform SizeDeltaY(this RectTransform self, float y)
  431. {
  432. Vector2 sizeDelta = self.sizeDelta;
  433. sizeDelta.y = y;
  434. self.sizeDelta = sizeDelta;
  435. return self;
  436. }
  437. #endregion
  438. #region Size
  439. /// <summary>
  440. /// Set width with current anchors.
  441. /// </summary>
  442. /// <param name="self"></param>
  443. /// <param name="width"></param>
  444. /// <returns></returns>
  445. public static RectTransform SetSizeWidth(this RectTransform self, float width)
  446. {
  447. self.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, width);
  448. return self;
  449. }
  450. /// <summary>
  451. /// Set height with current anchors.
  452. /// </summary>
  453. /// <param name="self"></param>
  454. /// <param name="height"></param>
  455. /// <returns></returns>
  456. public static RectTransform SetSizeHeight(this RectTransform self, float height)
  457. {
  458. self.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height);
  459. return self;
  460. }
  461. #endregion
  462. }

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

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

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

评论(0

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

全部回复

上滑加载中

设置昵称

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

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

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