Skip to content

Commit ed78dac

Browse files
authored
Fixed component init not happening with multiple required (#3)
1 parent a0598af commit ed78dac

File tree

3 files changed

+117
-39
lines changed

3 files changed

+117
-39
lines changed

packages/com.seaube.ecsact/Editor/EcsactUnitySyncDebugWindow.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static EcsactUnitySyncGameObjectPreview CreateInstance() {
2929
protected override void OnEnable() {
3030
base.OnEnable();
3131
scene = EditorSceneManager.NewPreviewScene();
32-
pool = EntityGameObjectPool.CreateInstance();
32+
pool = EntityGameObjectPool.CreateInstance(null!);
3333
pool.targetScene = scene;
3434

3535
if(Camera.main != null) {

packages/com.seaube.ecsact/Runtime/EcsactUnitySync.cs

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,39 @@ public static IEnumerable<Type> GetInterfaces
374374
}
375375
}
376376

377+
public static IEnumerable<System.Int32> GetInitComponentIds
378+
( System.Type type
379+
)
380+
{
381+
if(onInitComponentsMap.TryGetValue(type, out var compIds)) {
382+
foreach(var compId in compIds) {
383+
yield return compId;
384+
}
385+
}
386+
}
387+
388+
public static IEnumerable<System.Int32> GetRemoveComponentIds
389+
( System.Type type
390+
)
391+
{
392+
if(onInitComponentsMap.TryGetValue(type, out var compIds)) {
393+
foreach(var compId in compIds) {
394+
yield return compId;
395+
}
396+
}
397+
}
398+
399+
public static IEnumerable<System.Int32> GetRequiredComponentIds
400+
( System.Type type
401+
)
402+
{
403+
if(requiredComponentsMap.TryGetValue(type, out var compIds)) {
404+
foreach(var compId in compIds) {
405+
yield return compId;
406+
}
407+
}
408+
}
409+
377410
public static void ClearRegisteredMonoBehaviourTypes() {
378411
knownComponentIds.Clear();
379412
knownComponentTypes.Clear();
@@ -474,15 +507,6 @@ private static void EndMonoBehaviourRegistration
474507
( Type monoBehaviourType
475508
)
476509
{
477-
var compIds = new ComponentIdsList();
478-
var reqCompIds = new ComponentIdsList();
479-
480-
reqCompIds.UnionWith(requiredComponentsMap[monoBehaviourType]);
481-
compIds.UnionWith(onInitComponentsMap[monoBehaviourType]);
482-
compIds.UnionWith(onUpdateComponentsMap[monoBehaviourType]);
483-
compIds.UnionWith(onRemoveComponentsMap[monoBehaviourType]);
484-
485-
486510
}
487511

488512
private static void RegisterRequiredInterface

packages/com.seaube.ecsact/Runtime/EntityGameObjectPool.cs

Lines changed: 83 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,42 @@
1212

1313
namespace 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

Comments
 (0)