riversong code showcase

This commit is contained in:
Daniele Marotta
2026-05-21 15:52:18 +02:00
commit 4c9eea1c02
462 changed files with 23406 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
using UnityEngine.Pool;
namespace DanieleMarotta.RiversongCodeShowcase
{
public interface IPoolingService
{
void AddPool<T>(int key, IObjectPool<T> pool) where T : class;
IObjectPool<T> GetPool<T>(int key) where T : class;
}
}

View File

@@ -0,0 +1,9 @@
using UnityEngine;
namespace DanieleMarotta.RiversongCodeShowcase
{
public class PooledObject : MonoBehaviour
{
public int PoolKey { get; set; }
}
}

View File

@@ -0,0 +1,20 @@
using System.Collections.Generic;
using UnityEngine.Pool;
namespace DanieleMarotta.RiversongCodeShowcase
{
public class PoolingService : IPoolingService
{
private Dictionary<int, object> _poolLookup = new();
public void AddPool<T>(int key, IObjectPool<T> pool) where T : class
{
_poolLookup.Add(key, pool);
}
public IObjectPool<T> GetPool<T>(int key) where T : class
{
return _poolLookup.TryGetValue(key, out var pool) ? (IObjectPool<T>)pool : null;
}
}
}

View File

@@ -0,0 +1,88 @@
using UnityEngine;
using UnityEngine.Pool;
using Object = UnityEngine.Object;
namespace DanieleMarotta.RiversongCodeShowcase
{
public static class PoolingServiceExtensions
{
public static IObjectPool<GameObject> GetOrCreatePool(this IPoolingService poolingService, GameObject prefab, Transform folder = null)
{
var poolKey = prefab.GetInstanceID();
var pool = poolingService.GetPool<GameObject>(poolKey);
if (pool != null) return pool;
pool = new ObjectPool<GameObject>(
() =>
{
var go = Object.Instantiate(prefab, folder);
OnCreate(go, poolKey);
return go;
},
OnGet,
go => OnRelease(go, folder));
poolingService.AddPool(poolKey, pool);
return pool;
}
public static IObjectPool<T> GetOrCreatePool<T>(this IPoolingService poolingService, T prefab, Transform folder = null) where T : Component
{
var poolKey = prefab.GetInstanceID();
var pool = poolingService.GetPool<T>(poolKey);
if (pool != null) return pool;
pool = new ObjectPool<T>(
() =>
{
var component = Object.Instantiate(prefab, folder);
OnCreate(component.gameObject, poolKey);
return component;
},
component => OnGet(component.gameObject),
component => OnRelease(component.gameObject, folder));
poolingService.AddPool(poolKey, pool);
return pool;
}
private static void OnCreate(GameObject go, int poolKey)
{
go.AddComponent<PooledObject>().PoolKey = poolKey;
go.SetActive(false);
}
private static void OnGet(GameObject go)
{
go.transform.SetParent(null);
go.SetActive(true);
}
private static void OnRelease(GameObject go, Transform folder)
{
go.SetActive(false);
go.transform.SetParent(folder);
}
public static T Get<T>(this IPoolingService poolingService, int poolKey) where T : class
{
return poolingService.GetPool<T>(poolKey).Get();
}
public static void Release(this IPoolingService poolingService, GameObject go)
{
var poolKey = go.GetComponent<PooledObject>().PoolKey;
poolingService.GetPool<GameObject>(poolKey).Release(go);
}
public static void Release<T>(this IPoolingService poolingService, T component) where T : Component
{
var poolKey = component.GetComponent<PooledObject>().PoolKey;
poolingService.GetPool<T>(poolKey).Release(component);
}
}
}

View File

@@ -0,0 +1,31 @@
using Cysharp.Threading.Tasks;
using UnityEngine;
namespace DanieleMarotta.RiversongCodeShowcase
{
[GameSystemGroup(typeof(EarlyGameSystemGroup))]
[InitializeAfter(typeof(PreLoadAssetsSystem))]
public class PoolsInitializationSystem : GameSystem, IInitializable
{
[InjectService]
private IPoolingService _poolingService;
[InjectService]
private IGameDatabase _gameDatabase;
public PoolsInitializationSystem(IServiceLocator serviceLocator) : base(serviceLocator)
{
}
public UniTask InitializeAsync()
{
foreach (var product in _gameDatabase.OfType<ProductDefinition>())
{
_poolingService.GetOrCreatePool((GameObject)product.ProductStackVisualization.Asset);
_poolingService.GetOrCreatePool((GameObject)product.CarriedVisualization.Asset);
}
return UniTask.CompletedTask;
}
}
}