Entity.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  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. #if ENABLE_VIEW && UNITY_EDITOR
  184. this.viewGO.GetComponent<ComponentView>().Component = this;
  185. this.viewGO.transform.SetParent(this.Parent == null ?
  186. UnityEngine.GameObject.Find("Global").transform : this.Parent.viewGO.transform);
  187. foreach (var child in this.Children.Values)
  188. {
  189. child.viewGO.transform.SetParent(this.viewGO.transform);
  190. }
  191. foreach (var comp in this.Components.Values)
  192. {
  193. comp.viewGO.transform.SetParent(this.viewGO.transform);
  194. }
  195. #endif
  196. }
  197. }
  198. // 该方法只能在AddComponent中调用,其他人不允许调用
  199. [BsonIgnore]
  200. private Entity ComponentParent
  201. {
  202. set
  203. {
  204. if (value == null)
  205. {
  206. throw new Exception($"cant set parent null: {this.GetType().Name}");
  207. }
  208. if (value == this)
  209. {
  210. throw new Exception($"cant set parent self: {this.GetType().Name}");
  211. }
  212. // 严格限制parent必须要有domain,也就是说parent必须在数据树上面
  213. if (value.Domain == null)
  214. {
  215. throw new Exception($"cant set parent because parent domain is null: {this.GetType().Name} {value.GetType().Name}");
  216. }
  217. if (this.parent != null) // 之前有parent
  218. {
  219. // parent相同,不设置
  220. if (this.parent == value)
  221. {
  222. Log.Error($"重复设置了Parent: {this.GetType().Name} parent: {this.parent.GetType().Name}");
  223. return;
  224. }
  225. this.parent.RemoveFromComponents(this);
  226. }
  227. this.parent = value;
  228. this.IsComponent = true;
  229. this.parent.AddToComponents(this);
  230. this.Domain = this.parent.domain;
  231. }
  232. }
  233. public T GetParent<T>() where T : Entity
  234. {
  235. return this.Parent as T;
  236. }
  237. [BsonIgnoreIfDefault]
  238. [BsonDefaultValue(0L)]
  239. [BsonElement]
  240. [BsonId]
  241. public long Id
  242. {
  243. get;
  244. set;
  245. }
  246. [BsonIgnore]
  247. protected Entity domain;
  248. [BsonIgnore]
  249. public Entity Domain
  250. {
  251. get
  252. {
  253. return this.domain;
  254. }
  255. private set
  256. {
  257. if (value == null)
  258. {
  259. throw new Exception($"domain cant set null: {this.GetType().Name}");
  260. }
  261. if (this.domain == value)
  262. {
  263. return;
  264. }
  265. Entity preDomain = this.domain;
  266. this.domain = value;
  267. if (preDomain == null)
  268. {
  269. this.InstanceId = IdGenerater.Instance.GenerateInstanceId();
  270. this.IsRegister = true;
  271. // 反序列化出来的需要设置父子关系
  272. if (this.componentsDB != null)
  273. {
  274. foreach (Entity component in this.componentsDB)
  275. {
  276. component.IsComponent = true;
  277. this.Components.Add(component.GetType(), component);
  278. component.parent = this;
  279. }
  280. }
  281. if (this.childrenDB != null)
  282. {
  283. foreach (Entity child in this.childrenDB)
  284. {
  285. child.IsComponent = false;
  286. this.Children.Add(child.Id, child);
  287. child.parent = this;
  288. }
  289. }
  290. }
  291. // 递归设置孩子的Domain
  292. if (this.children != null)
  293. {
  294. foreach (Entity entity in this.children.Values)
  295. {
  296. entity.Domain = this.domain;
  297. }
  298. }
  299. if (this.components != null)
  300. {
  301. foreach (Entity component in this.components.Values)
  302. {
  303. component.Domain = this.domain;
  304. }
  305. }
  306. if (!this.IsCreated)
  307. {
  308. this.IsCreated = true;
  309. EventSystem.Instance.Deserialize(this);
  310. }
  311. }
  312. }
  313. [BsonElement("Children")]
  314. [BsonIgnoreIfNull]
  315. private HashSet<Entity> childrenDB;
  316. [BsonIgnore]
  317. private Dictionary<long, Entity> children;
  318. [BsonIgnore]
  319. public Dictionary<long, Entity> Children
  320. {
  321. get
  322. {
  323. return this.children ??= ObjectPool.Instance.Fetch<Dictionary<long, Entity>>();
  324. }
  325. }
  326. private void AddToChildren(Entity entity)
  327. {
  328. this.Children.Add(entity.Id, entity);
  329. this.AddToChildrenDB(entity);
  330. }
  331. private void RemoveFromChildren(Entity entity)
  332. {
  333. if (this.children == null)
  334. {
  335. return;
  336. }
  337. this.children.Remove(entity.Id);
  338. if (this.children.Count == 0)
  339. {
  340. ObjectPool.Instance.Recycle(this.children);
  341. this.children = null;
  342. }
  343. this.RemoveFromChildrenDB(entity);
  344. }
  345. private void AddToChildrenDB(Entity entity)
  346. {
  347. if (!(entity is ISerializeToEntity))
  348. {
  349. return;
  350. }
  351. this.childrenDB ??= ObjectPool.Instance.Fetch<HashSet<Entity>>();
  352. this.childrenDB.Add(entity);
  353. }
  354. private void RemoveFromChildrenDB(Entity entity)
  355. {
  356. if (!(entity is ISerializeToEntity))
  357. {
  358. return;
  359. }
  360. if (this.childrenDB == null)
  361. {
  362. return;
  363. }
  364. this.childrenDB.Remove(entity);
  365. if (this.childrenDB.Count == 0 && this.IsNew)
  366. {
  367. ObjectPool.Instance.Recycle(this.childrenDB);
  368. this.childrenDB = null;
  369. }
  370. }
  371. [BsonElement("C")]
  372. [BsonIgnoreIfNull]
  373. private HashSet<Entity> componentsDB;
  374. [BsonIgnore]
  375. private Dictionary<Type, Entity> components;
  376. [BsonIgnore]
  377. public Dictionary<Type, Entity> Components
  378. {
  379. get
  380. {
  381. return this.components ??= ObjectPool.Instance.Fetch<Dictionary<Type, Entity>>();
  382. }
  383. }
  384. public override void Dispose()
  385. {
  386. if (this.IsDisposed)
  387. {
  388. return;
  389. }
  390. this.IsRegister = false;
  391. this.InstanceId = 0;
  392. // 清理Children
  393. if (this.children != null)
  394. {
  395. foreach (Entity child in this.children.Values)
  396. {
  397. child.Dispose();
  398. }
  399. this.children.Clear();
  400. ObjectPool.Instance.Recycle(this.children);
  401. this.children = null;
  402. if (this.childrenDB != null)
  403. {
  404. this.childrenDB.Clear();
  405. // 创建的才需要回到池中,从db中不需要回收
  406. if (this.IsNew)
  407. {
  408. ObjectPool.Instance.Recycle(this.childrenDB);
  409. this.childrenDB = null;
  410. }
  411. }
  412. }
  413. // 清理Component
  414. if (this.components != null)
  415. {
  416. foreach (KeyValuePair<Type, Entity> kv in this.components)
  417. {
  418. kv.Value.Dispose();
  419. }
  420. this.components.Clear();
  421. ObjectPool.Instance.Recycle(this.components);
  422. this.components = null;
  423. // 创建的才需要回到池中,从db中不需要回收
  424. if (this.componentsDB != null)
  425. {
  426. this.componentsDB.Clear();
  427. if (this.IsNew)
  428. {
  429. ObjectPool.Instance.Recycle(this.componentsDB);
  430. this.componentsDB = null;
  431. }
  432. }
  433. }
  434. // 触发Destroy事件
  435. if (this is IDestroy)
  436. {
  437. EventSystem.Instance.Destroy(this);
  438. }
  439. this.domain = null;
  440. if (this.parent != null && !this.parent.IsDisposed)
  441. {
  442. if (this.IsComponent)
  443. {
  444. this.parent.RemoveComponent(this);
  445. }
  446. else
  447. {
  448. this.parent.RemoveFromChildren(this);
  449. }
  450. }
  451. this.parent = null;
  452. base.Dispose();
  453. if (this.IsFromPool)
  454. {
  455. ObjectPool.Instance.Recycle(this);
  456. }
  457. status = EntityStatus.None;
  458. }
  459. private void AddToComponentsDB(Entity component)
  460. {
  461. if (!(component is ISerializeToEntity))
  462. {
  463. return;
  464. }
  465. this.componentsDB ??= ObjectPool.Instance.Fetch<HashSet<Entity>>();
  466. this.componentsDB.Add(component);
  467. }
  468. private void RemoveFromComponentsDB(Entity component)
  469. {
  470. if (!(component is ISerializeToEntity))
  471. {
  472. return;
  473. }
  474. if (this.componentsDB == null)
  475. {
  476. return;
  477. }
  478. this.componentsDB.Remove(component);
  479. if (this.componentsDB.Count == 0 && this.IsNew)
  480. {
  481. ObjectPool.Instance.Recycle(this.componentsDB);
  482. this.componentsDB = null;
  483. }
  484. }
  485. private void AddToComponents(Entity component)
  486. {
  487. this.Components.Add(component.GetType(), component);
  488. this.AddToComponentsDB(component);
  489. }
  490. private void RemoveFromComponents(Entity component)
  491. {
  492. if (this.components == null)
  493. {
  494. return;
  495. }
  496. this.components.Remove(component.GetType());
  497. if (this.components.Count == 0)
  498. {
  499. ObjectPool.Instance.Recycle(this.components);
  500. this.components = null;
  501. }
  502. this.RemoveFromComponentsDB(component);
  503. }
  504. public K GetChild<K>(long id) where K: Entity
  505. {
  506. if (this.children == null)
  507. {
  508. return null;
  509. }
  510. this.children.TryGetValue(id, out Entity child);
  511. return child as K;
  512. }
  513. public void RemoveChild(long id)
  514. {
  515. if (this.children == null)
  516. {
  517. return;
  518. }
  519. if (!this.children.TryGetValue(id, out Entity child))
  520. {
  521. return;
  522. }
  523. this.children.Remove(id);
  524. child.Dispose();
  525. }
  526. public void RemoveComponent<K>() where K : Entity
  527. {
  528. if (this.IsDisposed)
  529. {
  530. return;
  531. }
  532. if (this.components == null)
  533. {
  534. return;
  535. }
  536. Type type = typeof (K);
  537. Entity c = this.GetComponent(type);
  538. if (c == null)
  539. {
  540. return;
  541. }
  542. this.RemoveFromComponents(c);
  543. c.Dispose();
  544. }
  545. public void RemoveComponent(Entity component)
  546. {
  547. if (this.IsDisposed)
  548. {
  549. return;
  550. }
  551. if (this.components == null)
  552. {
  553. return;
  554. }
  555. Entity c = this.GetComponent(component.GetType());
  556. if (c == null)
  557. {
  558. return;
  559. }
  560. if (c.InstanceId != component.InstanceId)
  561. {
  562. return;
  563. }
  564. this.RemoveFromComponents(c);
  565. c.Dispose();
  566. }
  567. public void RemoveComponent(Type type)
  568. {
  569. if (this.IsDisposed)
  570. {
  571. return;
  572. }
  573. Entity c = this.GetComponent(type);
  574. if (c == null)
  575. {
  576. return;
  577. }
  578. RemoveFromComponents(c);
  579. c.Dispose();
  580. }
  581. public K GetComponent<K>() where K : Entity
  582. {
  583. if (this.components == null)
  584. {
  585. return null;
  586. }
  587. Entity component;
  588. if (!this.components.TryGetValue(typeof (K), out component))
  589. {
  590. return default;
  591. }
  592. // 如果有IGetComponent接口,则触发GetComponentSystem
  593. if (this is IGetComponent)
  594. {
  595. EventSystem.Instance.GetComponent(this, component);
  596. }
  597. return (K) component;
  598. }
  599. public Entity GetComponent(Type type)
  600. {
  601. if (this.components == null)
  602. {
  603. return null;
  604. }
  605. Entity component;
  606. if (!this.components.TryGetValue(type, out component))
  607. {
  608. return null;
  609. }
  610. // 如果有IGetComponent接口,则触发GetComponentSystem
  611. if (this is IGetComponent)
  612. {
  613. EventSystem.Instance.GetComponent(this, component);
  614. }
  615. return component;
  616. }
  617. private static Entity Create(Type type, bool isFromPool)
  618. {
  619. Entity component;
  620. if (isFromPool)
  621. {
  622. component = (Entity)ObjectPool.Instance.Fetch(type);
  623. }
  624. else
  625. {
  626. component = Activator.CreateInstance(type) as Entity;
  627. }
  628. component.IsFromPool = isFromPool;
  629. component.IsCreated = true;
  630. component.IsNew = true;
  631. component.Id = 0;
  632. return component;
  633. }
  634. public Entity AddComponent(Entity component)
  635. {
  636. Type type = component.GetType();
  637. if (this.components != null && this.components.ContainsKey(type))
  638. {
  639. throw new Exception($"entity already has component: {type.FullName}");
  640. }
  641. component.ComponentParent = this;
  642. if (this is IAddComponent)
  643. {
  644. EventSystem.Instance.AddComponent(this, component);
  645. }
  646. return component;
  647. }
  648. public Entity AddComponent(Type type, bool isFromPool = false)
  649. {
  650. if (this.components != null && this.components.ContainsKey(type))
  651. {
  652. throw new Exception($"entity already has component: {type.FullName}");
  653. }
  654. Entity component = Create(type, isFromPool);
  655. component.Id = this.Id;
  656. component.ComponentParent = this;
  657. EventSystem.Instance.Awake(component);
  658. if (this is IAddComponent)
  659. {
  660. EventSystem.Instance.AddComponent(this, component);
  661. }
  662. return component;
  663. }
  664. public K AddComponent<K>(bool isFromPool = false, params object[] param) where K : Entity, IAwake, new()
  665. {
  666. Type type = typeof (K);
  667. if (this.components != null && this.components.ContainsKey(type))
  668. {
  669. throw new Exception($"entity already has component: {type.FullName}");
  670. }
  671. Entity component = Create(type, isFromPool);
  672. component.Id = this.Id;
  673. component.ComponentParent = this;
  674. EventSystem.Instance.Awake(component, param);
  675. if (this is IAddComponent)
  676. {
  677. EventSystem.Instance.AddComponent(this, component);
  678. }
  679. return component as K;
  680. }
  681. public K AddComponent<K, P1>(P1 p1, bool isFromPool = false) where K : Entity, IAwake<P1>, new()
  682. {
  683. Type type = typeof (K);
  684. if (this.components != null && this.components.ContainsKey(type))
  685. {
  686. throw new Exception($"entity already has component: {type.FullName}");
  687. }
  688. Entity component = Create(type, isFromPool);
  689. component.Id = this.Id;
  690. component.ComponentParent = this;
  691. EventSystem.Instance.Awake(component, p1);
  692. if (this is IAddComponent)
  693. {
  694. EventSystem.Instance.AddComponent(this, component);
  695. }
  696. return component as K;
  697. }
  698. public K AddComponent<K, P1, P2>(P1 p1, P2 p2, bool isFromPool = false) where K : Entity, IAwake<P1, P2>, new()
  699. {
  700. Type type = typeof (K);
  701. if (this.components != null && this.components.ContainsKey(type))
  702. {
  703. throw new Exception($"entity already has component: {type.FullName}");
  704. }
  705. Entity component = Create(type, isFromPool);
  706. component.Id = this.Id;
  707. component.ComponentParent = this;
  708. EventSystem.Instance.Awake(component, p1, p2);
  709. if (this is IAddComponent)
  710. {
  711. EventSystem.Instance.AddComponent(this, component);
  712. }
  713. return component as K;
  714. }
  715. public K AddComponent<K, P1, P2, P3>(P1 p1, P2 p2, P3 p3, bool isFromPool = false) where K : Entity, IAwake<P1, P2, P3>, new()
  716. {
  717. Type type = typeof (K);
  718. if (this.components != null && this.components.ContainsKey(type))
  719. {
  720. throw new Exception($"entity already has component: {type.FullName}");
  721. }
  722. Entity component = Create(type, isFromPool);
  723. component.Id = this.Id;
  724. component.ComponentParent = this;
  725. EventSystem.Instance.Awake(component, p1, p2, p3);
  726. if (this is IAddComponent)
  727. {
  728. EventSystem.Instance.AddComponent(this, component);
  729. }
  730. return component as K;
  731. }
  732. public Entity AddChild(Entity entity)
  733. {
  734. entity.Parent = this;
  735. return entity;
  736. }
  737. public T AddChild<T>(bool isFromPool = false) where T : Entity, IAwake
  738. {
  739. Type type = typeof (T);
  740. T component = (T) Entity.Create(type, isFromPool);
  741. component.Id = IdGenerater.Instance.GenerateId();
  742. component.Parent = this;
  743. EventSystem.Instance.Awake(component);
  744. return component;
  745. }
  746. public T AddChild<T, A>(A a, bool isFromPool = false) where T : Entity, IAwake<A>
  747. {
  748. Type type = typeof (T);
  749. T component = (T) Entity.Create(type, isFromPool);
  750. component.Id = IdGenerater.Instance.GenerateId();
  751. component.Parent = this;
  752. EventSystem.Instance.Awake(component, a);
  753. return component;
  754. }
  755. public T AddChild<T, A, B>(A a, B b, bool isFromPool = false) where T : Entity, IAwake<A, B>
  756. {
  757. Type type = typeof (T);
  758. T component = (T) Entity.Create(type, isFromPool);
  759. component.Id = IdGenerater.Instance.GenerateId();
  760. component.Parent = this;
  761. EventSystem.Instance.Awake(component, a, b);
  762. return component;
  763. }
  764. public T AddChild<T, A, B, C>(A a, B b, C c, bool isFromPool = false) where T : Entity, IAwake<A, B, C>
  765. {
  766. Type type = typeof (T);
  767. T component = (T) Entity.Create(type, isFromPool);
  768. component.Id = IdGenerater.Instance.GenerateId();
  769. component.Parent = this;
  770. EventSystem.Instance.Awake(component, a, b, c);
  771. return component;
  772. }
  773. 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>
  774. {
  775. Type type = typeof (T);
  776. T component = (T) Entity.Create(type, isFromPool);
  777. component.Id = IdGenerater.Instance.GenerateId();
  778. component.Parent = this;
  779. EventSystem.Instance.Awake(component, a, b, c, d);
  780. return component;
  781. }
  782. public T AddChildWithId<T>(long id, bool isFromPool = false) where T : Entity, IAwake, new()
  783. {
  784. Type type = typeof (T);
  785. T component = Entity.Create(type, isFromPool) as T;
  786. component.Id = id;
  787. component.Parent = this;
  788. EventSystem.Instance.Awake(component);
  789. return component;
  790. }
  791. public T AddChildWithId<T, A>(long id, A a, bool isFromPool = false) where T : Entity, IAwake<A>
  792. {
  793. Type type = typeof (T);
  794. T component = (T) Entity.Create(type, isFromPool);
  795. component.Id = id;
  796. component.Parent = this;
  797. EventSystem.Instance.Awake(component, a);
  798. return component;
  799. }
  800. public T AddChildWithId<T, A, B>(long id, A a, B b, bool isFromPool = false) where T : Entity, IAwake<A, B>
  801. {
  802. Type type = typeof (T);
  803. T component = (T) Entity.Create(type, isFromPool);
  804. component.Id = id;
  805. component.Parent = this;
  806. EventSystem.Instance.Awake(component, a, b);
  807. return component;
  808. }
  809. 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>
  810. {
  811. Type type = typeof (T);
  812. T component = (T) Entity.Create(type, isFromPool);
  813. component.Id = id;
  814. component.Parent = this;
  815. EventSystem.Instance.Awake(component, a, b, c);
  816. return component;
  817. }
  818. }
  819. }