NGUI 代码布局工具

ngui好像是没有布局工具的,今天有点闲暇,就写了一个比较粗糙的布局工具,可以代码设定控件


这个是程序运行前的样子

然后。。。运行之后是。。。


背景图片是设置了填充,x是顶部对齐,y是左对齐,那个鄙视的表情是左边缘和指定控件右边缘对齐,b是底部对齐,螺丝是右对齐,A是水平、垂直居中和padding200,生气的表情是水平、垂直居中


其中,设定的脚本写法如图:


用法基本是就是先构造填充键值字典,键是一个枚举,值是指定格式字符串,大部分枚举不需要值。

下面上代码:

工具代码:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;

namespace yang.UI {
    public enum Layout { 

        toLeftOf = 1,  //控件右边缘和指定控件左边缘对齐
        toRightOf = 2, //控件左边缘和指定控件右边缘对齐
        toTopOf = 3,   //控件下边缘和指定控件上边缘对齐
        toBottomOf = 4,//控件上边缘和指定控件下边缘对齐

        alignParentBottom = 5, //控件与指定控制底部对齐
        alignParentLeft = 6,   //控件与指定控制左边对齐
        alignParentRight = 7,  //控件与指定控制右边对齐
        alignParentTop = 8,    //控件与指定控制顶部对齐
        alignParentBaseLineX = 9,   //控件与指定控件中心X对齐
        alignParentBaseLineY = 10,   //控件与指定控件中心Y对齐

        fillParent = 11,        //填充父物体

        width = 12,            //宽度
        height = 13,           //高度

        padding = 14,          //边距 value = "上,右,下,左";

        centerHorizontal = 15, //水平居中在指定控件上
        centerVertical = 16,   //垂直居中在指定控件上
    }
    /// <summary>
    /// NGUI控件布局工具
    /// </summary>
    public class UILayoutTool {
        //用字典参数布局一个UI控件
        public static UIWidget LayoutUI(UIWidget widget, UIRect btarget, Dictionary<Layout, string> param) {
            #region 一些预先计算
            float dir_bottm = 0f;
            float dir_cent = 0.5f;
            float dir_top = 1f;
            float dir_left = 0f;
            float dir_right = 1f;
            UIWidget target = (UIWidget)btarget;
            UIPanel ptarget = null;
            UIRect ftarget;
            if (btarget == null) {
                if (widget.parent.GetType() == typeof(UIPanel)) {
                    ptarget = (UIPanel)widget.parent;
                } else {
                    target = (UIWidget)widget.parent;
                }
            }
            UIWidget.Pivot tarCentType = UIWidget.Pivot.Center;
            Vector4 tarSize;
            Vector3 tarCent;
            Vector3 tarTop, tarRight, tarBottom, tarLeft;//目标上右下左局部坐标
            Vector3 halfX, halfY;//目标半长
            if (target != null) {
                tarCentType = target.pivot;//目标中心类型
                target.pivot = UIWidget.Pivot.Center;
                tarSize = target.drawingDimensions;//目标范围
                tarCent = target.transform.localPosition;//目标中心
                halfX = new Vector3((tarSize.z - tarSize.x) / 2.0f, 0, 0);
                halfY = new Vector3(0, (tarSize.w - tarSize.y) / 2.0f, 0);
                ftarget = target;
            } else {
                tarCent = ptarget.transform.localPosition;
                halfX = new Vector3(ptarget.width / 2.0f, 0, 0);
                halfY = new Vector3(0, ptarget.height/ 2.0f, 0);
                ftarget = ptarget;
            }

            tarTop = tarCent + halfY;
            tarBottom = tarCent - halfY;
            tarLeft = tarCent - halfX;
            tarRight = tarCent +  halfX;
            #endregion
            //下面开始使用参数进行布局
            Transform wt = widget.transform;
            //宽
            if (param.ContainsKey(Layout.width)) {
                widget.SetDimensions( Int32.Parse(param[Layout.width]),widget.height);
            }
            //高
            if (param.ContainsKey(Layout.height)) {
                widget.SetDimensions(widget.width,Int32.Parse(param[Layout.height]));
            }
            //居左或右侧
            if (param.ContainsKey(Layout.toLeftOf)) {
                widget.pivot = UIWidget.Pivot.Right;
                widget.transform.localPosition = new Vector3(tarLeft.x, wt.localPosition.y, 0);
            }else if(param.ContainsKey(Layout.toRightOf)){
                widget.pivot = UIWidget.Pivot.Left;
                widget.transform.localPosition = new Vector3(tarRight.x, wt.localPosition.y, 0);
            }
            //居上或下侧
            if (param.ContainsKey(Layout.toTopOf)) {
                widget.pivot = UIWidget.Pivot.Bottom;
                widget.transform.localPosition = new Vector3(wt.localPosition.x, tarTop.y, 0);
            } else if (param.ContainsKey(Layout.toBottomOf)) {
                widget.pivot = UIWidget.Pivot.Top;
                widget.transform.localPosition = new Vector3(wt.localPosition.x, tarBottom.y, 0);
            }
            //左或右对齐
            if (param.ContainsKey(Layout.alignParentLeft)) {
                widget.pivot = UIWidget.Pivot.Left;
                widget.transform.localPosition = new Vector3(tarLeft.x, wt.localPosition.y, 0);
            }else if(param.ContainsKey(Layout.alignParentRight)){
                widget.pivot = UIWidget.Pivot.Right;
                widget.transform.localPosition = new Vector3(tarRight.x, wt.localPosition.y, 0);
            }
            //上或下对齐
            if (param.ContainsKey(Layout.alignParentTop)) {
                widget.pivot = UIWidget.Pivot.Top;
                widget.transform.localPosition = new Vector3(wt.localPosition.x, tarTop.y, 0);
            } else if (param.ContainsKey(Layout.alignParentBottom)) {
                widget.pivot = UIWidget.Pivot.Bottom;
                widget.transform.localPosition = new Vector3(wt.localPosition.x, tarBottom.y, 0);
            }
            //中心X对齐
            if (param.ContainsKey(Layout.alignParentBaseLineX)) {
                widget.transform.localPosition = new Vector3(tarCent.x,wt.localPosition.y,0);
            }
            //中心Y对齐
            if (param.ContainsKey(Layout.alignParentBaseLineY)) {
                widget.transform.localPosition = new Vector3(wt.localPosition.x, tarCent.y, 0);
            }
            //填充
            if (param.ContainsKey(Layout.fillParent)) {
                widget.SetAnchor(ftarget.transform);
                widget.topAnchor.Set(dir_top,0);
                widget.rightAnchor.Set(dir_right,0);
                widget.bottomAnchor.Set(dir_bottm,0);
                widget.leftAnchor.Set(dir_left,0);
            }
            //边距
            if (param.ContainsKey(Layout.padding)) {
                string[] padding = param[Layout.padding].Split(',');
                widget.SetAnchor(ftarget.transform);
                widget.topAnchor.Set(dir_top,-Int32.Parse(padding[0]));
                widget.rightAnchor.Set(dir_right,-Int32.Parse(padding[1]));
                widget.bottomAnchor.Set(dir_bottm,Int32.Parse(padding[2]));
                widget.leftAnchor.Set(dir_left,Int32.Parse(padding[3]));
            }
            //水平居中在目标上
            if (param.ContainsKey(Layout.centerHorizontal)) {
                widget.pivot = UIWidget.Pivot.Center;
                widget.transform.localPosition = new Vector3(tarCent.x, wt.localPosition.y, 0);
            }
            //垂直居中在目标上
            if (param.ContainsKey(Layout.centerVertical)) {
                widget.pivot = UIWidget.Pivot.Center;
                widget.transform.localPosition = new Vector3(wt.localPosition.x, tarCent.y, 0);
            }
            //
            if(target!=null)
                target.pivot = tarCentType;
            return widget;
        }
    }
}

用法代码:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using yang.UI;

public class UI_test : MonoBehaviour {

    public UIWidget[] wi;
	// Use this for initialization
	void Start () {
        Dictionary<Layout, string> s1 = new Dictionary<Layout, string>();
        s1.Add(Layout.fillParent, "");
        UILayoutTool.LayoutUI(wi[0], null, s1);

        Dictionary<Layout, string> s2 = new Dictionary<Layout, string>();
        s2.Add(Layout.fillParent, "");
        s2.Add(Layout.padding, "200,200,200,200");
        UILayoutTool.LayoutUI(wi[1], null, s2);

        Dictionary<Layout, string> s3 = new Dictionary<Layout, string>();
        s3.Add(Layout.width, "50");
        s3.Add(Layout.height,"50");
        s3.Add(Layout.alignParentBottom, "");
        s3.Add(Layout.centerHorizontal, "");
        UILayoutTool.LayoutUI(wi[2], null, s3);

        Dictionary<Layout, string> s4 = new Dictionary<Layout, string>();
        s4.Add(Layout.width, "50");
        s4.Add(Layout.height, "50");
        s4.Add(Layout.alignParentTop, "");
        s4.Add(Layout.centerHorizontal, "");
        UILayoutTool.LayoutUI(wi[3], null, s4);

        Dictionary<Layout, string> s5 = new Dictionary<Layout, string>();
        s5.Add(Layout.width, "50");
        s5.Add(Layout.height, "50");
        s5.Add(Layout.alignParentLeft, "");
        s5.Add(Layout.centerVertical, "");
        UILayoutTool.LayoutUI(wi[4], null, s5);

        Dictionary<Layout, string> s6 = new Dictionary<Layout, string>();
        s6.Add(Layout.width, "100");
        s6.Add(Layout.height, "100");
        s6.Add(Layout.centerHorizontal, "");
        s6.Add(Layout.centerVertical, "");
        UILayoutTool.LayoutUI(wi[5], null, s6);

        Dictionary<Layout, string> s7 = new Dictionary<Layout, string>();
        s7.Add(Layout.width, "50");
        s7.Add(Layout.height, "50");
        s7.Add(Layout.alignParentRight, "");
        s7.Add(Layout.centerVertical, "");
        UILayoutTool.LayoutUI(wi[6], null, s7);

        Dictionary<Layout, string> s8 = new Dictionary<Layout, string>();
        s8.Add(Layout.width, "50");
        s8.Add(Layout.height, "50");
        s8.Add(Layout.toRightOf, "");
        s8.Add(Layout.alignParentBaseLineY, "");
        UILayoutTool.LayoutUI(wi[7], wi[4], s8);
	}
}

其中,使用的那个脚本需要引用被布局的所有控件(拖在数组里)


好了,就是这样了,虽然说鼠标点anchor也很精确,但是控件相互之间位置必竟还是算起来麻烦,不过这个也写得很简陋,功能还可以加一些

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值