// 本地坐标系下的的各条边 protected Vector3[] getSides(GameObject parent) { // 如果自身带有旋转,则需要还原自身的变换 Vector3[] sides = new Vector3[4] { Vector3.zero, Vector3.zero, Vector3.zero, Vector3.zero }; UIRect thisRect = WidgetUtility.getGameObjectRect(gameObject); Vector3[] worldCorners = thisRect.worldCorners; Vector3[] localCorners = new Vector3[4]; for (int i = 0; i < 4; ++i) { if (parent != null) { localCorners[i] = parent.transform.InverseTransformPoint(worldCorners[i]); } else { localCorners[i] = worldCorners[i]; } } for (int i = 0; i < 4; ++i) { sides[i] = (localCorners[i] + localCorners[(i + 1) % 4]) / 2; } return(sides); }
//------------------------------------------------------------------------------------------------------------------- public static void addPaddingAnchor(GameObject obj) { // 先设置自己的Anchor if (obj.GetComponent <PaddingAnchor>() == null) { // 只要有Rect就可以添加该组件,panel也可以添加 UIRect rect = WidgetUtility.getGameObjectRect(obj); if (rect != null) { PaddingAnchor anchor = obj.AddComponent <PaddingAnchor>(); anchor.setAnchorMode(ANCHOR_MODE.AM_NEAR_PARENT_SIDE); } } // 再设置子节点的Anchor int childCount = obj.transform.childCount; for (int i = 0; i < childCount; ++i) { addPaddingAnchor(obj.transform.GetChild(i).gameObject); } }