/// <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); } } }
/// <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); }