public override void SetParent(UIElement parent) { if (parent != null && !(parent is LayoutUIElement)) { Debug.Log("该布局只能添加继承了LayoutUIElement的元素!"); return; } if (parent == Parent) { return; } LayoutUIElement ui = parent as LayoutUIElement; if (ui.ClipParent != null) { ClipParent = ui.ClipParent; } else if (ui._isActiveClipInChild) { ClipParent = ui; } base.SetParent(parent); CalcPosition(); UpdateClipInChild(); }
private void CalcPosition() { if (CurEuiCore == null) { return; } Rect parentRect = Rect.zero; Rect _result = RectInfo; LayoutUIElement parent = Parent as LayoutUIElement; if (parent != null) { parentRect = parent.RectInfo; } else { parentRect = CurEuiCore.CurEditorWindow.position; parentRect.position = Vector2.zero; } if (AnchorMax.x - AnchorMin.x == 0) //不拉伸 { _position.x = parentRect.x + AnchorMax.x * parentRect.width - Size.x * Pivot.x + _anchoredPosition.x; _result.width = Size.x; } else { _position.x = parentRect.x + _offsetMin.x; _result.width = parentRect.width - _offsetMax.x - _offsetMin.x; } if (AnchorMax.y - AnchorMin.y == 0) { _position.y = parentRect.y + (1 - AnchorMax.y) * parentRect.height - Size.y * Pivot.y + _anchoredPosition.y; _result.height = Size.y; } else { _position.y = parentRect.y + _offsetMin.y; _result.height = parentRect.height - _offsetMax.y - _offsetMin.y; } _result.position = Position; RectInfo = _result; }
void UpdateClipInChild() { LayoutUIElement par = Parent as LayoutUIElement; if (par == null) { return; } //判断父节点是是否开启子节点裁剪 Stack <LayoutUIElement> stack = new Stack <LayoutUIElement>(); for (int i = 0; i < ChildCount; i++) { stack.Push(GetChild(i) as LayoutUIElement); } while (stack.Count > 0) { LayoutUIElement ui = stack.Pop(); if (par._isActiveClipInChild) { ui.ClipParent = par; } else if (par.ClipParent != null) { ui.ClipParent = par.ClipParent; } else { ui.ClipParent = null; } //如果当前子物体开启裁剪则取消 ui._isActiveClipInChild = false; for (int i = 0; i < ui.ChildCount; i++) { stack.Push(ui.GetChild(i) as LayoutUIElement); } } }
/// <summary> /// 是否开启裁剪 /// </summary> /// <param name="isActive"></param> public void SetActiveClip(bool isActive) { //如果当前物体已经被裁剪了则不需要裁剪子物体 if (ClipParent != null) { return; } if (_isActiveClipInChild == isActive) { return; } _isActiveClipInChild = isActive; Stack <LayoutUIElement> stack = new Stack <LayoutUIElement>(); for (int i = 0; i < ChildCount; i++) { stack.Push(GetChild(i) as LayoutUIElement); } while (stack.Count > 0) { LayoutUIElement ui = stack.Pop(); if (isActive) { ui.ClipParent = this; } else { ui.ClipParent = null; } //如果当前子物体开启裁剪则取消 ui._isActiveClipInChild = false; for (int i = 0; i < ui.ChildCount; i++) { stack.Push(ui.GetChild(i) as LayoutUIElement); } } }