Search Unity

Help with LoadAsync

Discussion in 'Scripting' started by Deleted User, Jun 16, 2015.

  1. Deleted User

    Deleted User

    Guest

    Hi all. Theres a way to load my screen one time on menu screen and keep this screen loaded on memory, cache, or something, and when click on play the screen is already loaded, and if the player exits to menu and play again, dont need to load anymore.
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    You can use LoadLevelAdditiveAsync and just have the level begin 'inactive' or far away from the main action where the player can't see it; when they click 'play', everything gets activated.
     
    Deleted User likes this.
  3. Patico

    Patico

    Joined:
    May 21, 2013
    Posts:
    886
  4. Deleted User

    Deleted User

    Guest

    Thanks StarManta. Can you post a example code to do that please? thanks
     
  5. Deleted User

    Deleted User

    Guest

    I try to make this code, but when click on start button my scene "fase1" dont appear. can anyone help me. and it seems the AsyncOperation never reach the isDone status. my "loadingPanel" never set active to false...

    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;

    4. public class MenuScript : MonoBehaviour {

    5. public GameObject creditsPanel;

    6. public AudioSource btnClick;
    7. public AudioSource bgMusic;

    8. public SpriteRenderer sndButton;
    9. public Sprite soundOn;
    10. public Sprite soundOff;

    11. public GameObject loadingPanel;
    12. public Slider slider;

    13. private AsyncOperation loadingOp = null;

    14. int soundOption;

    15. void Start()
    16. {
    17. StartCoroutine (LoadScene());

    18. soundOption = PlayerPrefs.GetInt ("sound", 1);

    19. SoundController ();
    20. }

    21. IEnumerator LoadScene(){
    22. loadingOp = Application.LoadLevelAsync ("fase1");
    23. loadingOp.allowSceneActivation = false;
    24. yield return loadingOp;
    25. }
    26. // Update is called once per frame
    27. void Update () {
    28. if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) {
    29. RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint((Input.GetTouch(0).position)), Vector2.zero);
    30. if (hit.collider != null) {

    31. btnClick.Play ();

    32. if (hit.collider.tag == "Play") {
    33. if (loadingOp.isDone) {
    34. //old code - load the level evertime when back to menu
    35. //loadingOp = Application.LoadLevelAsync("fase1");
    36. loadingOp.allowSceneActivation = true;
    37. }
    38. }
    39. else if (hit.collider.tag == "Credits") {
    40. creditsPanel.SetActive(true);
    41. }
    42. else if (hit.collider.tag == "Quit") {
    43. Application.Quit();
    44. }
    45. else if (hit.collider.tag == "Sound") {
    46. if (soundOption == 1) {
    47. soundOption = 0;
    48. }
    49. else {
    50. soundOption = 1;
    51. }

    52. SoundController();
    53. PlayerPrefs.SetInt("sound", soundOption);
    54. }
    55. }
    56. }

    57. if (loadingOp != null) {

    58. if (loadingOp.isDone) {
    59. loadingPanel.SetActive(false);
    60. }
    61. else{
    62. loadingPanel.SetActive(true);
    63. slider.value = loadingOp.progress;
    64. }
    65. }
    66. }

    67. public void btnBack(){
    68. btnClick.Play ();
    69. creditsPanel.SetActive (false);
    70. }

    71. void SoundController(){
    72. if (soundOption == 1) {
    73. bgMusic.Play();
    74. btnClick.volume = 1;
    75. sndButton.sprite = soundOn;
    76. }
    77. else {
    78. bgMusic.Stop();
    79. btnClick.volume = 0;
    80. sndButton.sprite = soundOff;
    81. }
    82. }
    83. }