Пример #1
0
        private void BindCtrl(IBindableUI ui, FieldInfo fi)
        {
            int itemIdx = GetCtrlIndex(fi.Name);

            if (itemIdx == -1)
            {
                Debug.LogErrorFormat("can not find binding control of var [{0}]", fi.Name);
                return;
            }

            var objs = ctrlItemDatas[itemIdx];

            Type fieldType = fi.FieldType;

            if (fieldType.IsArray)
            {
                Array arrObj = Array.CreateInstance(fieldType.GetElementType(), objs.targets.Length);

                // 给数组元素设置数据
                for (int j = 0, jmax = objs.targets.Length; j < jmax; j++)
                {
                    arrObj.SetValue(objs.targets[j], j);
                }
                fi.SetValue(ui, arrObj);
            }
            else
            {
                UnityEngine.Object component = GetComponent(itemIdx);
                fi.SetValue(ui, component);
            }
        }
Пример #2
0
        public void BindDataToLua(IBindableUI ui, LuaTable luaTable)
        {
            if (luaTable == null)
            {
                return;
            }

            foreach (var itemData in ctrlItemDatas)
            {
                var targets = itemData.targets;
                if (targets.Length == 0)
                {
                    Debug.LogErrorFormat("control {0} is null", itemData.name);
                    continue;
                }

                if (targets.Length == 1)
                {
                    if (targets[0] != null)
                    {
                        luaTable.Set(itemData.name, itemData.targets[0]);
                    }
                    else
                    {
                        Debug.LogErrorFormat("Component {0} is null", itemData.name);
                    }
                }
                else
                {
                    LuaTable tmpTbl = luaTable.env.NewTable();
                    for (int i = 0, imax = targets.Length; i < imax; i++)
                    {
                        if (targets[i] != null)
                        {
                            tmpTbl.Set(i + 1, targets[i]);
                        }
                        else
                        {
                            Debug.LogErrorFormat("Component {0}[{1}] is null", itemData.name, i);
                        }
                    }

                    luaTable.Set(itemData.name, tmpTbl);
                }
            }

            foreach (var subUI in subUIItemDatas)
            {
                luaTable.Set(subUI.name, subUI.subUIData);
            }

            if (bindUIRefs == null)
            {
                bindUIRefs = new List <WeakReference <IBindableUI> >();
            }

            bindUIRefs.Add(new WeakReference <IBindableUI>(ui));
        }
Пример #3
0
        private void BindCtrl(IBindableUI ui, FieldInfo fi)
        {
            int itemIdx = GetCtrlIndex(fi.Name);

            if (itemIdx == -1)
            {
                if (!_ignoreNotFindBindingControlErrorTips)
                {
                    Debug.LogErrorFormat("can not find binding control of var [{0}]", fi.Name);
                }
                return;
            }

            var objs = ctrlItemDatas[itemIdx];

            Type fieldType = fi.FieldType;

            if (fieldType.IsArray)
            {
                Array arrObj = Array.CreateInstance(fieldType.GetElementType(), objs.targets.Length);

                // 给数组元素设置数据
                for (int j = 0, jmax = objs.targets.Length; j < jmax; j++)
                {
                    if (objs.targets[j] != null)
                    {
                        arrObj.SetValue(objs.targets[j], j);
                    }
                    else
                    {
                        Debug.LogErrorFormat("componet {0} [{1}] is null", objs.name, j);
                    }
                }
                fi.SetValue(ui, arrObj);
            }
            else
            {
                UnityEngine.Object component = GetComponent(itemIdx);
                try
                {
                    if (component != null)
                    {
                        fi.SetValue(ui, component);
                    }
                    else
                    {
                        Debug.LogErrorFormat("componet {0}  is null", objs.name);
                    }
                }
                catch (Exception e)
                {
                    UnityEngine.Debug.LogErrorFormat("Bind componet to Fied {0} fail!,msg:{1}", objs.name, e.StackTrace);
                }
            }
        }
Пример #4
0
        private void BindSubUI(IBindableUI ui, FieldInfo fi)
        {
            int subUIIdx = GetSubUIIndex(fi.Name);

            if (subUIIdx == -1)
            {
                Debug.LogErrorFormat("can not find binding subUI of var [{0}]", fi.Name);
                return;
            }

            fi.SetValue(ui, subUIItemDatas[subUIIdx].subUIData);
        }
Пример #5
0
    void Start()
    {
        // TODO get config from xml
        IBindableUI   uiA      = Activator.CreateInstance(Type.GetType("UIA")) as IBindableUI;
        GameObject    prefab   = Resources.Load <GameObject>("UI/UIA");
        GameObject    go       = Instantiate(prefab);
        UIControlData ctrlData = go.GetComponent <UIControlData>();

        if (ctrlData != null)
        {
            ctrlData.BindDataTo(uiA);
        }

        (uiA as UIA).CheckBinding();
    }
Пример #6
0
        /// <summary>
        /// 将当前数据绑定到某窗口类实例的字段,UI 加载后必须被执行
        /// </summary>
        /// <param name="ui">需要绑定数据的 UI</param>
        public void BindDataTo(IBindableUI ui)
        {
            if (ui == null)
            {
                return;
            }

#if DEBUG_LOG
            float time = Time.realtimeSinceStartup;
            Profiler.BeginSample("BindDataTo");
#endif
            UIFieldsInfo fieldInfos = GetUIFieldsInfo(ui.GetType());

            var controls = fieldInfos.controls;
            for (int i = 0, imax = controls.Count; i < imax; i++)
            {
                BindCtrl(ui, controls[i]);
            }

            var subUIs = fieldInfos.subUIs;
            for (int i = 0, imax = subUIs.Count; i < imax; i++)
            {
                BindSubUI(ui, subUIs[i]);
            }

            if (bindUIRefs == null)
            {
                bindUIRefs = new List <WeakReference <IBindableUI> >();
            }

            bindUIRefs.Add(new WeakReference <IBindableUI>(ui));

#if DEBUG_LOG
            Profiler.EndSample();
            float span = Time.realtimeSinceStartup - time;
            if (span > 0.002f)
            {
                Debug.LogWarningFormat("BindDataTo {0} 耗时{1}ms", ui.GetType().Name, span * 1000f);
            }
#endif
        }
Пример #7
0
        /// <summary>
        /// 将当前数据绑定到某窗口类实例的字段,UI 加载后必须被执行
        /// </summary>
        /// <param name="ui">需要绑定数据的 UI</param>
        public void BindDataTo(IBindableUI ui)
        {
            if (ui == null)
            {
                return;
            }

            FieldInfo[] fis = ui.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);
            for (int i = 0, imax = fis.Length; i < imax; i++)
            {
                FieldInfo fi = fis[i];

                if (fi.GetCustomAttributes(typeof(ControlBindingAttribute), false).Length != 0)
                {
                    BindCtrl(ui, fi);
                }
                else if (fi.GetCustomAttributes(typeof(SubUIBindingAttribute), false).Length != 0)
                {
                    BindSubUI(ui, fi);
                }
            }
        }
Пример #8
0
        public static void UnBindUI(GameObject uiGo)
        {
            if (uiGo == null)
            {
                return;
            }
#if DEBUG_LOG
            float time = Time.realtimeSinceStartup;
            Profiler.BeginSample("UnBindUI");
#endif
            uiGo.GetComponentsInChildren(true, s_tmpControlDataForUnbind);
            for (int i = 0, imax = s_tmpControlDataForUnbind.Count; i < imax; i++)
            {
                UIControlData controlData = s_tmpControlDataForUnbind[i];
                if (controlData.bindUIRefs == null)
                {
                    continue;
                }
                List <WeakReference> bindUIRefs = controlData.bindUIRefs;
                for (int j = 0, jmax = bindUIRefs.Count; j < jmax; j++)
                {
                    WeakReference bindUIRef = bindUIRefs[j];
                    IBindableUI   bindUI    = bindUIRef.Target as IBindableUI;
                    if (bindUI == null)
                    {
                        continue;
                    }
                    LuaViewRunner luaViewRunner = bindUI as LuaViewRunner;
                    if (luaViewRunner == null)
                    {
                        UIFieldsInfo fieldInfos = GetUIFieldsInfo(bindUI.GetType());
                        var          controls   = fieldInfos.controls;
                        for (int k = 0, kmax = controls.Count; k < kmax; k++)
                        {
                            controls[k].SetValue(bindUI, null);
                        }
                        var subUIs = fieldInfos.subUIs;
                        for (int k = 0, kmax = subUIs.Count; k < kmax; k++)
                        {
                            subUIs[k].SetValue(bindUI, null);
                        }
                    }
                    else
                    {
                        LuaTable luaTable = luaViewRunner.luaUI;
                        if (luaTable == null)
                        {
                            continue;
                        }
                        List <CtrlItemData> ctrlItemDatas = controlData.ctrlItemDatas;
                        for (int k = 0, kmax = ctrlItemDatas.Count; k < kmax; k++)
                        {
                            CtrlItemData itemData = ctrlItemDatas[k];
                            luaTable.Set <string, object>(itemData.name, null);
                        }

                        List <SubUIItemData> subUIItemDatas = controlData.subUIItemDatas;
                        for (int k = 0, kmax = subUIItemDatas.Count; k < kmax; k++)
                        {
                            SubUIItemData subUIItemData = subUIItemDatas[k];
                            luaTable.Set <string, object>(subUIItemData.name, null);
                        }
                    }
                }
                controlData.bindUIRefs = null;
            }
            s_tmpControlDataForUnbind.Clear();
#if DEBUG_LOG
            Profiler.EndSample();
            float span = Time.realtimeSinceStartup - time;
            if (span > 0.002f)
            {
                Debug.LogWarningFormat("BindDataTo {0} 耗时{1}ms", ui.GetType().Name, span * 1000f);
            }
#endif
        }
Пример #9
0
        /// <summary>
        /// 加载指定名称的“UI窗体”
        /// 功能:
        ///    1:根据“UI窗体名称”,加载预设克隆体。
        ///    2:根据不同预设克隆体中带的脚本中不同的“位置信息”,加载到“根窗体”下不同的节点。
        ///    3:隐藏刚创建的UI克隆体。
        ///    4:把克隆体,加入到“所有UI窗体”(缓存)集合中。
        ///
        /// </summary>
        /// <param name="uiFormName">UI窗体名称</param>
        private BaseUIForm LoadUIForm(string uiFormName)
        {
            string     strUIFormPaths   = uiFormName;       //UI窗体路径
            GameObject goCloneUIPrefabs = null;             //创建的UI克隆体预设
            BaseUIForm baseUiForm       = null;             //窗体基类


            //根据UI窗体名称,得到对应的加载路径
            //_DicFormsPaths.TryGetValue(uiFormName, out strUIFormPaths);
            //根据“UI窗体名称”,加载“预设克隆体”
            if (!string.IsNullOrEmpty(strUIFormPaths))
            {
                goCloneUIPrefabs = OpenUIPanel(strUIFormPaths);
            }

            UIControlData ctrlData = goCloneUIPrefabs.GetComponent <UIControlData>();
            CBLuaPanel    luaPanel = GetLuaPanel(uiFormName, ctrlData);

            if (luaPanel == null)
            {
                //C# 脚本逻辑
                IBindableUI uiA = Activator.CreateInstance(Type.GetType(uiFormName)) as IBindableUI;
                baseUiForm = uiA as BaseUIForm;
            }
            else
            {
                //Lua 逻辑
                baseUiForm = luaPanel as BaseUIForm;
            }
            baseUiForm.Source = goCloneUIPrefabs;
            if (ctrlData != null)
            {
                ctrlData.BindDataTo(baseUiForm);
            }
            //设置“UI克隆体”的父节点(根据克隆体中带的脚本中不同的“位置信息”)
            if (_TraCanvasTransfrom != null && goCloneUIPrefabs != null)
            {
                //baseUiForm = goCloneUIPrefabs.GetComponent<BaseUIForm>();
                if (baseUiForm == null)
                {
                    Debug.Log("baseUiForm==null! ,请先确认窗体预设对象上是否加载了baseUIForm的子类脚本! 参数 uiFormName=" + uiFormName);
                    return(null);
                }
                baseUiForm.OnReady();

                switch (baseUiForm.CurrentUIType.UIForms_Type)
                {
                case UIFormType.Normal:                     //普通窗体节点
                    goCloneUIPrefabs.transform.SetParent(_TraNormal, false);
                    break;

                case UIFormType.Fixed:                      //固定窗体节点
                    goCloneUIPrefabs.transform.SetParent(_TraFixed, false);
                    break;

                case UIFormType.PopUp:                      //弹出窗体节点
                    goCloneUIPrefabs.transform.SetParent(_TraPopUp, false);
                    break;

                default:
                    break;
                }

                //设置隐藏
                goCloneUIPrefabs.SetActive(false);
                //把克隆体,加入到“所有UI窗体”(缓存)集合中。
                _DicALLUIForms.Add(uiFormName, baseUiForm);
                return(baseUiForm);
            }
            else
            {
                Debug.Log("_TraCanvasTransfrom==null Or goCloneUIPrefabs==null!! ,Plese Check!, 参数uiFormName=" + uiFormName);
            }

            Debug.Log("出现不可以预估的错误,请检查,参数 uiFormName=" + uiFormName);
            return(null);
        }//Mehtod_end