Search Unity

[ SOLVED ] Async Operation Not Waiting Second Time Round

Discussion in 'Scripting' started by Deleted User, Jan 25, 2020.

  1. Deleted User

    Deleted User

    Guest

    Hi,

    The following code displays a privacy policy agreement at the start, when the user accepts, I save the response to the PlayerPrefs, and then I hide the Privacy Policy panel ( never to be shown again ), showing the 'loading' panel behind it, which preloads my main scene and waits for the user to tap to continue. This works first time round, but ... after quitting the app and then going back in, it's jumping straight to my main scene, so it's no longer waiting for user input, not sure why... ? Can anyone offer any advice ?
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class Startup : MonoBehaviour
    7. {
    8.     [Header("Privacy Policy")]
    9.  
    10.     public GameObject privacyPanel;
    11.     public string privacyPolicyURL;
    12.  
    13.     private string policyKey = "policy";
    14.  
    15.     [Header("Loading References")]
    16.  
    17.     public Text percentageText;
    18.     public Slider loadingSlider;
    19.     public GameObject tapText;
    20.  
    21.     void Awake()
    22.     {
    23.         // Have we shown the Privacy Policy before ?
    24.  
    25.         bool accepted = PlayerPrefs.GetInt(policyKey, 0) == 1;
    26.  
    27.         // If yes, start preloading main scene
    28.  
    29.         if (accepted)
    30.             PreloadScene();
    31.  
    32.         // Else, show the Privacy Policy
    33.  
    34.         privacyPanel.SetActive(true);
    35.     }
    36.  
    37.     // Access this function from the Accept button
    38.  
    39.     public void AcceptPrivacyPolicy()
    40.     {
    41.         // Hide Privacy Policy Panel
    42.  
    43.         privacyPanel.SetActive(false);
    44.  
    45.         // Privacy Policy has been accepted, save response to PlayerPrefs
    46.  
    47.         PlayerPrefs.SetInt(policyKey, 1);
    48.         PlayerPrefs.Save();
    49.  
    50.         // Call the function to start preloading the main scene
    51.  
    52.         PreloadScene();
    53.     }
    54.  
    55.     // Start preloading our main scene
    56.  
    57.     public void PreloadScene()
    58.     {
    59.         StartCoroutine(AsyncLoad());
    60.     }
    61.  
    62.     IEnumerator AsyncLoad()
    63.     {
    64.         // Load the main scene asynchronously ( in the background of this scene )
    65.  
    66.         AsyncOperation asyncOperation = SceneManager.LoadSceneAsync("Main");
    67.  
    68.         // Don't activate scene yet
    69.  
    70.         asyncOperation.allowSceneActivation = false;
    71.  
    72.         // Wait until the asynchronous scene is fully loaded
    73.  
    74.         while (!asyncOperation.isDone)
    75.         {
    76.             // Fill our loading slider
    77.  
    78.             loadingSlider.value = asyncOperation.progress / 0.9f;
    79.  
    80.             // Update our progression text
    81.  
    82.             percentageText.text = string.Format("{0:0}%", loadingSlider.value * 100);
    83.  
    84.             // Check if the scene has finished loading
    85.  
    86.             if (asyncOperation.progress >= 0.9f)
    87.             {
    88.                 tapText.SetActive(true);
    89.  
    90.                 if (Input.GetMouseButtonDown(0))
    91.                 {
    92.                     asyncOperation.allowSceneActivation = true;
    93.                 }
    94.             }
    95.  
    96.             yield return null;
    97.         }
    98.     }
    99. }
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    I notice on line 32, you have a comment that says "else", but you don't actually have any code that says "else", so the line below it runs whether "accepted" was true or not.

    And I recommend against putting the body of an "if" statement on a separate line but with no curly braces, as you are doing on line 30. This pattern is famously responsible for some really bad coding errors.


    I don't see a reason for the problem you are describing, though. Are you certain that you actually quit the app, rather than merely suspending it?
     
  3. Deleted User

    Deleted User

    Guest

    Thanks,

    I usually use the curly brackets, however, when it's 1 statement, I don't use them, I wasn't aware it was frowned upon to do this, but that's an easy fix ! ;)
    Code (CSharp):
    1. void Awake()
    2.     {
    3.         // Have we shown the Privacy Policy before ?
    4.  
    5.         bool accepted = PlayerPrefs.GetInt(policyKey, 0) == 1;
    6.  
    7.         // If yes, start preloading main scene, else, show the Privacy Policy
    8.  
    9.         if (accepted)
    10.         {
    11.             PreloadScene();
    12.         }
    13.         else
    14.         {
    15.             privacyPanel.SetActive(true);
    16.         }
    17.     }
    Ok, so I'm just testing this inside of Unity, no 'built' application has been made.

    So, I start the game in the game view
    Next, my privacy policy is shown, I click accept
    Next, the privacy policy is hidden, and my 'loading' panel is shown,
    The loading panel 'loads', and wait's for the user to 'tap to continue'
    user taps to continue, main scene loads

    Quit the game view

    Run the game view again, jumps directly to the main scene, almost bypassing the loading scene.
    It should check if privacy policy has been shown, it has, and then do the loading part again, and WAIT for user input, however, it does not wait at all. I added a debug.log to check the status of asyncOperation.allowSceneActivation and it tells me it is false, so why is it jumping directly to the next scene, without any user input ?
     
  4. Deleted User

    Deleted User

    Guest

    Never mind, I fixed it, issue was return value, posting code here for future reference :
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class Startup : MonoBehaviour
    7. {
    8.     [Header("Privacy Policy")]
    9.  
    10.     public GameObject privacyPanel;
    11.     public string privacyPolicyURL;
    12.  
    13.     private string policyKey = "policy";
    14.  
    15.     [Header("Loading References")]
    16.  
    17.     public Text percentageText;
    18.     public Slider loadingSlider;
    19.     public GameObject tapText;
    20.  
    21.     void Awake()
    22.     {
    23.         // Have we shown the Privacy Policy before ?
    24.  
    25.         bool accepted = PlayerPrefs.GetInt(policyKey, 0) == 1;
    26.  
    27.         // If yes, start preloading main scene, else, show the Privacy Policy
    28.  
    29.         if (accepted)
    30.         {
    31.             PreloadScene();
    32.         }
    33.         else
    34.         {
    35.             privacyPanel.SetActive(true);
    36.         }
    37.     }
    38.  
    39.     // Access this function from the Accept button
    40.  
    41.     public void AcceptPrivacyPolicy()
    42.     {
    43.         // Hide Privacy Policy Panel
    44.  
    45.         privacyPanel.SetActive(false);
    46.  
    47.         // Privacy Policy has been accepted, save response to PlayerPrefs
    48.  
    49.         PlayerPrefs.SetInt(policyKey, 1);
    50.         PlayerPrefs.Save();
    51.  
    52.         // Call the function to start preloading the main scene
    53.  
    54.         PreloadScene();
    55.     }
    56.  
    57.     // Access this function from the Privacy Policy URL link
    58.  
    59.     public void PrivacyPolicyOonline()
    60.     {
    61.         // Open URL, based on the value given in the public string privacyPolicyURL
    62.  
    63.         Application.OpenURL(privacyPolicyURL);
    64.     }
    65.  
    66.     // Start preloading our main scene
    67.  
    68.     public void PreloadScene()
    69.     {
    70.         StartCoroutine(AsyncLoad());
    71.     }
    72.  
    73.     IEnumerator AsyncLoad()
    74.     {
    75.         yield return null;
    76.  
    77.         // Load the main scene asynchronously ( in the background of this scene )
    78.  
    79.         AsyncOperation asyncOperation = SceneManager.LoadSceneAsync("Main");
    80.  
    81.         // Don't activate scene yet
    82.  
    83.         asyncOperation.allowSceneActivation = false;
    84.  
    85.         // Wait until the asynchronous scene is fully loaded
    86.  
    87.         while (!asyncOperation.isDone)
    88.         {
    89.             // Fill our loading slider
    90.  
    91.             loadingSlider.value = asyncOperation.progress / 0.9f;
    92.  
    93.             // Update our progression text
    94.  
    95.             percentageText.text = string.Format("{0:0}%", loadingSlider.value * 100);
    96.  
    97.             // Check if the scene has finished loading
    98.  
    99.             if (asyncOperation.progress >= 0.9f)
    100.             {
    101.                 tapText.SetActive(true);
    102.  
    103.                 if (Input.GetMouseButtonDown(0))
    104.                 {
    105.                     asyncOperation.allowSceneActivation = true;
    106.                 }
    107.             }
    108.  
    109.             yield return null;
    110.         }
    111.     }
    112. }
     
    Last edited by a moderator: Jan 25, 2020