/// <summary>
        /// 设置属性
        /// </summary>
        /// <param name="t"></param>
        /// <param name="aState"></param>
        public void SetComponentValue(Transform t, AStateBase aState)
        {
            //第一次进行缓存绑定后,就不再重新解析了,
            //所以使用者要保证每次的 state尽量是一致的
            Dictionary <string, ComponentValueCahce> map = null;
            var key = t.GetInstanceID();

            if (!componentValueCacheMap.TryGetValue(key, out map))
            {
                map = TransformStateBind(t, aState);
                componentValueCacheMap[key] = map;
            }

            while (true)
            {
                var field = aState.GetPropertyChange();

                if (field == null)
                {
                    break;
                }

                //开始
                ComponentValueCahce cvc = null;
                if (map.TryGetValue(field, out cvc))
                {
                    //考虑 是否二次验证值是否改变
                    var newValue = aState.GetValue(field);
                    //执行赋值操作
                    if (cvc.UIBehaviour != null) //UI操作
                    {
                        adaptorMap[cvc.ValueBind.Type].SetData(cvc.UIBehaviour, cvc.ValueBind.FieldName, newValue);
                    }
                    else if (cvc.Transform) //自定义逻辑管理
                    {
                        adaptorMap[cvc.ValueBind.Type].SetData(cvc.Transform, cvc.ValueBind.FieldName, newValue);
                    }

                    cvc.LastValue = newValue;
                }
                else
                {
                    BDebug.LogError("State不存在字段:" + field);
                }
            }
        }
        /// <summary>
        /// bind Tansform和State的值,防止每次都修改
        /// </summary>
        /// <param name="t"></param>
        /// <param name="aState"></param>
        /// <returns></returns>
        private Dictionary <string, ComponentValueCahce> TransformStateBind(Transform t, AStateBase aState)
        {
            Dictionary <string, ComponentValueCahce> retMap = new Dictionary <string, ComponentValueCahce>();
            //所有的成员信息
            var memberInfos = StateFactory.GetCache(aState.GetType());

            foreach (var mi in memberInfos.Values)
            {
                //先寻找节点
                Transform transform = null;
                {
                    var attrs = mi.GetCustomAttributes(typeof(TransformPath), false);
                    if (attrs.Length > 0)
                    {
                        var attr = attrs[0] as TransformPath;
                        transform = t.Find(attr.Path);
                        if (!transform)
                        {
                            BDebug.LogError("节点不存在:" + attr.Path + "  -" + aState.GetType().Name);
                            continue;
                        }
                    }
                    else
                    {
                        continue;
                    }
                }
                //再进行值绑定
                {
                    ComponentValueCahce cvc = new ComponentValueCahce();

                    var attrType = typeof(ComponentValueBind);
                    var attrs    = mi.GetCustomAttributes(attrType, false);
                    //
                    if (attrs.Length > 0) //寻找ComponentValueBind
                    {
                        if (attrs[0] is ComponentValueBind)
                        {
                            var cvb = (ComponentValueBind)attrs[0];

                            if (cvb.Type.IsSubclassOf(typeof(UIBehaviour)))
                            {
                                cvc.UIBehaviour = transform.GetComponent(cvb.Type) as UIBehaviour;
                            }
                            else
                            {
                                cvc.Transform = transform;
                            }

                            cvc.ValueBind = cvb;
                        }
                    }
                    else //如果只有Transform 没有ComponentValueBind标签,处理默认逻辑
                    {
                        Type type = null;

                        if (mi is FieldInfo)
                        {
                            type = ((FieldInfo)mi).FieldType;
                        }
                        else if (mi is PropertyInfo)
                        {
                            type = ((PropertyInfo)mi).PropertyType;
                        }


                        if (type.IsSubclassOf(typeof(PropsBase)))
                        {
                            //填充 子节点赋值逻辑
                            cvc.ValueBind =
                                new ComponentValueBind(typeof(UFluxAutoLogic), nameof(UFluxAutoLogic.SetChildValue));
                        }
                        else
                        {
                            cvc.ValueBind = new ComponentValueBind(typeof(UFluxAutoLogic), nameof(UFluxAutoLogic.ForeachSetChildValue));

                            #if UNITY_EDITOR
                            //props 数组
                            if (type.IsArray && !type.GetElementType().IsSubclassOf(typeof(PropsBase)))  //数组
                            {
                                Debug.LogError("自动适配节点逻辑失败,类型元素不是Props!!!");
                            }
                            //泛型
                            else if (type.IsGenericType && !type.GetGenericArguments()[0].IsSubclassOf(typeof(PropsBase))) //泛型
                            {
                                Debug.LogError("自动适配节点逻辑失败,类型元素不是Props!!!");
                            }
                            #endif

                            //list t或者array
                            if (type.IsArray || type.IsGenericType)  //数组
                            {
                                cvc.ValueBind = new ComponentValueBind(typeof(UFluxAutoLogic), nameof(UFluxAutoLogic.ForeachSetChildValue));
                            }
                        }

                        cvc.Transform = transform;
                    }

                    //缓存
                    retMap[mi.Name] = cvc;
                }
            }

            return(retMap);
        }
Пример #3
0
 /// <summary>
 /// 设置ComponentData
 /// </summary>
 /// <param name="t"></param>
 /// <param name="aState"></param>
 static public void SetComponentValue(Transform t, AStateBase aState)
 {
     ComponentValueAdaptorManager.Inst.SetComponentValue(t, aState);
 }