8
8
using DCL . CharacterMotion . Components ;
9
9
using DCL . Diagnostics ;
10
10
using DCL . ECSComponents ;
11
+ using DCL . Optimization . Pools ;
11
12
using ECS . Abstract ;
12
13
using ECS . LifeCycle ;
13
14
using ECS . LifeCycle . Components ;
14
15
using ECS . Prioritization . Components ;
15
16
using ECS . Unity . AvatarShape . Components ;
16
17
using ECS . Unity . Groups ;
17
18
using ECS . Unity . Transforms . Components ;
19
+ using UnityEngine ;
18
20
using Utility . Arch ;
19
21
20
22
namespace ECS . Unity . AvatarShape . Systems
@@ -25,10 +27,12 @@ namespace ECS.Unity.AvatarShape.Systems
25
27
public partial class AvatarShapeHandlerSystem : BaseUnityLoopSystem , IFinalizeWorldSystem
26
28
{
27
29
private readonly World globalWorld ;
30
+ private readonly IComponentPool < Transform > globalTransformPool ;
28
31
29
- public AvatarShapeHandlerSystem ( World world , World globalWorld ) : base ( world )
32
+ public AvatarShapeHandlerSystem ( World world , World globalWorld , IComponentPool < Transform > globalTransformPool ) : base ( world )
30
33
{
31
34
this . globalWorld = globalWorld ;
35
+ this . globalTransformPool = globalTransformPool ;
32
36
}
33
37
34
38
protected override void Update ( float t )
@@ -44,9 +48,14 @@ protected override void Update(float t)
44
48
[ None ( typeof ( SDKAvatarShapeComponent ) , typeof ( DeleteEntityIntention ) ) ]
45
49
private void LoadAvatarShape ( Entity entity , ref PBAvatarShape pbAvatarShape , ref PartitionComponent partitionComponent , ref TransformComponent transformComponent )
46
50
{
51
+ // We have to create a global transform to hold the CharacterTransform. Using the Transform from the TransformComponent
52
+ // may lead to unexpected consequences, since that one is disposed by the scene, while the avatar lives in the global world
53
+ Transform globalTransform = globalTransformPool . Get ( ) ;
54
+ globalTransform . SetParent ( transformComponent . Transform ) ;
55
+
47
56
var globalWorldEntity = globalWorld . Create (
48
57
pbAvatarShape , partitionComponent ,
49
- new CharacterTransform ( transformComponent . Transform ) ,
58
+ new CharacterTransform ( globalTransform ) ,
50
59
new CharacterInterpolationMovementComponent ( transformComponent . Transform . position , transformComponent . Transform . position , transformComponent . Transform . rotation ) ,
51
60
new CharacterAnimationComponent ( ) ,
52
61
new CharacterEmoteComponent ( ) ) ;
@@ -95,8 +104,8 @@ public void FinalizeComponents(in Query query) =>
95
104
96
105
public void MarkGlobalWorldEntityForDeletion ( Entity globalEntity )
97
106
{
98
- // Has to be removed, otherwise scene loading may break after teleportation (no error anywhere to know why)
99
- globalWorld . Remove < CharacterTransform > ( globalEntity ) ;
107
+ // Need to remove parenting, since it may unintenionally deleted when
108
+ globalWorld . Get < CharacterTransform > ( globalEntity ) . Transform . SetParent ( null ) ;
100
109
101
110
// Has to be deferred because many times it happens that the entity is marked for deletion AFTER the
102
111
// AvatarCleanUpSystem.Update() and BEFORE the DestroyEntitiesSystem.Update(), probably has to do with
0 commit comments