// 通过数量初始化
        private void InitCount(int count)
        {
            if (IsInitedCount)
            {
                CLog.Error("InitCount 无法初始化2次!!!! " + Path);
                return;
            }
            IsInitedCount = true;
            GOs.Clear();
            for (int i = 0; i < Trans.childCount; ++i)
            {
                Transform temp = Trans.GetChild(i);
                var       ele  = temp.GetComponent <LayoutElement>();
                if (ele != null && ele.ignoreLayout)
                {
                    continue;
                }
                var ignore = temp.GetComponent <UIgnore>();
                if (ignore != null)
                {
                    continue;
                }
                GOs.Add(temp.gameObject);
            }
            if (Prefab == null && GOs.Count > 0)
            {
                Prefab = GOs[0];
            }
            if (Prefab == null)
            {
                CLog.Error("{0}: Prefab == null", Path);
                return;
            }
            if (Prefab.name.StartsWith(Const.STR_Base))
            {
                CLog.Error($"不能使用基础UI Prefab 初始化:{Prefab.name}");
            }

            //差值
            int subCount = count - GOs.Count;

            if (subCount > 0)
            {
                //生成剩余的游戏对象
                for (int i = 0; i < subCount; ++i)
                {
                    GameObject temp = GameObject.Instantiate(Prefab, this.RectTrans.position, this.RectTrans.rotation);
                    (temp.transform as RectTransform).SetParent(this.RectTrans);
                    (temp.transform as RectTransform).localScale = Vector3.one;
                    GOs.Add(temp);
                }
            }

            Controls.Clear();
            for (int i = 0; i < GOs.Count; ++i)
            {
                var tempPresenter = GOs[i].GetComponent <UControl>();
                if (tempPresenter == null)
                {
                    CLog.Error("Item:没有添加组件");
                    return;
                }
                Controls.Add(tempPresenter);

                if (i < count)
                {
                    tempPresenter.Show(true);
                }
                else
                {
                    tempPresenter.Show(false);
                }

                if (tempPresenter is UCheckBox checkBox)
                {
                    ToggleGroupCheckBoxs.Add(checkBox);
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// 通过数量初始化
        /// </summary>
        /// <param name="count"></param>
        public void InitCount <TP, TD>(int count) where TP : Presenter <TD>, new() where TD : PresenterData, new()
        {
            if (IsInitedCount)
            {
                CLog.Error("InitCount 无法初始化2次!!!!");
                return;
            }
            IsInitedCount = true;
            GOs.Clear();
            for (int i = 0; i < Trans.childCount; ++i)
            {
                Transform temp = Trans.GetChild(i);
                var       ele  = temp.GetComponent <LayoutElement>();
                if (ele != null && ele.ignoreLayout)
                {
                    continue;
                }
                GOs.Add(temp.gameObject);
                temp.gameObject.SetActive(false);
            }
            if (Prefab == null && GOs.Count > 0)
            {
                Prefab = GOs[0];
            }
            if (Prefab == null)
            {
                CLog.Error("{0}: Prefab == null", Path);
                return;
            }
            if (count <= 0)
            {
                CLog.Error("Count <= 0");
            }
            if (Prefab.name.StartsWith(BaseConstMgr.STR_Base))
            {
                CLog.Error($"不能使用基础UI Prefab 初始化:{Prefab.name}");
            }

            //差值
            int subCount = count - GOs.Count;

            if (subCount > 0)
            {
                //生成剩余的游戏对象
                for (int i = 0; i < subCount; ++i)
                {
                    GameObject temp = GameObject.Instantiate(Prefab, this.RectTrans.position, this.RectTrans.rotation) as GameObject;
                    (temp.transform as RectTransform).SetParent(this.RectTrans);
                    (temp.transform as RectTransform).localScale = Vector3.one;
                    GOs.Add(temp);
                }
            }

            for (int i = 0; i < count; ++i)
            {
                GOs[i].SetActive(true);
                var tempPresenter = GOs[i].GetComponent <TP>();
                Presenters.Add(tempPresenter);

                if (tempPresenter is BaseCheckBox checkBox)
                {
                    checkBox.IsToggleGroup = IsToggleGroup;
                    ToggleGroupCheckBoxs.Add(checkBox);
                }
            }

            //设置数量
            //Count = GOs.Count;
        }