| | 1 | | using System; |
| | 2 | | using DCL.ECSRuntime; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | namespace DCL.ECSComponents |
| | 6 | | { |
| | 7 | | public class AvatarShapeRegister : IDisposable |
| | 8 | | { |
| | 9 | | private const string AVATAR_POOL_NAME = "AvatarShapeECS7"; |
| | 10 | | private const string AVATAR_PREFAB_PATH = "NewAvatarShape"; |
| | 11 | |
|
| | 12 | | private readonly ECSComponentsFactory factory; |
| | 13 | | private readonly IECSComponentWriter componentWriter; |
| | 14 | | private readonly int componentId; |
| | 15 | |
|
| | 16 | | private Pool pool; |
| | 17 | |
|
| 21 | 18 | | public AvatarShapeRegister(int componentId, ECSComponentsFactory factory, IECSComponentWriter componentWriter) |
| | 19 | | { |
| 21 | 20 | | AvatarShape avatarShapePrefab = Resources.Load<AvatarShape>(AVATAR_PREFAB_PATH); |
| 21 | 21 | | ConfigurePool(avatarShapePrefab.gameObject); |
| 21 | 22 | | factory.AddOrReplaceComponent(componentId, AvatarShapeSerializer.Deserialize, () => new AvatarShapeComponent |
| 21 | 23 | | componentWriter.AddOrReplaceComponentSerializer<PBAvatarShape>(componentId, AvatarShapeSerializer.Serialize) |
| | 24 | |
|
| 21 | 25 | | this.factory = factory; |
| 21 | 26 | | this.componentWriter = componentWriter; |
| 21 | 27 | | this.componentId = componentId; |
| 21 | 28 | | } |
| | 29 | |
|
| | 30 | | public void Dispose() |
| | 31 | | { |
| 2 | 32 | | factory.RemoveComponent(componentId); |
| 2 | 33 | | componentWriter.RemoveComponentSerializer(componentId); |
| | 34 | |
|
| 2 | 35 | | PoolManager.i.RemovePool(AVATAR_POOL_NAME); |
| 2 | 36 | | pool.ReleaseAll(); |
| 2 | 37 | | } |
| | 38 | |
|
| | 39 | | internal void ConfigurePool(GameObject prefab) |
| | 40 | | { |
| 21 | 41 | | pool = PoolManager.i.GetPool(AVATAR_POOL_NAME); |
| 21 | 42 | | if (pool != null) |
| 18 | 43 | | return; |
| | 44 | |
|
| 3 | 45 | | pool = PoolManager.i.AddPool( |
| | 46 | | AVATAR_POOL_NAME, |
| | 47 | | GameObject.Instantiate(prefab).gameObject, |
| | 48 | | isPersistent: true); |
| | 49 | |
|
| 3 | 50 | | pool.ForcePrewarm(); |
| 3 | 51 | | } |
| | 52 | | } |
| | 53 | | } |