Ejemplo n.º 1
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
        }
Ejemplo n.º 2
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);
                }
            }
        }
Ejemplo n.º 3
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
        }