示例#1
0
        /// <summary>
        /// 获取指定UI类的字段信息
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        private static UIFieldsInfo GetUIFieldsInfo(Type type)
        {
            UIFieldsInfo uIFieldsInfo;

            if (s_uiFieldsCache.TryGetValue(type, out uIFieldsInfo))
            {
                return(uIFieldsInfo);
            }

            uIFieldsInfo = new UIFieldsInfo()
            {
                type = type
            };
            FieldInfo[] fis = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
            for (int i = 0, imax = fis.Length; i < imax; i++)
            {
                FieldInfo fi = fis[i];

                if (fi.IsDefined(typeof(ControlBindingAttribute), false))
                {
                    uIFieldsInfo.controls.Add(fi);
                }
                else if (fi.IsDefined(typeof(SubUIBindingAttribute), false))
                {
                    uIFieldsInfo.subUIs.Add(fi);
                }
            }

            s_uiFieldsCache.Add(type, uIFieldsInfo);

            return(uIFieldsInfo);
        }
示例#2
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
        }
示例#3
0
        /// <summary>
        /// 解除指定UI及其子节点自动绑定字段的引用
        /// </summary>
        /// <param name="uiGo"></param>
        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 <IBindableUI> > bindUIRefs = controlData.bindUIRefs;
                for (int j = 0, jmax = bindUIRefs.Count; j < jmax; j++)
                {
                    WeakReference <IBindableUI> bindUIRef = bindUIRefs[j];
                    IBindableUI bindUI;
                    if (!bindUIRef.TryGetTarget(out bindUI))
                    {
                        continue;
                    }

                    CBLuaPanel luaViewRunner = bindUI as CBLuaPanel;
                    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._table;
                        if (luaTable == null)
                        {
                            continue;
                        }

                        List <CtrlItemData> ctrlItemData = controlData.ctrlItemDatas;
                        for (int k = 0, kmax = ctrlItemData.Count; k < kmax; k++)
                        {
                            CtrlItemData itemData = ctrlItemData[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("UnBindUI {0} 耗时{1}ms", uiGo.Name, span * 1000f);
            }
#endif
        }