RectTranform通用位置大小工具
# 最简单的就是,直接将anchor先设为center-mid,然后再设置位置大小
public static void SetAnchoredPos(RectTransform rtf, float x, float y) { var v2Mid = Vector2.one * 0.5f; rtf.anchorMin = v2Mid; rtf.anchorMax = v2Mid; var v2 = rtf.anchoredPosition; v2.x = x; v2.y = y; rtf.anchoredPosition = v2; } public static void SetSize(RectTransform rtf, float w, float h) { var size = rtf.sizeDelta; size.Set(w, h); rtf.sizeDelta = size; }
# 不调整原来的anchor设置,通过left-top边距来调整RectTramsform的位置
/// <summary> /// 移动到left-top边距处(与parent的left和top边, 不是anchor边), 保持大小不变 /// </summary> public static void MoveByLeftTopMargin(RectTransform self, float leftMargin, float topMargin) { MoveByLeftTopMargin(self, leftMargin, topMargin, (RectTransform) self.parent); } public static void MoveByLeftTopMargin(RectTransform self, float leftMargin, float topMargin, RectTransform parentRtf) { var sizeDelta = self.sizeDelta; var rect = self.rect; var parentRect = parentRtf.rect; var offsetMin = self.offsetMin; var offsetMax = self.offsetMax; var anchorMin = self.anchorMin; var newLeftAnchorMargin = leftMargin - anchorMin.x * parentRect.width; newLeftAnchorMargin -= rect.width * 0.5f * (1 - self.localScale.x); if (sizeDelta.x <= 0) //x方向拉伸 { var marginDiff = newLeftAnchorMargin - offsetMin.x /* oldAnchorMargin */; offsetMax.x += marginDiff; } else { offsetMax.x = newLeftAnchorMargin + rect.width; } offsetMin.x = newLeftAnchorMargin; var anchorMax = self.anchorMax; var newTopAnchorMargin = topMargin - (1 - anchorMax.y) * parentRect.height; newTopAnchorMargin -= rect.height * 0.5f * (1 - self.localScale.y); if (sizeDelta.y <= 0) //y方向拉伸 { var marginDiff = -newTopAnchorMargin - offsetMax.y /* oldAnchorMargin */; offsetMin.y += marginDiff; } else { offsetMin.y = -newTopAnchorMargin - rect.height; } offsetMax.y = -newTopAnchorMargin; //我们的margin是parent内部为正, 外部为负; unity的top offset是向上为正, 向下为负 self.offsetMin = offsetMin; self.offsetMax = offsetMax; }
# 获取边距
/// <summary> /// 获取与parent的边距(非anchor边), 在parent内部为正, 在parent外部为负 /// </summary> public static float GetMarginLeft(RectTransform self) { return GetMarginLeft(self, (RectTransform) self.parent); } public static float GetMarginLeft(RectTransform self, RectTransform parentRtf) { var parentRect = parentRtf.rect; return self.offsetMin.x + (self.anchorMin.x * parentRect.width); } /// <summary> /// 获取与parent的边距(非anchor边), 在parent内部为正, 在parent外部为负 /// </summary> public static float GetMarginTop(RectTransform self) { return GetMarginTop(self, (RectTransform) self.parent); } public static float GetMarginTop(RectTransform self, RectTransform parentRtf) { var parentRect = parentRtf.rect; return -self.offsetMax.y + (1 - self.anchorMax.y) * parentRect.height; } /// <summary> /// 获取与parent的边距(非anchor边), 在parent内部为正, 在parent外部为负 /// </summary> public static float GetMarginRight(RectTransform self) { return GetMarginRight(self, (RectTransform) self.parent); } public static float GetMarginRight(RectTransform self, RectTransform parentRtf) { var parentRect = parentRtf.rect; return -self.offsetMax.x + (1 - self.anchorMax.x) * parentRect.width; } /// <summary> /// 获取与parent的边距(非anchor边), 在parent内部为正, 在parent外部为负 /// </summary> public static float GetMarginBottom(RectTransform self) { return GetMarginBottom(self, (RectTransform) self.parent); } public static float GetMarginBottom(RectTransform self, RectTransform parentRtf) { var parentRect = parentRtf.rect; return self.offsetMin.y + self.anchorMin.y * parentRect.height; }
# 设置大小
/// <param name="leftDiff">向左为负, 向右为正</param> /// <param name="rightDiff">向左为负, 向右为正</param> /// <param name="topDiff">向上为正, 向下为负</param> /// <param name="bottomDiff">向上为正, 向下为负</param> public static void SetSizeByDiff(RectTransform rtf, float leftDiff, float rightDiff, float topDiff, float bottomDiff) { var offsetMin = rtf.offsetMin; var offsetMax = rtf.offsetMax; offsetMin.x += leftDiff; offsetMax.x += rightDiff; offsetMin.y += bottomDiff; offsetMax.y += topDiff; rtf.offsetMin = offsetMin; rtf.offsetMax = offsetMax; } public static void SetSize(RectTransform rtf, float w, float h) { var sizeDelta = rtf.sizeDelta; if (sizeDelta.x <= 0 && sizeDelta.y <= 0) //x, y都拉伸 { var offsetMin = rtf.offsetMin; var offsetMax = rtf.offsetMax; var parentRectTrans = rtf.parent as RectTransform; if (null != parentRectTrans) { offsetMax.x = w + offsetMin.x - parentRectTrans.rect.width; offsetMax.y = h + offsetMin.y - parentRectTrans.rect.height; rtf.offsetMax = offsetMax; } } if (sizeDelta.x > 0 && sizeDelta.y > 0) { sizeDelta.x = w; sizeDelta.y = h; rtf.sizeDelta = sizeDelta; } else if (sizeDelta.x > 0) //x方向正常, y方向拉伸 { sizeDelta.x = w; rtf.sizeDelta = sizeDelta; //y方向拉伸 var offsetMin = rtf.offsetMin; var offsetMax = rtf.offsetMax; var parentRectTrans = rtf.parent as RectTransform; if (null != parentRectTrans) { //根据: dst-height = parentRectTrans.rect.height + offsetMax.y - offsetMin.y offsetMax.y = h + offsetMin.y - parentRectTrans.rect.height; rtf.offsetMax = offsetMax; } } else if (sizeDelta.y > 0) //x方向拉伸, y方向正常 { //x方向拉伸 var offsetMin = rtf.offsetMin; var offsetMax = rtf.offsetMax; var parentRectTrans = rtf.parent as RectTransform; if (null != parentRectTrans) { //根据: dst-width = parentRectTrans.rect.width + offsetMax.x - offsetMin.x offsetMax.x = w + offsetMin.x - parentRectTrans.rect.width; rtf.offsetMax = offsetMax; } sizeDelta.y = h; rtf.sizeDelta = sizeDelta; } }