PrefabManager.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using UniFramework.Pooling;
  2. using UnityEngine;
  3. using System.Collections.Generic;
  4. using YooAsset;
  5. namespace GFGGame
  6. {
  7. public class PrefabManager : SingletonBase<PrefabManager>
  8. {
  9. private Spawner _entitySpawner;
  10. public void Init()
  11. {
  12. // 创建游戏对象发生器
  13. _entitySpawner = UniPooling.CreateSpawner(VersionController.DefaultPackage);
  14. }
  15. public GameObject SpawnSync(string resPath)
  16. {
  17. if (!_entitySpawner.IsGameObjectPoolExisted(resPath))
  18. {
  19. // 创建游戏对象池,销毁时间单位为秒
  20. _entitySpawner.CreateGameObjectPoolAsync(resPath, false, 0, 1, 60);
  21. }
  22. var handle = _entitySpawner.SpawnSync(resPath);
  23. var assetReleaser = handle.GameObj.GetComponent<AssetReleaser>();
  24. if (assetReleaser == null)
  25. {
  26. assetReleaser = handle.GameObj.AddComponent<AssetReleaser>();
  27. }
  28. assetReleaser.SetSpawn(resPath, handle);
  29. return handle.GameObj;
  30. }
  31. public GameObject InstantiateSync(string resPath)
  32. {
  33. AssetOperationHandle handle = YooAssets.LoadAssetSync<GameObject>(resPath);
  34. GameObject gameObject = handle.InstantiateSync();
  35. AssetReleaser assetReleaser = gameObject.AddComponent<AssetReleaser>();
  36. assetReleaser.SetRes(resPath, handle);
  37. return gameObject;
  38. }
  39. public void Restore(GameObject gameObject)
  40. {
  41. if (gameObject == null)
  42. {
  43. return;
  44. }
  45. AssetReleaser assetRestorer = gameObject.GetComponent<AssetReleaser>();
  46. if (assetRestorer != null && assetRestorer.IsSpawn)
  47. {
  48. assetRestorer.Restore();
  49. }
  50. else
  51. {
  52. GameObject.DestroyImmediate(gameObject);
  53. }
  54. }
  55. }
  56. }