public AStateBase() { var t = this.GetType(); var cache = StateFactory.GetCache(t); if (cache == null) { List <MemberInfo> list = new List <MemberInfo>(); var flag = BindingFlags.Instance | BindingFlags.Public; list.AddRange(t.GetFields(flag)); list.AddRange(t.GetProperties(flag)); //缓存所有属性 propMap = new Dictionary <string, MemberInfo>(); foreach (var mi in list) { var attrs = mi.GetCustomAttributes(typeof(TransformPath), false); if (attrs.Length > 0) { propMap[mi.Name] = mi; } } StateFactory.AddCache(t, propMap); } else { propMap = cache; } }
/// <summary> /// 设置所有属性改变 /// </summary> /// <exception cref="NotImplementedException"></exception> public void SetAllPropertyChanged() { var t = this.GetType(); var map = StateFactory.GetCache(t); var list = map.Keys.ToArray(); this.curProptyChangeList.Clear(); this.curProptyChangeList.AddRange(list); }
/// <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); }