Entity.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  1. using System;
  2. using System.Collections.Generic;
  3. using MongoDB.Bson.Serialization.Attributes;
  4. namespace ET
  5. {
  6. [Flags]
  7. public enum EntityStatus: byte
  8. {
  9. None = 0,
  10. IsFromPool = 1,
  11. IsRegister = 1 << 1,
  12. IsComponent = 1 << 2,
  13. IsCreated = 1 << 3,
  14. IsNew = 1 << 4,
  15. }
  16. public partial class Entity: DisposeObject
  17. {
  18. #if ENABLE_VIEW && UNITY_EDITOR
  19. private UnityEngine.GameObject viewGO;
  20. #endif
  21. [BsonIgnore]
  22. public long InstanceId
  23. {
  24. get;
  25. protected set;
  26. }
  27. protected Entity()
  28. {
  29. }
  30. [BsonIgnore]
  31. private EntityStatus status = EntityStatus.None;
  32. [BsonIgnore]
  33. private bool IsFromPool
  34. {
  35. get => (this.status & EntityStatus.IsFromPool) == EntityStatus.IsFromPool;
  36. set
  37. {
  38. if (value)
  39. {
  40. this.status |= EntityStatus.IsFromPool;
  41. }
  42. else
  43. {
  44. this.status &= ~EntityStatus.IsFromPool;
  45. }
  46. }
  47. }
  48. [BsonIgnore]
  49. protected bool IsRegister
  50. {
  51. get => (this.status & EntityStatus.IsRegister) == EntityStatus.IsRegister;
  52. set
  53. {
  54. if (this.IsRegister == value)
  55. {
  56. return;
  57. }
  58. if (value)
  59. {
  60. this.status |= EntityStatus.IsRegister;
  61. }
  62. else
  63. {
  64. this.status &= ~EntityStatus.IsRegister;
  65. }
  66. if (!value)
  67. {
  68. Root.Instance.Remove(this.InstanceId);
  69. }
  70. else
  71. {
  72. Root.Instance.Add(this);
  73. EventSystem.Instance.RegisterSystem(this);
  74. }
  75. #if ENABLE_VIEW && UNITY_EDITOR
  76. if (value)
  77. {
  78. this.viewGO = new UnityEngine.GameObject(this.ViewName);
  79. this.viewGO.AddComponent<ComponentView>().Component = this;
  80. this.viewGO.transform.SetParent(this.Parent == null?
  81. UnityEngine.GameObject.Find("Global").transform : this.Parent.viewGO.transform);
  82. }
  83. else
  84. {
  85. UnityEngine.Object.Destroy(this.viewGO);
  86. }
  87. #endif
  88. }
  89. }
  90. protected virtual string ViewName
  91. {
  92. get
  93. {
  94. return this.GetType().Name;
  95. }
  96. }
  97. [BsonIgnore]
  98. private bool IsComponent
  99. {
  100. get => (this.status & EntityStatus.IsComponent) == EntityStatus.IsComponent;
  101. set
  102. {
  103. if (value)
  104. {
  105. this.status |= EntityStatus.IsComponent;
  106. }
  107. else
  108. {
  109. this.status &= ~EntityStatus.IsComponent;
  110. }
  111. }
  112. }
  113. [BsonIgnore]
  114. protected bool IsCreated
  115. {
  116. get => (this.status & EntityStatus.IsCreated) == EntityStatus.IsCreated;
  117. set
  118. {
  119. if (value)
  120. {
  121. this.status |= EntityStatus.IsCreated;
  122. }
  123. else
  124. {
  125. this.status &= ~EntityStatus.IsCreated;
  126. }
  127. }
  128. }
  129. [BsonIgnore]
  130. protected bool IsNew
  131. {
  132. get => (this.status & EntityStatus.IsNew) == EntityStatus.IsNew;
  133. set
  134. {
  135. if (value)
  136. {
  137. this.status |= EntityStatus.IsNew;
  138. }
  139. else
  140. {
  141. this.status &= ~EntityStatus.IsNew;
  142. }
  143. }
  144. }
  145. [BsonIgnore]
  146. public bool IsDisposed => this.InstanceId == 0;
  147. [BsonIgnore]
  148. protected Entity parent;
  149. // 可以改变parent,但是不能设置为null
  150. [BsonIgnore]
  151. public Entity Parent
  152. {
  153. get => this.parent;
  154. private set
  155. {
  156. if (value == null)
  157. {
  158. throw new Exception($"cant set parent null: {this.GetType().Name}");
  159. }
  160. if (value == this)
  161. {
  162. throw new Exception($"cant set parent self: {this.GetType().Name}");
  163. }
  164. // 严格限制parent必须要有domain,也就是说parent必须在数据树上面
  165. if (value.Domain == null)
  166. {
  167. throw new Exception($"cant set parent because parent domain is null: {this.GetType().Name} {value.GetType().Name}");
  168. }
  169. if (this.parent != null) // 之前有parent
  170. {
  171. // parent相同,不设置
  172. if (this.parent == value)
  173. {
  174. Log.Error($"重复设置了Parent: {this.GetType().Name} parent: {this.parent.GetType().Name}");
  175. return;
  176. }
  177. this.parent.RemoveFromChildren(this);
  178. }
  179. this.parent = value;
  180. this.IsComponent = false;
  181. this.parent.AddToChildren(this);
  182. this.Domain = this.parent.domain;
  183. }
  184. }
  185. // 该方法只能在AddComponent中调用,其他人不允许调用
  186. [BsonIgnore]
  187. private Entity ComponentParent
  188. {
  189. set
  190. {
  191. if (value == null)
  192. {
  193. throw new Exception($"cant set parent null: {this.GetType().Name}");
  194. }
  195. if (value == this)
  196. {
  197. throw new Exception($"cant set parent self: {this.GetType().Name}");
  198. }
  199. // 严格限制parent必须要有domain,也就是说parent必须在数据树上面
  200. if (value.Domain == null)
  201. {
  202. throw new Exception($"cant set parent because parent domain is null: {this.GetType().Name} {value.GetType().Name}");
  203. }
  204. if (this.parent != null) // 之前有parent
  205. {
  206. // parent相同,不设置
  207. if (this.parent == value)
  208. {
  209. Log.Error($"重复设置了Parent: {this.GetType().Name} parent: {this.parent.GetType().Name}");
  210. return;
  211. }
  212. this.parent.RemoveFromComponents(this);
  213. }
  214. this.parent = value;
  215. this.IsComponent = true;
  216. this.parent.AddToComponents(this);
  217. this.Domain = this.parent.domain;
  218. }
  219. }
  220. public T GetParent<T>() where T : Entity
  221. {
  222. return this.Parent as T;
  223. }
  224. [BsonIgnoreIfDefault]
  225. [BsonDefaultValue(0L)]
  226. [BsonElement]
  227. [BsonId]
  228. public long Id
  229. {
  230. get;
  231. set;
  232. }
  233. [BsonIgnore]
  234. protected Entity domain;
  235. [BsonIgnore]
  236. public Entity Domain
  237. {
  238. get
  239. {
  240. return this.domain;
  241. }
  242. private set
  243. {
  244. if (value == null)
  245. {
  246. throw new Exception($"domain cant set null: {this.GetType().Name}");
  247. }
  248. if (this.domain == value)
  249. {
  250. return;
  251. }
  252. Entity preDomain = this.domain;
  253. this.domain = value;
  254. if (preDomain == null)
  255. {
  256. this.InstanceId = IdGenerater.Instance.GenerateInstanceId();
  257. this.IsRegister = true;
  258. // 反序列化出来的需要设置父子关系
  259. if (this.componentsDB != null)
  260. {
  261. foreach (Entity component in this.componentsDB)
  262. {
  263. component.IsComponent = true;
  264. this.Components.Add(component.GetType(), component);
  265. component.parent = this;
  266. }
  267. }
  268. if (this.childrenDB != null)
  269. {
  270. foreach (Entity child in this.childrenDB)
  271. {
  272. child.IsComponent = false;
  273. this.Children.Add(child.Id, child);
  274. child.parent = this;
  275. }
  276. }
  277. }
  278. // 递归设置孩子的Domain
  279. if (this.children != null)
  280. {
  281. foreach (Entity entity in this.children.Values)
  282. {
  283. entity.Domain = this.domain;
  284. }
  285. }
  286. if (this.components != null)
  287. {
  288. foreach (Entity component in this.components.Values)
  289. {
  290. component.Domain = this.domain;
  291. }
  292. }
  293. if (!this.IsCreated)
  294. {
  295. this.IsCreated = true;
  296. EventSystem.Instance.Deserialize(this);
  297. }
  298. }
  299. }
  300. [BsonElement("Children")]
  301. [BsonIgnoreIfNull]
  302. private HashSet<Entity> childrenDB;
  303. [BsonIgnore]
  304. private Dictionary<long, Entity> children;
  305. [BsonIgnore]
  306. public Dictionary<long, Entity> Children
  307. {
  308. get
  309. {
  310. return this.children ??= ObjectPool.Instance.Fetch<Dictionary<long, Entity>>();
  311. }
  312. }
  313. private void AddToChildren(Entity entity)
  314. {
  315. this.Children.Add(entity.Id, entity);
  316. this.AddToChildrenDB(entity);
  317. }
  318. private void RemoveFromChildren(Entity entity)
  319. {
  320. if (this.children == null)
  321. {
  322. return;
  323. }
  324. this.children.Remove(entity.Id);
  325. if (this.children.Count == 0)
  326. {
  327. ObjectPool.Instance.Recycle(this.children);
  328. this.children = null;
  329. }
  330. this.RemoveFromChildrenDB(entity);
  331. }
  332. private void AddToChildrenDB(Entity entity)
  333. {
  334. if (!(entity is ISerializeToEntity))
  335. {
  336. return;
  337. }
  338. this.childrenDB ??= ObjectPool.Instance.Fetch<HashSet<Entity>>();
  339. this.childrenDB.Add(entity);
  340. }
  341. private void RemoveFromChildrenDB(Entity entity)
  342. {
  343. if (!(entity is ISerializeToEntity))
  344. {
  345. return;
  346. }
  347. if (this.childrenDB == null)
  348. {
  349. return;
  350. }
  351. this.childrenDB.Remove(entity);
  352. if (this.childrenDB.Count == 0 && this.IsNew)
  353. {
  354. ObjectPool.Instance.Recycle(this.childrenDB);
  355. this.childrenDB = null;
  356. }
  357. }
  358. [BsonElement("C")]
  359. [BsonIgnoreIfNull]
  360. private HashSet<Entity> componentsDB;
  361. [BsonIgnore]
  362. private Dictionary<Type, Entity> components;
  363. [BsonIgnore]
  364. public Dictionary<Type, Entity> Components
  365. {
  366. get
  367. {
  368. return this.components ??= ObjectPool.Instance.Fetch<Dictionary<Type, Entity>>();
  369. }
  370. }
  371. public override void Dispose()
  372. {
  373. if (this.IsDisposed)
  374. {
  375. return;
  376. }
  377. this.IsRegister = false;
  378. this.InstanceId = 0;
  379. // 清理Children
  380. if (this.children != null)
  381. {
  382. foreach (Entity child in this.children.Values)
  383. {
  384. child.Dispose();
  385. }
  386. this.children.Clear();
  387. ObjectPool.Instance.Recycle(this.children);
  388. this.children = null;
  389. if (this.childrenDB != null)
  390. {
  391. this.childrenDB.Clear();
  392. // 创建的才需要回到池中,从db中不需要回收
  393. if (this.IsNew)
  394. {
  395. ObjectPool.Instance.Recycle(this.childrenDB);
  396. this.childrenDB = null;
  397. }
  398. }
  399. }
  400. // 清理Component
  401. if (this.components != null)
  402. {
  403. foreach (KeyValuePair<Type, Entity> kv in this.components)
  404. {
  405. kv.Value.Dispose();
  406. }
  407. this.components.Clear();
  408. ObjectPool.Instance.Recycle(this.components);
  409. this.components = null;
  410. // 创建的才需要回到池中,从db中不需要回收
  411. if (this.componentsDB != null)
  412. {
  413. this.componentsDB.Clear();
  414. if (this.IsNew)
  415. {
  416. ObjectPool.Instance.Recycle(this.componentsDB);
  417. this.componentsDB = null;
  418. }
  419. }
  420. }
  421. // 触发Destroy事件
  422. if (this is IDestroy)
  423. {
  424. EventSystem.Instance.Destroy(this);
  425. }
  426. this.domain = null;
  427. if (this.parent != null && !this.parent.IsDisposed)
  428. {
  429. if (this.IsComponent)
  430. {
  431. this.parent.RemoveComponent(this);
  432. }
  433. else
  434. {
  435. this.parent.RemoveFromChildren(this);
  436. }
  437. }
  438. this.parent = null;
  439. base.Dispose();
  440. if (this.IsFromPool)
  441. {
  442. ObjectPool.Instance.Recycle(this);
  443. }
  444. status = EntityStatus.None;
  445. }
  446. private void AddToComponentsDB(Entity component)
  447. {
  448. if (!(component is ISerializeToEntity))
  449. {
  450. return;
  451. }
  452. this.componentsDB ??= ObjectPool.Instance.Fetch<HashSet<Entity>>();
  453. this.componentsDB.Add(component);
  454. }
  455. private void RemoveFromComponentsDB(Entity component)
  456. {
  457. if (!(component is ISerializeToEntity))
  458. {
  459. return;
  460. }
  461. if (this.componentsDB == null)
  462. {
  463. return;
  464. }
  465. this.componentsDB.Remove(component);
  466. if (this.componentsDB.Count == 0 && this.IsNew)
  467. {
  468. ObjectPool.Instance.Recycle(this.componentsDB);
  469. this.componentsDB = null;
  470. }
  471. }
  472. private void AddToComponents(Entity component)
  473. {
  474. this.Components.Add(component.GetType(), component);
  475. this.AddToComponentsDB(component);
  476. }
  477. private void RemoveFromComponents(Entity component)
  478. {
  479. if (this.components == null)
  480. {
  481. return;
  482. }
  483. this.components.Remove(component.GetType());
  484. if (this.components.Count == 0)
  485. {
  486. ObjectPool.Instance.Recycle(this.components);
  487. this.components = null;
  488. }
  489. this.RemoveFromComponentsDB(component);
  490. }
  491. public K GetChild<K>(long id) where K: Entity
  492. {
  493. if (this.children == null)
  494. {
  495. return null;
  496. }
  497. this.children.TryGetValue(id, out Entity child);
  498. return child as K;
  499. }
  500. public void RemoveChild(long id)
  501. {
  502. if (this.children == null)
  503. {
  504. return;
  505. }
  506. if (!this.children.TryGetValue(id, out Entity child))
  507. {
  508. return;
  509. }
  510. this.children.Remove(id);
  511. child.Dispose();
  512. }
  513. public void RemoveComponent<K>() where K : Entity
  514. {
  515. if (this.IsDisposed)
  516. {
  517. return;
  518. }
  519. if (this.components == null)
  520. {
  521. return;
  522. }
  523. Type type = typeof (K);
  524. Entity c = this.GetComponent(type);
  525. if (c == null)
  526. {
  527. return;
  528. }
  529. this.RemoveFromComponents(c);
  530. c.Dispose();
  531. }
  532. public void RemoveComponent(Entity component)
  533. {
  534. if (this.IsDisposed)
  535. {
  536. return;
  537. }
  538. if (this.components == null)
  539. {
  540. return;
  541. }
  542. Entity c = this.GetComponent(component.GetType());
  543. if (c == null)
  544. {
  545. return;
  546. }
  547. if (c.InstanceId != component.InstanceId)
  548. {
  549. return;
  550. }
  551. this.RemoveFromComponents(c);
  552. c.Dispose();
  553. }
  554. public void RemoveComponent(Type type)
  555. {
  556. if (this.IsDisposed)
  557. {
  558. return;
  559. }
  560. Entity c = this.GetComponent(type);
  561. if (c == null)
  562. {
  563. return;
  564. }
  565. RemoveFromComponents(c);
  566. c.Dispose();
  567. }
  568. public K GetComponent<K>() where K : Entity
  569. {
  570. if (this.components == null)
  571. {
  572. return null;
  573. }
  574. Entity component;
  575. if (!this.components.TryGetValue(typeof (K), out component))
  576. {
  577. return default;
  578. }
  579. // 如果有IGetComponent接口,则触发GetComponentSystem
  580. if (this is IGetComponent)
  581. {
  582. EventSystem.Instance.GetComponent(this, component);
  583. }
  584. return (K) component;
  585. }
  586. public Entity GetComponent(Type type)
  587. {
  588. if (this.components == null)
  589. {
  590. return null;
  591. }
  592. Entity component;
  593. if (!this.components.TryGetValue(type, out component))
  594. {
  595. return null;
  596. }
  597. // 如果有IGetComponent接口,则触发GetComponentSystem
  598. if (this is IGetComponent)
  599. {
  600. EventSystem.Instance.GetComponent(this, component);
  601. }
  602. return component;
  603. }
  604. private static Entity Create(Type type, bool isFromPool)
  605. {
  606. Entity component;
  607. if (isFromPool)
  608. {
  609. component = (Entity)ObjectPool.Instance.Fetch(type);
  610. }
  611. else
  612. {
  613. component = Activator.CreateInstance(type) as Entity;
  614. }
  615. component.IsFromPool = isFromPool;
  616. component.IsCreated = true;
  617. component.IsNew = true;
  618. component.Id = 0;
  619. return component;
  620. }
  621. public Entity AddComponent(Entity component)
  622. {
  623. Type type = component.GetType();
  624. if (this.components != null && this.components.ContainsKey(type))
  625. {
  626. throw new Exception($"entity already has component: {type.FullName}");
  627. }
  628. component.ComponentParent = this;
  629. if (this is IAddComponent)
  630. {
  631. EventSystem.Instance.AddComponent(this, component);
  632. }
  633. return component;
  634. }
  635. public Entity AddComponent(Type type, bool isFromPool = false)
  636. {
  637. if (this.components != null && this.components.ContainsKey(type))
  638. {
  639. throw new Exception($"entity already has component: {type.FullName}");
  640. }
  641. Entity component = Create(type, isFromPool);
  642. component.Id = this.Id;
  643. component.ComponentParent = this;
  644. EventSystem.Instance.Awake(component);
  645. if (this is IAddComponent)
  646. {
  647. EventSystem.Instance.AddComponent(this, component);
  648. }
  649. return component;
  650. }
  651. public K AddComponent<K>(bool isFromPool = false) where K : Entity, IAwake, new()
  652. {
  653. Type type = typeof (K);
  654. if (this.components != null && this.components.ContainsKey(type))
  655. {
  656. throw new Exception($"entity already has component: {type.FullName}");
  657. }
  658. Entity component = Create(type, isFromPool);
  659. component.Id = this.Id;
  660. component.ComponentParent = this;
  661. EventSystem.Instance.Awake(component);
  662. if (this is IAddComponent)
  663. {
  664. EventSystem.Instance.AddComponent(this, component);
  665. }
  666. return component as K;
  667. }
  668. public K AddComponent<K, P1>(P1 p1, bool isFromPool = false) where K : Entity, IAwake<P1>, new()
  669. {
  670. Type type = typeof (K);
  671. if (this.components != null && this.components.ContainsKey(type))
  672. {
  673. throw new Exception($"entity already has component: {type.FullName}");
  674. }
  675. Entity component = Create(type, isFromPool);
  676. component.Id = this.Id;
  677. component.ComponentParent = this;
  678. EventSystem.Instance.Awake(component, p1);
  679. if (this is IAddComponent)
  680. {
  681. EventSystem.Instance.AddComponent(this, component);
  682. }
  683. return component as K;
  684. }
  685. public K AddComponent<K, P1, P2>(P1 p1, P2 p2, bool isFromPool = false) where K : Entity, IAwake<P1, P2>, new()
  686. {
  687. Type type = typeof (K);
  688. if (this.components != null && this.components.ContainsKey(type))
  689. {
  690. throw new Exception($"entity already has component: {type.FullName}");
  691. }
  692. Entity component = Create(type, isFromPool);
  693. component.Id = this.Id;
  694. component.ComponentParent = this;
  695. EventSystem.Instance.Awake(component, p1, p2);
  696. if (this is IAddComponent)
  697. {
  698. EventSystem.Instance.AddComponent(this, component);
  699. }
  700. return component as K;
  701. }
  702. public K AddComponent<K, P1, P2, P3>(P1 p1, P2 p2, P3 p3, bool isFromPool = false) where K : Entity, IAwake<P1, P2, P3>, new()
  703. {
  704. Type type = typeof (K);
  705. if (this.components != null && this.components.ContainsKey(type))
  706. {
  707. throw new Exception($"entity already has component: {type.FullName}");
  708. }
  709. Entity component = Create(type, isFromPool);
  710. component.Id = this.Id;
  711. component.ComponentParent = this;
  712. EventSystem.Instance.Awake(component, p1, p2, p3);
  713. if (this is IAddComponent)
  714. {
  715. EventSystem.Instance.AddComponent(this, component);
  716. }
  717. return component as K;
  718. }
  719. public Entity AddChild(Entity entity)
  720. {
  721. entity.Parent = this;
  722. return entity;
  723. }
  724. public T AddChild<T>(bool isFromPool = false) where T : Entity, IAwake
  725. {
  726. Type type = typeof (T);
  727. T component = (T) Entity.Create(type, isFromPool);
  728. component.Id = IdGenerater.Instance.GenerateId();
  729. component.Parent = this;
  730. EventSystem.Instance.Awake(component);
  731. return component;
  732. }
  733. public T AddChild<T, A>(A a, bool isFromPool = false) where T : Entity, IAwake<A>
  734. {
  735. Type type = typeof (T);
  736. T component = (T) Entity.Create(type, isFromPool);
  737. component.Id = IdGenerater.Instance.GenerateId();
  738. component.Parent = this;
  739. EventSystem.Instance.Awake(component, a);
  740. return component;
  741. }
  742. public T AddChild<T, A, B>(A a, B b, bool isFromPool = false) where T : Entity, IAwake<A, B>
  743. {
  744. Type type = typeof (T);
  745. T component = (T) Entity.Create(type, isFromPool);
  746. component.Id = IdGenerater.Instance.GenerateId();
  747. component.Parent = this;
  748. EventSystem.Instance.Awake(component, a, b);
  749. return component;
  750. }
  751. public T AddChild<T, A, B, C>(A a, B b, C c, bool isFromPool = false) where T : Entity, IAwake<A, B, C>
  752. {
  753. Type type = typeof (T);
  754. T component = (T) Entity.Create(type, isFromPool);
  755. component.Id = IdGenerater.Instance.GenerateId();
  756. component.Parent = this;
  757. EventSystem.Instance.Awake(component, a, b, c);
  758. return component;
  759. }
  760. public T AddChild<T, A, B, C, D>(A a, B b, C c, D d, bool isFromPool = false) where T : Entity, IAwake<A, B, C, D>
  761. {
  762. Type type = typeof (T);
  763. T component = (T) Entity.Create(type, isFromPool);
  764. component.Id = IdGenerater.Instance.GenerateId();
  765. component.Parent = this;
  766. EventSystem.Instance.Awake(component, a, b, c, d);
  767. return component;
  768. }
  769. public T AddChildWithId<T>(long id, bool isFromPool = false) where T : Entity, IAwake, new()
  770. {
  771. Type type = typeof (T);
  772. T component = Entity.Create(type, isFromPool) as T;
  773. component.Id = id;
  774. component.Parent = this;
  775. EventSystem.Instance.Awake(component);
  776. return component;
  777. }
  778. public T AddChildWithId<T, A>(long id, A a, bool isFromPool = false) where T : Entity, IAwake<A>
  779. {
  780. Type type = typeof (T);
  781. T component = (T) Entity.Create(type, isFromPool);
  782. component.Id = id;
  783. component.Parent = this;
  784. EventSystem.Instance.Awake(component, a);
  785. return component;
  786. }
  787. public T AddChildWithId<T, A, B>(long id, A a, B b, bool isFromPool = false) where T : Entity, IAwake<A, B>
  788. {
  789. Type type = typeof (T);
  790. T component = (T) Entity.Create(type, isFromPool);
  791. component.Id = id;
  792. component.Parent = this;
  793. EventSystem.Instance.Awake(component, a, b);
  794. return component;
  795. }
  796. public T AddChildWithId<T, A, B, C>(long id, A a, B b, C c, bool isFromPool = false) where T : Entity, IAwake<A, B, C>
  797. {
  798. Type type = typeof (T);
  799. T component = (T) Entity.Create(type, isFromPool);
  800. component.Id = id;
  801. component.Parent = this;
  802. EventSystem.Instance.Awake(component, a, b, c);
  803. return component;
  804. }
  805. }
  806. }