Пример #1
0
        public static CButton CreateImgButton(string path, string text, int depth, int x, int y, Transform parent, int w = 0, int h = 0, UIEventListener.VoidDelegate OnClick = null, string name = "Button", string toolTip = null)
        {
            GameObject obj = UnityEngine.Object.Instantiate(FuncUtil.GetUIAssetByPath(path)) as GameObject;

            if (obj == null)
            {
                return(null);
            }
            obj.name                    = name;
            obj.transform.parent        = parent;
            obj.transform.localScale    = Vector3.one;
            obj.transform.localPosition = new Vector3(x, y, 0);
            CButton  btn = obj.GetComponent <CButton>();
            UISprite sp  = btn.GetComponent <UISprite>();

            sp.depth = depth;
            if (w > 0 || h > 0)
            {
                sp.width  = w;
                sp.height = h;
            }
            if (toolTip != null)
            {
                btn.AddToolTip(toolTip);
            }
            if (OnClick != null)
            {
                UIEventListener.Get(obj).onClick = OnClick;
            }
            return(btn);
        }
Пример #2
0
        public static CButton CreateButton(string text, int depth, int x, int y, Transform parent, int w = 0, int h = 0, UIEventListener.VoidDelegate OnClick = null, string name = "Button", string toolTip = null)
        {
            GameObject obj = UnityEngine.Object.Instantiate(FuncUtil.GetUIAssetByPath(BUTTON_URL)) as GameObject;

            if (obj == null)
            {
                return(null);
            }
            obj.name                    = name;
            obj.transform.parent        = parent;
            obj.transform.localScale    = Vector3.one;
            obj.transform.localPosition = new Vector3(x, y, 0);
            CButton btn = obj.GetComponent <CButton>();

            btn.Text = text ?? "";
            UISprite sp = btn.GetComponent <UISprite>();

            btn.Label.SetAnchor(btn.transform);
            btn.Label.leftAnchor.Set(0, -38);
            btn.Label.rightAnchor.Set(1, 38);
            btn.Label.topAnchor.Set(1, 32);
            btn.Label.bottomAnchor.Set(0, -40);
            sp.depth = depth;
            if (w > 0 || h > 0)
            {
                sp.width  = w;
                sp.height = h;
            }
            if (toolTip != null)
            {
                btn.AddToolTip(toolTip);
            }
            btn.Label.depth = 200; //整个界面的文字基本不会重叠,搞成同一层避免隔开具有相同图集的其他组件的深度
            if (OnClick != null)
            {
                //UIEventListener.Get(obj).onClick = OnClick;
                btn.AddClick(OnClick);
            }
            return(btn);
        }