1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- using UniFramework.Pooling;
- using UnityEngine;
- using System.Collections.Generic;
- using YooAsset;
- namespace GFGGame
- {
- public class PrefabManager : SingletonBase<PrefabManager>
- {
- private Spawner _entitySpawner;
- public void Init()
- {
- // 创建游戏对象发生器
- _entitySpawner = UniPooling.CreateSpawner(VersionController.DefaultPackage);
- }
- public GameObject SpawnSync(string resPath)
- {
- if (!_entitySpawner.IsGameObjectPoolExisted(resPath))
- {
- // 创建游戏对象池,销毁时间单位为秒
- _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);
- }
- }
- }
- }
|