BackVillage.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. public class BackVillage : MonoBehaviour
  6. {
  7. // Start is called before the first frame update
  8. private AsyncOperation async;//异步加载的对象
  9. private int Progress = 0;//定义进度条数
  10. void Awake()
  11. {
  12. }
  13. void Start()
  14. {
  15. StartCoroutine(LoadAsync());
  16. }
  17. // Update is called once per frame
  18. void Update()
  19. {
  20. if (Input.GetKeyDown(KeyCode.B))
  21. { StartCoroutine(LoadAsync()); }
  22. if (async == null)
  23. {
  24. return;
  25. }
  26. int toProcess;
  27. if (async.progress < 0.9f)
  28. {
  29. toProcess = (int)(async.progress * 100);
  30. }
  31. else
  32. {
  33. toProcess = 100;
  34. }
  35. if (Progress < toProcess)
  36. {
  37. Progress++;
  38. Debug.Log(Progress + "进度");
  39. }
  40. if (Progress == 100)
  41. {
  42. Debug.Log("跳转");
  43. }
  44. }
  45. public void tiaozhuan()
  46. {
  47. if (Progress>99)
  48. {
  49. async.allowSceneActivation = true;
  50. }
  51. }
  52. IEnumerator LoadAsync()
  53. {
  54. async = SceneManager.LoadSceneAsync(1);//加上场景名
  55. async.allowSceneActivation = false;//不激活场景转换
  56. yield return async;
  57. }
  58. }