/// <summary> /// 移除组件回调方法 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="path"></param> static void InnerRemoveUIComponent(this Entity self, Entity component, string path) { if (component != null) { self.GetCompoennts()[path].Remove(component.GetType()); self.SetLength(self.GetLength() - 1); if (self.GetCompoennts()[path].Count <= 0) { self.GetCompoennts().Remove(path); } } }
//记录Component static void RecordUIComponent(this Entity self, string name, Type component_class, Entity component) { if (self.GetCompoennts().TryGetValue(name, out var obj)) { if (obj.ContainsKey(component_class)) { Log.Error("Aready exist component_class : " + component_class.Name); return; } } else//如果必要,创建新的记录,对应Unity下一个Transform下所有挂载脚本的记录表 { self.GetCompoennts()[name] = new Dictionary <Type, Entity>(); } self.GetCompoennts()[name][component_class] = component; }
/// <summary> /// 移除组件 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="path"></param> public static void RemoveUIComponent <T>(this Entity self, string path = "") where T : Entity { var component = self.GetUIComponent <T>(path); if (component != null) { component.BeforeOnDestroy(); UIEventSystem.Instance.OnDestroy(component); self.GetCompoennts()[path].Remove(typeof(T)); component.Dispose(); } }
/// <summary> /// 获取组件 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="path"></param> /// <returns></returns> public static T GetUIComponent <T>(this Entity self, string path = "") where T : Entity { if (self.GetCompoennts().TryGetValue(path, out var obj)) { Type type = typeof(T); if (obj.TryGetValue(type, out var component)) { return(component as T); } } return(null); }
//遍历:注意,这里是无序的 static void Walk(this Entity self, Action <Entity> callback) { foreach (var item in self.GetCompoennts()) { if (item.Value != null) { foreach (var item2 in item.Value) { callback(item2.Value); } } } }
public static void BeforeOnDestroy(this Entity self) { var keys1 = self.GetCompoennts().Keys.ToList(); for (int i = keys1.Count - 1; i >= 0; i--) { if (self.GetCompoennts()[keys1[i]] != null) { var keys2 = self.GetCompoennts()[keys1[i]].Keys.ToList(); for (int j = keys2.Count - 1; j >= 0; j--) { var component = self.GetCompoennts()[keys1[i]][keys2[j]]; component.BeforeOnDestroy(); UIEventSystem.Instance.OnDestroy(component); } } } self.SetLength(self.GetLength() - 1); if (self.GetLength() <= 0) { if (UIManagerComponent.Instance.pathMap.TryGetValue(self.Id, out var path)) { self.Parent.InnerRemoveUIComponent(self, path); } else { Log.Info("Close window here, type name: " + self.GetType().Name); } } else { Log.Error("OnDestroy fail, length != 0"); } UIManagerComponent.Instance.componentsMap.Remove(self.Id); UIManagerComponent.Instance.lengthMap.Remove(self.Id); UIManagerComponent.Instance.pathMap.Remove(self.Id); self.Dispose(); }