public void AddCustomDebugToolItem(string text, GameHandler callbackHandler) { GameObject newGo = GameCloneUtils.CreateEmptyUIObjectWithParent(DebugToolsItemHost.transform, "DebugToolItem"); RectTransform rectTransform = newGo.GetComponent <RectTransform>(); rectTransform.pivot = new Vector2(0, 1); UIAnchorPosUtils.SetUIAnchor(rectTransform, UIAnchor.Stretch, UIAnchor.Top); CustomData customData = newGo.AddComponent <CustomData> (); customData.customData = callbackHandler; Text newText = newGo.AddComponent <Text>(); newText.font = Resources.GetBuiltinResource <Font>("Arial.ttf"); newText.text = text; newText.color = Color.white; newText.fontSize = 11; newText.alignment = TextAnchor.MiddleCenter; ContentSizeFitter contentSizeFitter = newGo.AddComponent <ContentSizeFitter>(); contentSizeFitter.verticalFit = ContentSizeFitter.FitMode.PreferredSize; EventTriggerListener.Get(newGo).onClick = OnCustomDebugToolItemClick; rectTransform.anchoredPosition = new Vector2(rectTransform.anchoredPosition.x, -customDebugToolItemY); customDebugToolItemY += 20; DebugToolsItemHost.sizeDelta = new Vector2(DebugToolsItemHost.sizeDelta.x, customDebugToolItemY); }
protected override void OnLayout() { UIElement e = null; RectTransform rectTransform = null; float startVal = 0; UIAnchor[] thisAnchor = UIAnchorPosUtils.GetUIAnchor(RectTransform); //需要布局子布局, for (int i = 0; i < Elements.Count; i++) { e = Elements[i]; if (e.Visibility != UIVisibility.Gone) { if (e.IsLayout) { (e as UILayout).DoLayout(); } } } //计算所有元素布局占用高度 float allLayoutHeight = (Elements.Count - 1) * layoutChildSpacing; //另外一个轴最高的元素高度 float maxLayoutOtherHeight = 0; for (int i = 0; i < Elements.Count; i++) { e = Elements[i]; if (e.Visibility != UIVisibility.Gone) { rectTransform = e.RectTransform; allLayoutHeight += (layoutDirection == LayoutAxis.Vertical ? rectTransform.rect.height : rectTransform.rect.width); //计算控件边距 allLayoutHeight += (layoutDirection == LayoutAxis.Vertical ? (e.Layout_marginTop + e.Layout_marginBottom) : (e.Layout_marginLeft + e.Layout_marginRight)); if (layoutDirection == LayoutAxis.Vertical) { if (e.AnchorX == UIAnchor.Stretch) { UIAnchorPosUtils.SetUIAnchor(e.RectTransform, UIAnchor.Stretch, layoutReverse ? UIAnchor.Bottom : UIAnchor.Top); UIAnchorPosUtils.SetUILeftBottom(e.RectTransform, e.Layout_marginLeft, UIAnchorPosUtils.GetUIBottom(e.RectTransform)); UIAnchorPosUtils.SetUIRightTop(e.RectTransform, e.Layout_marginRight, UIAnchorPosUtils.GetUITop(e.RectTransform)); } else { UIAnchorPosUtils.SetUIAnchor(e.RectTransform, UILayoutUtils.GravityToAnchor(Gravity, RectTransform.Axis.Horizontal), layoutReverse ? UIAnchor.Bottom : UIAnchor.Top); e.RectTransform.anchoredPosition = Vector2.zero; } UIAnchorPosUtils.SetUIPivot(e.RectTransform, UILayoutUtils.AnchorToPivot( layoutReverse ? UIAnchor.Bottom : UIAnchor.Top, RectTransform.Axis.Vertical), RectTransform.Axis.Vertical); e.DoResize(); if (e.RectTransform.rect.width > maxLayoutOtherHeight) { maxLayoutOtherHeight = e.RectTransform.rect.width; } } else { if (e.AnchorY == UIAnchor.Stretch) { UIAnchorPosUtils.SetUIAnchor(e.RectTransform, layoutReverse ? UIAnchor.Right : UIAnchor.Left, UIAnchor.Stretch); UIAnchorPosUtils.SetUILeftBottom(e.RectTransform, UIAnchorPosUtils.GetUILeft(e.RectTransform), e.Layout_marginBottom); UIAnchorPosUtils.SetUIRightTop(e.RectTransform, UIAnchorPosUtils.GetUIRight(e.RectTransform), e.Layout_marginTop); } else { UIAnchorPosUtils.SetUIAnchor(e.RectTransform, layoutReverse ? UIAnchor.Right : UIAnchor.Left, UILayoutUtils.GravityToAnchor(Gravity, RectTransform.Axis.Vertical)); e.RectTransform.anchoredPosition = Vector2.zero; } UIAnchorPosUtils.SetUIPivot(e.RectTransform, UILayoutUtils.AnchorToPivot( layoutReverse ? UIAnchor.Right : UIAnchor.Left, RectTransform.Axis.Horizontal), RectTransform.Axis.Horizontal); e.DoResize(); if (e.RectTransform.rect.height > maxLayoutOtherHeight) { maxLayoutOtherHeight = e.RectTransform.rect.height; } } } } if (maxLayoutOtherHeight <= 0) { maxLayoutOtherHeight = layoutDirection == LayoutAxis.Vertical ? RectTransform.rect.width : RectTransform.rect.height; } //自动将本容器撑大 if (layoutDirection == LayoutAxis.Vertical) { if (MaxSize.y > 0 && allLayoutHeight > MaxSize.y) { allLayoutHeight = MaxSize.y; } if (allLayoutHeight > MinSize.y) { RectTransform.sizeDelta = new Vector2(widthAutoWarp ? maxLayoutOtherHeight : RectTransform.sizeDelta.x, allLayoutHeight); } else if (thisAnchor[1] != UIAnchor.Stretch) { RectTransform.sizeDelta = new Vector2(widthAutoWarp ? maxLayoutOtherHeight : RectTransform.sizeDelta.x, MinSize.y); } } else if (layoutDirection == LayoutAxis.Horizontal) { if (MaxSize.x > 0 && allLayoutHeight > MaxSize.x) { allLayoutHeight = MaxSize.x; } if (allLayoutHeight > MinSize.x) { RectTransform.sizeDelta = new Vector2(allLayoutHeight, heightAutoWarp ? maxLayoutOtherHeight : RectTransform.sizeDelta.y); } else if (thisAnchor[0] != UIAnchor.Stretch) { RectTransform.sizeDelta = new Vector2(MinSize.x, heightAutoWarp ? maxLayoutOtherHeight : RectTransform.sizeDelta.y); } } DoResize(); //如果内容空间小于容器大小,那么居中内容 if (allLayoutHeight < (layoutDirection == LayoutAxis.Vertical ? RectTransform.rect.height : RectTransform.rect.width)) { if (layoutDirection == LayoutAxis.Vertical) { if ((Gravity & LayoutGravity.CenterVertical) == LayoutGravity.CenterVertical) { startVal -= (RectTransform.rect.height / 2 - allLayoutHeight / 2); } if ((Gravity & LayoutGravity.Bottom) == LayoutGravity.Bottom) { startVal -= (RectTransform.rect.height - allLayoutHeight); } } else { if ((Gravity & LayoutGravity.CenterHorizontal) == LayoutGravity.CenterHorizontal) { startVal += (RectTransform.rect.width / 2 - allLayoutHeight / 2); } if ((Gravity & LayoutGravity.End) == LayoutGravity.End) { startVal += (RectTransform.rect.width - allLayoutHeight); } } } //布局 if (!layoutReverse) { for (int i = 0; i < Elements.Count; i++) { e = Elements[i]; if (e.Visibility != UIVisibility.Gone) //跳过隐藏元素 { rectTransform = e.RectTransform; if (layoutDirection == LayoutAxis.Vertical) { startVal -= e.Layout_marginTop; rectTransform.anchoredPosition = new Vector2(rectTransform.anchoredPosition.x, startVal); startVal -= e.Layout_marginBottom; startVal -= (rectTransform.rect.height + layoutChildSpacing); } else { startVal += e.Layout_marginLeft; rectTransform.anchoredPosition = new Vector2(startVal, rectTransform.anchoredPosition.y); startVal += e.Layout_marginRight; startVal += (rectTransform.rect.width + layoutChildSpacing); } } } } else { //反向布局 for (int i = Elements.Count; i >= 0; i--) { e = Elements[i]; if (e.Visibility != UIVisibility.Gone)//跳过隐藏元素 { rectTransform = e.RectTransform; if (layoutDirection == LayoutAxis.Vertical) { startVal -= e.Layout_marginTop; rectTransform.anchoredPosition = new Vector2(rectTransform.anchoredPosition.x, startVal); startVal -= e.Layout_marginBottom; startVal = startVal - rectTransform.rect.height - layoutChildSpacing; } else { startVal += e.Layout_marginLeft; rectTransform.anchoredPosition = new Vector2(startVal, rectTransform.anchoredPosition.y); startVal += e.Layout_marginRight; startVal = startVal + rectTransform.rect.width + layoutChildSpacing; } } } } if (Parent != null) { Parent.DoLayout(); } base.OnLayout(); }
private void DoLayoutElementVerticalInternal(UIElement e) { UIAnchor a = UIAnchor.None; if (e.AnchorY != UIAnchor.Stretch) { if (!string.IsNullOrEmpty(e.Layout_alignTop) || !string.IsNullOrEmpty(e.Layout_below) || e.Layout_alignParentTop) { a = UIAnchor.Top; } else if (!string.IsNullOrEmpty(e.Layout_alignBottom) || !string.IsNullOrEmpty(e.Layout_above) || e.Layout_alignParentBottom) { a = UIAnchor.Bottom; } else if (e.Layout_centerVertical || e.Layout_centerInParent) { a = UIAnchor.Center; } else { a = UILayoutUtils.GravityToAnchor(Gravity, RectTransform.Axis.Vertical); } UIAnchorPosUtils.SetUIAnchor(e.RectTransform, UIAnchorPosUtils.GetUIAnchor(e.RectTransform, RectTransform.Axis.Horizontal), a); UIAnchorPosUtils.SetUIPivot(e.RectTransform, UILayoutUtils.AnchorToPivot(a, RectTransform.Axis.Vertical), RectTransform.Axis.Vertical); float finalValue = 0; UIElement eRef = null; if (!string.IsNullOrEmpty(e.Layout_below)) { eRef = FindElementInLayoutByName(e.Layout_below); if (eRef != null) { finalValue = eRef.RectTransform.anchoredPosition.y - eRef.RectTransform.rect.height - e.Layout_marginTop; } } else if (!string.IsNullOrEmpty(e.Layout_above)) { eRef = FindElementInLayoutByName(e.Layout_above); if (eRef != null) { finalValue = eRef.RectTransform.anchoredPosition.x + e.RectTransform.rect.height + e.Layout_marginBottom; } } else if (!string.IsNullOrEmpty(e.Layout_alignTop)) { eRef = FindElementInLayoutByName(e.Layout_alignTop); if (eRef != null) { finalValue = eRef.RectTransform.anchoredPosition.y - e.Layout_marginTop; } } else if (!string.IsNullOrEmpty(e.Layout_alignBottom)) { eRef = FindElementInLayoutByName(e.Layout_alignBottom); if (eRef != null) { finalValue = eRef.RectTransform.anchoredPosition.y - eRef.RectTransform.rect.height + e.RectTransform.rect.height + e.Layout_marginBottom; } } else if (a == UIAnchor.Center) { finalValue = 0; } else if (a == UIAnchor.Top) { finalValue = -e.Layout_marginTop; } else if (a == UIAnchor.Bottom) { finalValue = e.Layout_marginBottom; } e.RectTransform.anchoredPosition = new Vector2( e.RectTransform.anchoredPosition.x, finalValue ); } }
protected virtual void SetProp(string name, string val) { switch (name) { case "name": { Name = val; break; } case "minSize": { string[] av = val.Split(','); float x, y; if (av.Length >= 2 && float.TryParse(av[0], out x) && float.TryParse(av[1], out y)) { minSize = new Vector2(x, y); DoPostLayout(); } break; } case "width": { if (val.ToLower() == "match_parent") { anchorX = UIAnchor.Stretch; UIAnchorPosUtils.SetUIAnchor(RectTransform, anchorX, anchorY); DoPostLayout(); } else { float x; if (float.TryParse(val, out x)) { RectTransform.sizeDelta = new Vector2(x, RectTransform.sizeDelta.y); DoPostLayout(); } } break; } case "height": { if (val.ToLower() == "match_parent") { anchorY = UIAnchor.Stretch; UIAnchorPosUtils.SetUIAnchor(RectTransform, anchorX, anchorY); DoPostLayout(); } else { float y; if (float.TryParse(val, out y)) { RectTransform.sizeDelta = new Vector2(RectTransform.sizeDelta.x, y); DoPostLayout(); } } break; } case "x": { float v; if (float.TryParse(val, out v)) { RectTransform.anchoredPosition = new Vector2(v, RectTransform.anchoredPosition.y); DoPostLayout(); } break; } case "y": { float v; if (float.TryParse(val, out v)) { RectTransform.anchoredPosition = new Vector2(RectTransform.anchoredPosition.x, v); DoPostLayout(); } break; } case "position": { string[] av = val.Split(','); float x, y; if (av.Length >= 2 && float.TryParse(av[0], out x) && float.TryParse(av[1], out y)) { RectTransform.anchoredPosition = new Vector2(x, y); DoPostLayout(); } break; } case "left": { float v; if (float.TryParse(val, out v)) { UIAnchorPosUtils.SetUILeftBottom(RectTransform, v, UIAnchorPosUtils.GetUIBottom(RectTransform)); } break; } case "bottom": { float v; if (float.TryParse(val, out v)) { UIAnchorPosUtils.SetUILeftBottom(RectTransform, UIAnchorPosUtils.GetUILeft(RectTransform), v); } break; } case "leftBottom": { string[] av = val.Split(','); float x, y; if (av.Length >= 2 && float.TryParse(av[0], out x) && float.TryParse(av[1], out y)) { UIAnchorPosUtils.SetUILeftBottom(RectTransform, x, y); } break; } case "right": { float v; if (float.TryParse(val, out v)) { UIAnchorPosUtils.SetUIRightTop(RectTransform, v, UIAnchorPosUtils.GetUITop(RectTransform)); } break; } case "top": { float v; if (float.TryParse(val, out v)) { UIAnchorPosUtils.SetUIRightTop(RectTransform, UIAnchorPosUtils.GetUIRight(RectTransform), v); } break; } case "rightTop": { string[] av = val.Split(','); float x, y; if (av.Length >= 2 && float.TryParse(av[0], out x) && float.TryParse(av[1], out y)) { UIAnchorPosUtils.SetUIRightTop(RectTransform, x, y); } break; } case "anchor": { string[] av = val.Split(','); if (av.Length >= 2) { System.Enum.TryParse(av[0], out anchorX); System.Enum.TryParse(av[1], out anchorY); DoPostLayout(); } break; } case "anchorX": { System.Enum.TryParse(val, out anchorX); DoPostLayout(); break; } case "visibility": { UIVisibility v; System.Enum.TryParse(val, out v); Visibility = v; break; } case "anchorY": { System.Enum.TryParse(val, out anchorY); DoPostLayout(); break; } case "layout_margin": { string[] av = val.Split(','); float x, y, z, w; if (av.Length >= 4 && float.TryParse(av[0], out x) && float.TryParse(av[1], out y) && float.TryParse(av[1], out z) && float.TryParse(av[2], out w)) { Layout_marginLeft = x; Layout_marginTop = y; Layout_marginRight = z; Layout_marginBottom = w; } if (av.Length >= 2 && float.TryParse(av[0], out x) && float.TryParse(av[1], out y)) { Layout_marginLeft = x; Layout_marginTop = y; Layout_marginRight = x; Layout_marginBottom = y; } if (av.Length >= 1 && float.TryParse(av[0], out x)) { Layout_marginLeft = x; Layout_marginTop = x; Layout_marginRight = x; Layout_marginBottom = x; } DoPostLayout(); break; } case "layout_marginLeft": { float v; if (float.TryParse(val, out v)) { Layout_marginLeft = v; DoPostLayout(); } break; } case "layout_marginTop": { float v; if (float.TryParse(val, out v)) { Layout_marginTop = v; DoPostLayout(); } break; } case "layout_marginRight": { float v; if (float.TryParse(val, out v)) { Layout_marginRight = v; DoPostLayout(); } break; } case "layout_marginBottom": { float v; if (float.TryParse(val, out v)) { Layout_marginBottom = v; DoPostLayout(); } break; } case "layout_centerHorizontal": { if (bool.TryParse(val, out layout_centerHorizontal)) { DoPostLayout(); } break; } case "layout_centerVertical": { if (bool.TryParse(val, out layout_centerVertical)) { DoPostLayout(); } break; } case "layout_centerInParent": { if (bool.TryParse(val, out layout_centerInParent)) { DoPostLayout(); } break; } case "layout_alignParentTop": { if (bool.TryParse(val, out layout_alignParentTop)) { DoPostLayout(); } break; } case "layout_alignParentBottom": { if (bool.TryParse(val, out layout_alignParentBottom)) { DoPostLayout(); } break; } case "layout_alignParentLeft": { if (bool.TryParse(val, out layout_alignParentLeft)) { DoPostLayout(); } break; } case "layout_alignParentRight": { if (bool.TryParse(val, out layout_alignParentRight)) { DoPostLayout(); } break; } case "layout_above": { layout_toLeftOf = val; DoPostLayout(); break; } case "layout_below": { layout_toLeftOf = val; DoPostLayout(); break; } case "layout_toRightOf": { layout_toLeftOf = val; DoPostLayout(); break; } case "layout_toLeftOf": { layout_toLeftOf = val; DoPostLayout(); break; } case "layout_alignTop": { layout_alignTop = val; DoPostLayout(); break; } case "layout_alignBottom": { layout_alignBottom = val; DoPostLayout(); break; } case "layout_alignLeft": { layout_alignLeft = val; DoPostLayout(); break; } case "layout_alignRight": { layout_alignRight = val; DoPostLayout(); break; } } }
private void DoLayoutElementHorizontalInternal(UIElement e) { UIAnchor a = UIAnchor.None; if (e.AnchorX != UIAnchor.Stretch) { if (!string.IsNullOrEmpty(e.Layout_alignLeft) || !string.IsNullOrEmpty(e.Layout_toLeftOf) || e.Layout_alignParentLeft) { a = UIAnchor.Left; } else if (!string.IsNullOrEmpty(e.Layout_alignRight) || !string.IsNullOrEmpty(e.Layout_toRightOf) || e.Layout_alignParentRight) { a = UIAnchor.Right; } else if (e.Layout_centerHorizontal || e.Layout_centerInParent) { a = UIAnchor.Center; } else { a = UILayoutUtils.GravityToAnchor(Gravity, RectTransform.Axis.Horizontal); } UIAnchorPosUtils.SetUIAnchor(e.RectTransform, a, UIAnchorPosUtils.GetUIAnchor(e.RectTransform, RectTransform.Axis.Vertical)); UIAnchorPosUtils.SetUIPivot(e.RectTransform, UILayoutUtils.AnchorToPivot(a, RectTransform.Axis.Horizontal), RectTransform.Axis.Horizontal); float finalValue = 0; UIElement eRef = null; if (!string.IsNullOrEmpty(e.Layout_toLeftOf)) { eRef = FindElementInLayoutByName(e.Layout_toLeftOf); if (eRef != null) { finalValue = eRef.RectTransform.anchoredPosition.x + eRef.RectTransform.rect.width + e.Layout_marginLeft; } } else if (!string.IsNullOrEmpty(e.Layout_toRightOf)) { eRef = FindElementInLayoutByName(e.Layout_toRightOf); if (eRef != null) { finalValue = eRef.RectTransform.anchoredPosition.x - eRef.RectTransform.rect.width - e.RectTransform.rect.width - e.Layout_marginRight; } } else if (!string.IsNullOrEmpty(e.Layout_alignLeft)) { eRef = FindElementInLayoutByName(e.Layout_alignLeft); if (eRef != null) { finalValue = eRef.RectTransform.anchoredPosition.x + e.Layout_marginLeft; } } else if (!string.IsNullOrEmpty(e.Layout_alignRight)) { eRef = FindElementInLayoutByName(e.Layout_alignRight); if (eRef != null) { finalValue = eRef.RectTransform.anchoredPosition.x + eRef.RectTransform.rect.width - e.RectTransform.rect.width - e.Layout_marginRight; } } else if (a == UIAnchor.Center) { finalValue = 0; } else if (a == UIAnchor.Right) { finalValue = -e.Layout_marginRight; } else if (a == UIAnchor.Left) { finalValue = e.Layout_marginLeft; } e.RectTransform.anchoredPosition = new Vector2( finalValue, e.RectTransform.anchoredPosition.y); } }