public void init(LAYOUT_TYPE type, string name, int renderOrder, bool isNGUI, bool isScene) { mName = name; mType = type; mIsNGUI = isNGUI; mIsScene = isScene; mScript = mLayoutManager.createScript(mName, this); if (mScript == null) { logError("can not create layout script! type : " + mType); } // 初始化布局脚本 if (!mIsScene) { if (mIsNGUI) { mScript.newObject(out mLayoutPanel, mLayoutManager.getNGUIRoot(), mName); } else { mScript.newObject(out mLayoutCanvas, mLayoutManager.getUGUIRoot(), mName); } } else { if (mIsNGUI) { mScript.newObject(out mLayoutPanel, null, mName); } else { mScript.newObject(out mLayoutCanvas, null, mName); } } mScript.newObject(out mRoot, getLayoutPanel(), "Root"); setRenderOrder(renderOrder); mScript.setRoot(mRoot); mScript.assignWindow(); // 布局实例化完成,初始化之前,需要调用自适应组件的更新 ScaleAnchor scaleAnchor = getLayoutPanel().getObject().GetComponent <ScaleAnchor>(); PaddingAnchor customAnchor = getLayoutPanel().getObject().GetComponent <PaddingAnchor>(); if (scaleAnchor != null) { ScaleAnchor.forceUpdateChildren(getLayoutPanel().getObject()); } if (customAnchor != null) { PaddingAnchor.forceUpdateChildren(getLayoutPanel().getObject()); } mScript.init(); mScriptInited = true; // 加载完布局后强制隐藏 setVisibleForce(false); }
public override void OnInspectorGUI() { anchor = target as ScaleAnchor; anchor.mKeepAspect = displayToggle("KeepAspect", anchor.mKeepAspect); anchor.mAdjustFont = displayToggle("AdjustFont", anchor.mAdjustFont); if (anchor.mKeepAspect) { var aspectBase = (ASPECT_BASE)displayEnum("AspectBase", anchor.mAspectBase); anchor.mAspectBase = aspectBase; } }
static void ClaerCalculation() { if (Selection.activeGameObject == null) { return; } ScaleAnchor Anchor = Selection.activeGameObject.GetComponent <ScaleAnchor>(); Anchor.mHorizontalRelativePos = 0.0f; Anchor.mVerticalRelativePos = 0.0f; Anchor.mPadding = PADDING_STYLE.PS_NONE; }
public void forceUpdateChildren() { // 先更新自己 updateRect(true); // 然后更新所有子节点 int childCount = transform.childCount; for (int i = 0; i < childCount; ++i) { ScaleAnchor anchor = transform.GetChild(i).GetComponent <ScaleAnchor>(); if (anchor != null) { anchor.forceUpdateChildren(); } } }
public override void OnInspectorGUI() { anchor = target as ScaleAnchor; anchor.mKeepAspect = displayToggle("KeepAspect", anchor.mKeepAspect, out bool keepAspectModified); anchor.mAdjustFont = displayToggle("AdjustFont", anchor.mAdjustFont, out bool adjustFountModified); bool aspectBaseModified = false; if (anchor.mKeepAspect) { var aspectBase = (ASPECT_BASE)displayEnum("AspectBase", anchor.mAspectBase, out aspectBaseModified); anchor.mAspectBase = aspectBase; } if (keepAspectModified || adjustFountModified || aspectBaseModified) { EditorUtility.SetDirty(target); } }
static void Calculation() { if (Selection.activeGameObject == null) { return; } ScaleAnchor anchor = Selection.activeGameObject.GetComponent <ScaleAnchor>(); if (anchor == null) { return; } var obj = Selection.activeGameObject; Vector2 thisPos = obj.transform.localPosition; // 获取父物体的属性 UIRect parentRect = CustomAnchor.findParentRect(obj); Vector2 parentSize = CustomAnchor.getRectSize(parentRect); // 计算 anchor.mHorizontalRelativePos = thisPos.x / parentSize.x * 2; anchor.mVerticalRelativePos = thisPos.y / parentSize.y * 2; // 设置成自定义方式 anchor.mPadding = PADDING_STYLE.PS_CUSTOM_VALUE; }