1212
1313namespace Ecsact . UnitySync {
1414 public class EntityGameObjectPool : ScriptableObject {
15+ public abstract class EntitySource {
16+ public abstract object GetComponent
17+ ( System . Int32 entityId
18+ , System . Int32 componentId
19+ ) ;
20+ public abstract bool HasComponent
21+ ( System . Int32 entityId
22+ , System . Int32 componentId
23+ ) ;
24+ }
1525
16- public static EntityGameObjectPool CreateInstance ( ) {
17- return ( EntityGameObjectPool ) ScriptableObject . CreateInstance (
26+ public static EntityGameObjectPool CreateInstance
27+ ( EntitySource entitySource
28+ )
29+ {
30+ var pool = ( EntityGameObjectPool ) ScriptableObject . CreateInstance (
1831 typeof ( EntityGameObjectPool )
1932 ) ;
33+
34+ pool . _entitySource = entitySource ;
35+
36+ return pool ;
37+ }
38+
39+ private EntitySource ? _entitySource ;
40+ private EntitySource entitySource {
41+ get {
42+ UnityEngine . Debug . Assert (
43+ _entitySource != null ,
44+ "entitySource is unset. Please use " +
45+ "EntityGameObjectPool.CreateInstance when creating an " +
46+ "EntityGameObjectPool instance." ,
47+ this
48+ ) ;
49+ return _entitySource ! ;
50+ }
2051 }
2152
2253 private List < ComponentIdsList > entityComponentIds ;
@@ -155,11 +186,26 @@ public void InitComponent
155186 if ( onInitEntity != null ) {
156187 onInitEntity . OnInitEntity ( entityId ) ;
157188 }
189+ var initCompIds = UnitySyncMonoBehaviours . GetInitComponentIds ( type ) ;
190+ foreach ( var initCompId in initCompIds ) {
191+ if ( initCompId == componentId ) continue ;
192+ if ( ! entitySource . HasComponent ( entityId , initCompId ) ) continue ;
193+
194+ var initComponent = entitySource . GetComponent (
195+ entityId ,
196+ initCompId
197+ ) ;
198+ UnitySyncMonoBehaviours . InvokeOnInit (
199+ newMonoBehaviour ,
200+ initCompId ,
201+ in initComponent
202+ ) ;
203+ }
158204 }
159205 }
160206
161207 gameObject = gameObject ?? GetEntityGameObject ( entityId ) ;
162-
208+
163209 if ( gameObject != null ) {
164210 UnitySyncMonoBehaviours . InvokeOnInit (
165211 gameObject ,
@@ -188,18 +234,13 @@ public void UpdateComponent
188234 )
189235 {
190236 var gameObject = GetEntityGameObject ( entityId ) ;
191- if ( gameObject == null ) {
192- throw new System . ArgumentException (
193- $ "EntityGameObjectPool.UpdateComponent called before " +
194- $ "EntityGameObjectPool.InitComponent. entityId={ entityId } "
237+ if ( gameObject != null ) {
238+ UnitySyncMonoBehaviours . InvokeOnUpdate (
239+ gameObject ,
240+ componentId ,
241+ in component
195242 ) ;
196243 }
197-
198- UnitySyncMonoBehaviours . InvokeOnUpdate (
199- gameObject ,
200- componentId ,
201- in component
202- ) ;
203244 }
204245
205246 public void RemoveComponent < T >
@@ -237,6 +278,35 @@ public void RemoveComponent
237278 ) ;
238279
239280 var gameObject = entityGameObjects [ entityId ] ! ;
281+
282+ var allMonoBehaviourTypes = UnitySyncMonoBehaviours . GetTypes (
283+ nextCompIds
284+ ) ;
285+
286+ foreach ( var type in addedTypes ) {
287+ var newMonoBehaviour = ( MonoBehaviour ) gameObject . AddComponent ( type ) ;
288+ IOnInitEntity ? onInitEntity = newMonoBehaviour as IOnInitEntity ;
289+ if ( onInitEntity != null ) {
290+ onInitEntity . OnInitEntity ( entityId ) ;
291+ }
292+
293+ var initCompIds = UnitySyncMonoBehaviours . GetInitComponentIds ( type ) ;
294+ foreach ( var initCompId in initCompIds ) {
295+ if ( initCompId == componentId ) continue ;
296+ if ( ! entitySource . HasComponent ( entityId , initCompId ) ) continue ;
297+
298+ var initComponent = entitySource . GetComponent (
299+ entityId ,
300+ initCompId
301+ ) ;
302+ UnitySyncMonoBehaviours . InvokeOnInit (
303+ newMonoBehaviour ,
304+ initCompId ,
305+ in initComponent
306+ ) ;
307+ }
308+ }
309+
240310 UnitySyncMonoBehaviours . InvokeOnRemove (
241311 gameObject ,
242312 componentId ,
@@ -253,22 +323,6 @@ in component
253323 }
254324 }
255325
256- foreach ( var type in addedTypes ) {
257- var newMonoBehaviour = ( MonoBehaviour ) gameObject . AddComponent ( type ) ;
258- IOnInitEntity ? onInitEntity = newMonoBehaviour as IOnInitEntity ;
259- if ( onInitEntity != null ) {
260- onInitEntity . OnInitEntity ( entityId ) ;
261- }
262- UnitySyncMonoBehaviours . InvokeOnInit (
263- newMonoBehaviour ,
264- componentId ,
265- in component
266- ) ;
267- }
268-
269- var allMonoBehaviourTypes = UnitySyncMonoBehaviours . GetTypes (
270- nextCompIds
271- ) ;
272326 if ( ! allMonoBehaviourTypes . Any ( ) ) {
273327 gameObject . SetActive ( false ) ;
274328 gameObject . name = $ "entity ({ entityId } )";
0 commit comments