Пример #1
0
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="props"></param>
        public void Destroy(APropsBase props)
        {
            var com = ContentMap[props];

            com.Destroy();
            ContentMap.Remove(props);
        }
Пример #2
0
        /// <summary>
        /// 设置数据
        /// </summary>
        /// <param name="propsBase"></param>
        public void SetProps(APropsBase propsBase)
        {
            var t = propsBase as T;

            if (t == null)
            {
                BDebug.LogError("类型转换失败:" + propsBase.GetType().Name);
            }
            else
            {
                this.SetProps(t);
            }
        }
        /// <summary>
        /// 设置组件绑定值
        /// </summary>
        /// <param name="transform"></param>
        /// <param name="newProps"></param>
        public void SetTransformProps(Transform transform, APropsBase newProps)
        {
            //第一次进行缓存绑定后,就不再重新解析了,
            //所以使用者要保证每次的 state尽量是一致的
            TransformBindData transformBindData;

            //获取是否有缓存
            if (!globalTransformBindCacheMap.TryGetValue(transform, out transformBindData))
            {
                transformBindData           = new TransformBindData();
                transformBindData.Transform = transform;
                //生成Bind信息
                transformBindData.FieldCacheMap        = BindTransformProps(transform, newProps);
                globalTransformBindCacheMap[transform] = transformBindData;
            }

            //修改field
            List <string> changedFieldList = AnalysisPropsChanged(transformBindData, newProps);

            //
            for (int j = 0; j < changedFieldList.Count; j++)
            {
                var fieldName = changedFieldList[j];
                var cf        = transformBindData.FieldCacheMap[fieldName];
                //TODO 这里考虑优化多次获取值的性能
                var newFieldValue = newProps.GetValue(fieldName);
                //执行赋值操作
                var comBindAdaptor = componentBindAdaptorMap[cf.Attribute.Type];
                if (cf.UIBehaviour != null) //UI操作
                {
                    comBindAdaptor.SetData(cf.UIBehaviour, cf.Attribute.FunctionName, newFieldValue);
                }
                else if (cf.Transform) //自定义逻辑管理
                {
                    comBindAdaptor.SetData(cf.Transform, cf.Attribute.FunctionName, newFieldValue);
                }
            }
        }
Пример #4
0
 /// <summary>
 /// 设置Component Props
 /// </summary>
 /// <param name="trans"></param>
 /// <param name="aState"></param>
 static public void SetComponentProps(Transform trans, APropsBase props)
 {
     ComponentBindAdaptorManager.Inst.SetTransformProps(trans, props);
 }
Пример #5
0
 /// <summary>
 /// 获取item
 /// </summary>
 /// <param name="index"></param>
 /// <returns></returns>
 public IComponent GetItem(APropsBase props)
 {
     return(ContentMap[props]);
 }
Пример #6
0
 /// <summary>
 /// 添加item
 /// </summary>
 public void AddItem(APropsBase propsBase, IComponent component)
 {
     component.Transform.SetParent(this.sr.content, false);
     this.ContentMap[propsBase] = component;
 }
        /// <summary>
        /// 分析Props修改
        /// </summary>
        /// <param name="transformBindData"></param>
        /// <param name="newProps"></param>
        /// <returns></returns>
        List <string> AnalysisPropsChanged(TransformBindData transformBindData, APropsBase newProps)
        {
            List <string> changedFiledList = new List <string>();

            //手动设置field模式
            if (newProps.IsMunalMarkMode)
            {
                if (newProps.IsChanged)
                {
                    changedFiledList.AddRange(newProps.GetChangedPropertise());
                }
            }
            else
            {
                //自动判断模式
                foreach (var item in transformBindData.FieldCacheMap)
                {
                    var fieldName     = item.Key;
                    var newFieldValue = newProps.GetValue(fieldName);

                    //旧数据为null 直接加入
                    if (item.Value.LastValue == null)
                    {
                        item.Value.LastValue = newFieldValue;
                        changedFiledList.Add(fieldName);
                        continue;
                    }
                    bool isChanged    = false;
                    var  newValueType = newFieldValue.GetType();
                    //开始新的对比判断
                    if (newValueType.IsValueType || newValueType.Namespace == "System") //内置类型处理
                    {
                        if (!newProps.Equals(item.Value.LastValue))
                        {
                            isChanged = true;
                        }
                    }
                    else if (newFieldValue is APropsBase) //成员属性尽量使用手动版本设置改变
                    {
                        var props = newFieldValue as APropsBase;
                        if (props.IsMunalMarkMode)
                        {
                            if (props.IsChanged)
                            {
                                isChanged = true;
                            }
                        }
                        else
                        {
                            //这里较慢,因为是全局列表更新
                            var transofrmdata =
                                globalTransformBindCacheMap.Values.FirstOrDefault((v) => v.Props == props);
                            if (transofrmdata == null)
                            {
                                isChanged = true;
                            }
                            else
                            {
                                //递归获取是否改变,一旦层数过多,效率不是很高,所以一般嵌套不要超过2层
                                var ret = AnalysisPropsChanged(transformBindData, props);
                                if (ret.Count > 0)
                                {
                                    isChanged = true;
                                }
                            }
                        }
                    }
                    else if (newFieldValue is IPropsList)
                    {
                        var comList = newFieldValue as IPropsList;
                        if (comList.IsChanged)
                        {
                            isChanged = true;
                        }
                    }
                    else
                    {
                        BDebug.LogError("可能不支持的Props字段类型:" + newValueType.FullName);
                    }

                    if (isChanged)
                    {
                        changedFiledList.Add(fieldName);

                        item.Value.LastValue = newFieldValue;
                    }
                }
            }

            return(changedFiledList);
        }
        /// <summary>
        /// bind Tansform和State的值,防止每次都修改
        /// </summary>
        /// <param name="transform"></param>
        /// <param name="props"></param>
        /// <returns></returns>
        private Dictionary <string, TransformBindData.ComponentFieldCahce> BindTransformProps(Transform transform,
                                                                                              APropsBase props)
        {
            var componentFieldCacheMap = new Dictionary <string, TransformBindData.ComponentFieldCahce>();
            //先初始化Props的成员信息
            var type = props.GetType();

            if (props.MemberinfoMap == null)
            {
                var memberInfoMap = StateFactory.GetMemberinfoCache(type);
                if (memberInfoMap == null)
                {
                    memberInfoMap = new Dictionary <string, MemberInfo>();
                    List <MemberInfo> memberInfoList = new List <MemberInfo>();
                    var flag = BindingFlags.Instance | BindingFlags.Public;
                    memberInfoList.AddRange(type.GetFields(flag));
                    memberInfoList.AddRange(type.GetProperties(flag));
                    //缓存所有属性
                    foreach (var mi in memberInfoList)
                    {
                        var attr = mi.GetAttributeInILRuntime <ComponentValueBindAttribute>();
                        if (attr != null)
                        {
                            memberInfoMap[mi.Name] = mi;
                        }
                    }

                    StateFactory.AddMemberinfoCache(type, memberInfoMap);
                }

                props.MemberinfoMap = memberInfoMap;
            }

            //进行值绑定
            foreach (var mi in props.MemberinfoMap.Values)
            {
                var cf        = new TransformBindData.ComponentFieldCahce();
                var attribute = mi.GetAttributeInILRuntime <ComponentValueBindAttribute>();
                if (attribute.Type == null)
                {
                    Debug.LogErrorFormat("is null: {0} - {1}", attribute.Type, attribute.FunctionName);
                    continue;
                }

                cf.Transform = transform.Find(attribute.TransformPath);
                if (attribute.Type.IsSubclassOf(typeof(UIBehaviour)))
                {
                    cf.UIBehaviour = cf.Transform.GetComponent(attribute.Type) as UIBehaviour;
                }

                cf.Attribute = attribute;
                //缓存
                componentFieldCacheMap[mi.Name] = cf;
            }

            return(componentFieldCacheMap);
        }