AssetReleaser.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using UniFramework.Pooling;
  2. using UnityEngine;
  3. using YooAsset;
  4. namespace GFGGame
  5. {
  6. public class AssetReleaser : MonoBehaviour
  7. {
  8. private string resPath;
  9. public string ResPath
  10. {
  11. get
  12. {
  13. return resPath;
  14. }
  15. }
  16. private AssetOperationHandle assetOperationHandle;
  17. private SpawnHandle spawnHandle;
  18. public bool IsSpawn
  19. {
  20. get { return spawnHandle != null; }
  21. }
  22. public void SetRes(string resPath, AssetOperationHandle handle)
  23. {
  24. Dispose();
  25. this.resPath = resPath;
  26. this.assetOperationHandle = handle;
  27. }
  28. public void SetSpawn(string resPath, SpawnHandle handle)
  29. {
  30. this.resPath = resPath;
  31. this.spawnHandle = handle;
  32. }
  33. private void OnDestroy()
  34. {
  35. //if (!string.IsNullOrEmpty(resPath))
  36. //{
  37. // GFGAsset.Release(resPath);
  38. //}
  39. // resPath = string.Empty;
  40. Dispose();
  41. }
  42. private void Dispose()
  43. {
  44. this.assetOperationHandle?.Release();
  45. this.assetOperationHandle = null;
  46. //this.spawnHandle?.Discard();
  47. this.spawnHandle = null;
  48. this.resPath = null;
  49. }
  50. public void Restore()
  51. {
  52. SetGoLayers(this.gameObject, 0);
  53. this.spawnHandle?.Restore();
  54. this.spawnHandle = null;
  55. }
  56. private static void SetGoLayers(GameObject gameObject, int layer)
  57. {
  58. if (gameObject == null || gameObject.layer == layer)
  59. return;
  60. var helperTransformList = gameObject.GetComponentsInChildren<Transform>(true);
  61. int cnt = helperTransformList.Length;
  62. for (int i = 0; i < cnt; i++)
  63. helperTransformList[i].gameObject.layer = layer;
  64. }
  65. }
  66. }