12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- using UniFramework.Pooling;
- using UnityEngine;
- using System.Collections.Generic;
- using YooAsset;
- namespace GFGGame
- {
- public class PrefabManager : SingletonBase<PrefabManager>
- {
- private Spawner _entitySpawner;
- private Dictionary<string, bool> poolCreated = new Dictionary<string, bool>();
- public void Init()
- {
- // 创建游戏对象发生器
- _entitySpawner = UniPooling.CreateSpawner(VersionController.DefaultPackage);
- }
- public GameObject SpawnSync(string resPath)
- {
- this.poolCreated.TryGetValue(resPath, out bool poolCreated);
- if (!poolCreated)
- {
- // 创建游戏对象池,销毁时间单位为秒
- _entitySpawner.CreateGameObjectPoolAsync(resPath, false, 0, 1, 60);
- }
- var handle = _entitySpawner.SpawnSync(resPath);
- var assetReleaser = handle.GameObj.GetComponent<AssetReleaser>();
- if (assetReleaser == null)
- {
- assetReleaser = handle.GameObj.AddComponent<AssetReleaser>();
- }
- assetReleaser.SetSpawn(resPath, handle);
- return handle.GameObj;
- }
- public GameObject InstantiateSync(string resPath)
- {
- AssetOperationHandle handle = YooAssets.LoadAssetSync<GameObject>(resPath);
- GameObject gameObject = handle.InstantiateSync();
- AssetReleaser assetReleaser = gameObject.AddComponent<AssetReleaser>();
- assetReleaser.SetRes(resPath, handle);
- return gameObject;
- }
- public void Restore(GameObject gameObject)
- {
- if (gameObject == null)
- {
- return;
- }
- AssetReleaser assetRestorer = gameObject.GetComponent<AssetReleaser>();
- if (assetRestorer != null && assetRestorer.IsSpawn)
- {
- assetRestorer.Restore();
- }
- else
- {
- GameObject.DestroyImmediate(gameObject);
- }
- }
- }
- }
|