public override void OnInspectorGUI()
    {
        serializedObject.Update();
        anchor             = target as PaddingAnchor;
        anchor.mAdjustFont = displayToggle("AdjustFont", anchor.mAdjustFont);
        // 类似于EditorGUILayout.EnumPopup这种方式显示属性是用于可以在编辑器修改,并且触发指定逻辑
        var anchorMode = (ANCHOR_MODE)displayEnum("AnchorMode", anchor.mAnchorMode);

        if (anchorMode != anchor.mAnchorMode)
        {
            anchor.setAnchorModeInEditor(anchorMode);
        }
        var relativePos = displayToggle("RelativePosition", anchor.mRelativeDistance);

        if (relativePos != anchor.mRelativeDistance)
        {
            anchor.setRelativeDistanceInEditor(relativePos);
        }
        // 只是停靠到父节点的某个位置
        if (anchor.mAnchorMode == ANCHOR_MODE.AM_PADDING_PARENT_SIDE)
        {
            var horizontalPadding = (HORIZONTAL_PADDING_SIDE)displayEnum("HorizontalPaddingSide", anchor.mHorizontalNearSide);
            if (horizontalPadding != anchor.mHorizontalNearSide)
            {
                anchor.setHorizontalNearSideInEditor(horizontalPadding);
            }
            var verticalPadding = (VERTICAL_PADDING_SIDE)displayEnum("VerticalPaddingSide", anchor.mVerticalNearSide);
            if (verticalPadding != anchor.mVerticalNearSide)
            {
                anchor.setVerticalNearSideInEditor(verticalPadding);
            }
            // HPS_CENTER模式下才会显示mHorizontalPosition
            if (anchor.mHorizontalNearSide == HORIZONTAL_PADDING_SIDE.HPS_CENTER)
            {
                // displayProperty用于只是简单的使用默认方式显示属性,用于编辑器中直接修改值,不能触发任何其他逻辑
                displayProperty("mHorizontalPositionRelative", "HorizontalPositionRelative");
                displayProperty("mHorizontalPositionAbsolute", "HorizontalPositionAbsolute");
            }
            // VPS_CENTER模式下才会显示mVerticalPosition
            if (anchor.mVerticalNearSide == VERTICAL_PADDING_SIDE.VPS_CENTER)
            {
                displayProperty("mVerticalPositionRelative", "VerticalPositionRelative");
                displayProperty("mVerticalPositionAbsolute", "VerticalPositionAbsolute");
            }
            // 显示边距离参数
            if (anchor.mHorizontalNearSide != HORIZONTAL_PADDING_SIDE.HPS_CENTER ||
                anchor.mVerticalNearSide != VERTICAL_PADDING_SIDE.VPS_CENTER)
            {
                displayProperty("mDistanceToBoard", "DistanceToBoard");
            }
        }
        else
        {
            displayProperty("mAnchorPoint", "AnchorPoint");
        }
        serializedObject.ApplyModifiedProperties();
    }
Beispiel #2
0
    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);
    }
Beispiel #3
0
    //-------------------------------------------------------------------------------------------------------------------
    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);
        }
    }
    public override void OnInspectorGUI()
    {
        serializedObject.Update();
        anchor             = target as PaddingAnchor;
        anchor.mAdjustFont = displayToggle("AdjustFont", anchor.mAdjustFont, out bool adjustFontModified);
        // 类似于EditorGUILayout.EnumPopup这种方式显示属性是用于可以在编辑器修改,并且触发指定逻辑
        var anchorMode = (ANCHOR_MODE)displayEnum("AnchorMode", anchor.mAnchorMode, out bool anchorModeModified);

        if (anchorMode != anchor.mAnchorMode)
        {
            anchor.setAnchorModeInEditor(anchorMode);
        }
        var relativePos = displayToggle("RelativePosition", anchor.mRelativeDistance, out bool relativePosModified);

        if (relativePos != anchor.mRelativeDistance)
        {
            anchor.setRelativeDistanceInEditor(relativePos);
        }
        // 只是停靠到父节点的某个位置
        bool horiPaddingModified = false;
        bool vertPaddingModified = false;

        if (anchor.mAnchorMode == ANCHOR_MODE.PADDING_PARENT_SIDE)
        {
            var horizontalPadding = (HORIZONTAL_PADDING)displayEnum("HorizontalPaddingSide", anchor.mHorizontalNearSide, out horiPaddingModified);
            if (horizontalPadding != anchor.mHorizontalNearSide)
            {
                anchor.setHorizontalNearSideInEditor(horizontalPadding);
            }
            var verticalPadding = (VERTICAL_PADDING)displayEnum("VerticalPaddingSide", anchor.mVerticalNearSide, out vertPaddingModified);
            if (verticalPadding != anchor.mVerticalNearSide)
            {
                anchor.setVerticalNearSideInEditor(verticalPadding);
            }
            // HPS_CENTER模式下才会显示mHorizontalPosition
            if (anchor.mHorizontalNearSide == HORIZONTAL_PADDING.CENTER)
            {
                // displayProperty用于只是简单的使用默认方式显示属性,用于编辑器中直接修改值,不能触发任何其他逻辑
                displayProperty("mHorizontalPositionRelative", "HorizontalPositionRelative");
                displayProperty("mHorizontalPositionAbsolute", "HorizontalPositionAbsolute");
            }
            // VPS_CENTER模式下才会显示mVerticalPosition
            if (anchor.mVerticalNearSide == VERTICAL_PADDING.CENTER)
            {
                displayProperty("mVerticalPositionRelative", "VerticalPositionRelative");
                displayProperty("mVerticalPositionAbsolute", "VerticalPositionAbsolute");
            }
            // 显示边距离参数
            if (anchor.mHorizontalNearSide != HORIZONTAL_PADDING.CENTER ||
                anchor.mVerticalNearSide != VERTICAL_PADDING.CENTER)
            {
                displayProperty("mDistanceToBoard", "DistanceToBoard");
            }
        }
        else
        {
            displayProperty("mAnchorPoint", "AnchorPoint");
        }
        // 由于不确定hasModifiedProperties在ApplyModifiedProperties以后是否会改变,所以预先获取
        bool dirty = serializedObject.hasModifiedProperties ||
                     adjustFontModified ||
                     anchorModeModified ||
                     relativePosModified ||
                     horiPaddingModified ||
                     vertPaddingModified;

        serializedObject.ApplyModifiedProperties();
        if (dirty)
        {
            EditorUtility.SetDirty(target);
        }
    }