using UnityEngine; namespace UVC.Extension { /// /// RectTransform¿¡ ´ëÇÑ À¯¿ëÇÑ È®Àå ¸Þ¼­µå¸¦ Á¦°øÇϴ Ŭ·¡½ºÀÔ´Ï´Ù. /// public static class RectTransformEx { /// /// RectTransformÀÇ ¸¶ÁøÀ» ¼³Á¤ÇÕ´Ï´Ù. ºÎ¸ð ¿ä¼ÒÀÇ Àüü ¿µ¿ªÀ» ±âÁØÀ¸·Î »óÇÏÁÂ¿ì ¿©¹éÀ» ÁöÁ¤ÇÕ´Ï´Ù. /// ¾ÞÄ¿´Â ÀÚµ¿À¸·Î ¸ð¼­¸®¿¡ ¼³Á¤µË´Ï´Ù(anchorMin=[0,0], anchorMax=[1,1]). /// /// ´ë»ó RectTransform /// ¿ÞÂÊ ¿©¹é /// ¿À¸¥ÂÊ ¿©¹é /// À§ÂÊ ¿©¹é /// ¾Æ·¡ÂÊ ¿©¹é /// /// /// // °ÔÀÓ ¿ÀºêÁ§Æ®ÀÇ RectTransform¿¡ ¿©¹é Àû¿ë /// RectTransform panelRect = panel.GetComponent(); /// panelRect.SetRectMargin(10f, 10f, 10f, 10f); // »ç¹æ 10Çȼ¿ ¿©¹é ¼³Á¤ /// /// // UI ¿ä¼Ò¸¦ ºÎ¸ð ÄÁÅ×À̳ʿ¡ ¸ÂÃßµÇ ¿©¹é ÁÖ±â /// RectTransform childRect = childObject.GetComponent(); /// childRect.SetRectMargin(5f, 5f, 20f, 5f); // »ó´Ü¿¡ ´õ Å« ¿©¹é ¼³Á¤ /// /// public static void SetRectMargin(this RectTransform trans, float left, float right, float top, float bottom) { trans.anchorMin = new Vector2(0, 0); trans.anchorMax = new Vector2(1f, 1f); trans.offsetMin = new Vector2(left, bottom); trans.offsetMax = new Vector2(-right, -top); } /// /// RectTransformÀÇ ³Êºñ¸¦ ¼³Á¤ÇÕ´Ï´Ù. /// /// ´ë»ó RectTransform /// ¼³Á¤ÇÒ ³Êºñ /// /// /// // ¹öưÀÇ ³Êºñ¸¦ 200À¸·Î ¼³Á¤ /// RectTransform buttonRect = button.GetComponent(); /// buttonRect.SetWidth(200f); /// /// public static void SetWidth(this RectTransform trans, float width) { trans.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, width); } /// /// RectTransformÀÇ ³ôÀ̸¦ ¼³Á¤ÇÕ´Ï´Ù. /// /// ´ë»ó RectTransform /// ¼³Á¤ÇÒ ³ôÀÌ /// /// /// // ÆÐ³ÎÀÇ ³ôÀ̸¦ 150À¸·Î ¼³Á¤ /// RectTransform panelRect = panel.GetComponent(); /// panelRect.SetHeight(150f); /// /// public static void SetHeight(this RectTransform trans, float height) { trans.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height); } /// /// RectTransformÀÇ Å©±â¸¦ ¼³Á¤ÇÕ´Ï´Ù. /// /// ´ë»ó RectTransform /// ¼³Á¤ÇÒ Å©±â (³Êºñ, ³ôÀÌ) /// /// /// // À̹ÌÁöÀÇ Å©±â¸¦ 100x100À¸·Î ¼³Á¤ /// RectTransform imageRect = image.GetComponent(); /// imageRect.SetSize(new Vector2(100f, 100f)); /// /// public static void SetSize(this RectTransform trans, Vector2 size) { trans.SetWidth(size.x); trans.SetHeight(size.y); } /// /// RectTransformÀÇ ¾ÞÄ¿¸¦ Áß¾Ó¿¡ À§Ä¡½Ã۰í Çǹþµµ Áß¾ÓÀ¸·Î ¼³Á¤ÇÕ´Ï´Ù. /// /// ´ë»ó RectTransform /// /// /// // ¹öưÀ» È­¸é Áß¾Ó¿¡ À§Ä¡½Ã۱â /// RectTransform buttonRect = button.GetComponent(); /// buttonRect.SetAnchorsToCenter(); /// buttonRect.anchoredPosition = Vector2.zero; // Áß¾Ó À§Ä¡¿¡ ¹èÄ¡ /// /// public static void SetAnchorsToCenter(this RectTransform trans) { trans.anchorMin = new Vector2(0.5f, 0.5f); trans.anchorMax = new Vector2(0.5f, 0.5f); trans.pivot = new Vector2(0.5f, 0.5f); } /// /// RectTransformÀÇ ¾ÞÄ¿¿Í ÇǹþÀ» ¿ÞÂÊ »ó´ÜÀ¸·Î ¼³Á¤ÇÕ´Ï´Ù. /// /// ´ë»ó RectTransform /// /// /// // UI ¿ä¼Ò¸¦ ¿ÞÂÊ »ó´Ü¿¡ À§Ä¡½Ã۱â /// RectTransform elementRect = element.GetComponent(); /// elementRect.SetAnchorsToTopLeft(); /// elementRect.anchoredPosition = new Vector2(10f, -10f); // ¾à°£ÀÇ ¿©¹é Ãß°¡ /// /// public static void SetAnchorsToTopLeft(this RectTransform trans) { trans.anchorMin = new Vector2(0f, 1f); trans.anchorMax = new Vector2(0f, 1f); trans.pivot = new Vector2(0f, 1f); } /// /// RectTransformÀ» ºÎ¸ð ¿µ¿ª¿¡ ²Ë Â÷°Ô ¼³Á¤ÇÕ´Ï´Ù(¸¶Áø ¾øÀ½). /// /// ´ë»ó RectTransform /// /// /// // ¹è°æ À̹ÌÁö¸¦ ÆÐ³Î Àüü¿¡ ä¿ì±â /// RectTransform backgroundRect = backgroundImage.GetComponent(); /// backgroundRect.StretchToParentEdges(); /// /// public static void StretchToParentEdges(this RectTransform trans) { trans.SetRectMargin(0f, 0f, 0f, 0f); } /// /// RectTransformÀÇ À§Ä¡¸¦ ºÎ¸ð RectTransform ³»ÀÇ »ó´ë À§Ä¡(0~1)·Î ¼³Á¤ÇÕ´Ï´Ù. /// /// ´ë»ó RectTransform /// Á¤±ÔÈ­µÈ À§Ä¡ (0,0=¿ÞÂÊ ÇÏ´Ü, 1,1=¿À¸¥ÂÊ »ó´Ü) /// /// /// // ¿ä¼Ò¸¦ ºÎ¸ðÀÇ ¿À¸¥ÂÊ »ó´Ü¿¡ ¹èÄ¡ /// RectTransform elementRect = element.GetComponent(); /// elementRect.SetNormalizedPosition(new Vector2(0.95f, 0.95f)); /// /// public static void SetNormalizedPosition(this RectTransform trans, Vector2 normalizedPosition) { if (trans.parent is RectTransform parent) { trans.anchorMin = trans.anchorMax = new Vector2(0, 0); trans.pivot = new Vector2(0.5f, 0.5f); Rect parentRect = parent.rect; float x = parentRect.x + parentRect.width * normalizedPosition.x; float y = parentRect.y + parentRect.height * normalizedPosition.y; trans.anchoredPosition = new Vector2(x, y); } } /// /// RectTransformÀÇ »ç°¢Çü Á¤º¸¸¦ ¿ùµå ÁÂÇ¥·Î ¹ÝȯÇÕ´Ï´Ù. /// /// ´ë»ó RectTransform /// ¿ùµå ÁÂÇ¥°èÀÇ »ç°¢Çü °æ°è /// /// /// // UI ¿ä¼Ò°¡ ƯÁ¤ ¿ùµå ÁÂÇ¥¸¦ Æ÷ÇÔÇÏ´ÂÁö È®ÀÎ /// RectTransform elementRect = element.GetComponent(); /// Rect worldRect = elementRect.GetWorldRect(); /// Vector3 worldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition); /// bool isOverUI = worldRect.Contains(new Vector2(worldPos.x, worldPos.y)); /// /// public static Rect GetWorldRect(this RectTransform trans) { Vector3[] corners = new Vector3[4]; trans.GetWorldCorners(corners); Vector2 min = corners[0]; Vector2 max = corners[2]; return new Rect(min, max - min); } } }