/// <summary> /// 设置组件值,对最后一个添加的组件操作 /// </summary> /// <param name="component"></param> /// <param name="value"></param> /// <returns></returns> public Entity SetValue(ComponentValue component, object value) { int componentId = component.ComponentId; if (ComponentIds.COMPONENT_MAX_COUNT > componentId) { NormalComponent normalComponent = this._allComponenArray[componentId]; if (normalComponent == null) { Debug.LogError("组件不存在,组件Id:" + componentId); return(this); } try { if (Observer.IsUseThread) { TSThread.Instance.SetValue(this, normalComponent, component.PropertyId, value); } else { normalComponent.PropertyArray[component.PropertyId].Setter(this, normalComponent, value); } return(this); } catch (Exception ex) { Debug.LogErrorFormat("设置属性{0},的时候出错,错误信息{1}", component.PropertyId, ex.Message); return(this); } } Debug.LogError("组件Id过大无法设置,组件Id:" + componentId); return(this); }
/// <summary> /// 获取值,对最后一个添加的组件操作 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="component"></param> /// <returns></returns> public T GetValue <T>(ComponentValue component) { T t = default(T); int componentId = component.ComponentId; if (ComponentIds.COMPONENT_MAX_COUNT > componentId) { NormalComponent normalComponent = this._allComponenArray[componentId]; if (normalComponent == null) { Debug.LogError("组件不存在,组件Id:" + componentId); return(t); } try { object o = normalComponent.PropertyArray[component.PropertyId].Getter(normalComponent.CurrentComponent); t = (T)o; return(t); } catch (Exception ex) { Debug.LogErrorFormat("获取属性{0},的时候出错,错误信息{1}", component.PropertyId, ex.Message); return(t); } } Debug.LogError("组件Id过大无法获取,组件Id:" + componentId); return(t); }
public static void RegisteComponent(Type instanceType, int index) { try { Type varType = instanceType.Assembly.GetType("TSFrame.ECS." + instanceType.Name + "Variable"); if (varType == null) { throw new Exception(instanceType.Name + "没有组件说明类,请先生成组件说明类"); } PropertyInfo countProperty = varType.GetProperty("Count", BindingFlags.Public | BindingFlags.Static); if (countProperty == null) { throw new Exception(instanceType.Name + "没有组件说明类,请先生成组件说明类"); } int count = (int)countProperty.GetValue(null, null); if (count <= 0) { return; } TSProperty[] tempArray = new TSProperty[count]; //Dictionary<string, TSProperty> tempReturnDic = new Dictionary<string, TSProperty>(); #region Property PropertyInfo[] props = instanceType.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); if (props != null && props.Length > 0) { for (int i = 0; i < props.Length; i++) { //bool isDataDriven = false; PropertyInfo property = props[i]; if (property.GetSetMethod() == null || property.GetGetMethod() == null) { continue; } FieldInfo varFieldInfo = varType.GetField(property.Name, BindingFlags.Public | BindingFlags.Static); if (varFieldInfo == null) { continue; } ComponentValue componentValue = varFieldInfo.GetValue(null) as ComponentValue; if (componentValue == null) { continue; } TSProperty tsProperty = CreateProperty(null, property, componentValue.NeedReactive); tsProperty.DontCopy = componentValue.DontCopy; object[] objs = property.GetCustomAttributes(_defaultValueType, false); if (objs.Length > 0) { tsProperty.DefaultValue = (objs[0] as DefaultValueAttribute).Value; } else { tsProperty.DefaultValue = property.PropertyType.IsValueType ? Activator.CreateInstance(property.PropertyType) : null; } tempArray[componentValue.PropertyId] = tsProperty; //tempReturnDic.Add(property.Name, tsProperty); } } #endregion #region FieldInfo FieldInfo[] fieldInfos = instanceType.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); if (fieldInfos != null && fieldInfos.Length > 0) { for (int i = 0; i < fieldInfos.Length; i++) { FieldInfo fieldInfo = fieldInfos[i]; FieldInfo varFieldInfo = varType.GetField(fieldInfo.Name, BindingFlags.Public | BindingFlags.Static); if (varFieldInfo == null) { continue; } ComponentValue componentValue = varFieldInfo.GetValue(null) as ComponentValue; if (componentValue == null) { continue; } TSProperty tsProperty = CreateProperty(null, fieldInfo, componentValue.NeedReactive); tsProperty.DontCopy = componentValue.DontCopy; object[] objs = fieldInfo.GetCustomAttributes(_defaultValueType, false); if (objs.Length > 0) { tsProperty.DefaultValue = (objs[0] as DefaultValueAttribute).Value; } else { tsProperty.DefaultValue = fieldInfo.FieldType.IsValueType ? Activator.CreateInstance(fieldInfo.FieldType) : null; } tempArray[componentValue.PropertyId] = tsProperty; } } #endregion //_ilCache.Add(instance.CurrentId, tempArray); _ilArrayCache[index] = tempArray; } catch (Exception ex) { throw ex; } }