TestLevel.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Cysharp.Threading.Tasks;
  5. using UnityEngine;
  6. using UnityEngine.AddressableAssets;
  7. using UnityEngine.ResourceManagement.AsyncOperations;
  8. public class TestLevel : MonoBehaviour
  9. {
  10. [SerializeField] private AssetReference papers;
  11. [SerializeField] private FoldLinesGeneratorNew foldLinesGenerator;
  12. private Paper currentPaper;
  13. public delegate void OnPaperInstantiated(Paper paper);
  14. public static OnPaperInstantiated onPaperInstantiated;
  15. async void Start()
  16. {
  17. onPaperInstantiated += foldLinesGenerator.DrawFoldingLines;
  18. await this.SpawnLevel();
  19. }
  20. // Update is called once per frame
  21. public async UniTask SpawnLevel()
  22. {
  23. Paper levelPrefab = await LoadPaperLevel(0);
  24. currentPaper = Instantiate(levelPrefab, transform);
  25. onPaperInstantiated?.Invoke(currentPaper);
  26. currentPaper.setPaperCallBack(this.levelComplete);
  27. }
  28. private async UniTask<Paper> LoadPaperLevel(int level)
  29. {
  30. GameObject paperGameObject = null;
  31. if (papers.Asset is GameObject)
  32. paperGameObject = papers.Asset as GameObject;
  33. if (paperGameObject == null)
  34. {
  35. AsyncOperationHandle<GameObject> paperHandler = papers.LoadAssetAsync<GameObject>();
  36. paperGameObject = await paperHandler.Task;
  37. }
  38. return paperGameObject.GetComponent<Paper>();
  39. }
  40. private void levelComplete()
  41. {
  42. Debug.Log("Level Complete");
  43. }
  44. }