Search Unity

Question Loading a new Scene is taking forever

Discussion in 'Editor & General Support' started by qihuanaixinjueluo, Jun 23, 2021.

  1. qihuanaixinjueluo

    qihuanaixinjueluo

    Joined:
    Apr 7, 2021
    Posts:
    3
    Hi

    I am makeing a 2d card game and it takes about 3 minutes to change from one scene to another. There are nothing fancy in the new scene, just 2d UI things like panel, text, buttons and toggles? Can someone please tell me how can I improve that?

    Thanks
     
  2. qihuanaixinjueluo

    qihuanaixinjueluo

    Joined:
    Apr 7, 2021
    Posts:
    3
     

    Attached Files:

  3. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Please show your code. I suspect you may be doing something in Update() or FixedUpdate() that may be causing the delay.
     
  4. qihuanaixinjueluo

    qihuanaixinjueluo

    Joined:
    Apr 7, 2021
    Posts:
    3
    Code (CSharp):
    1. public class GameSceneController : MonoBehaviour, IPointerClickHandler
    2.     {
    3.         [HideInInspector]
    4.         public CardScriptable _selectedScriptable;
    5.         [HideInInspector]
    6.         public GameObject _selectedCard;
    7.         [HideInInspector]
    8.         public CardScriptable.CardClass[] _classes;
    9.         [HideInInspector]
    10.         public GameObject _slot;
    11.  
    12.         [SerializeField]
    13.         private GameObject _gridLayoutPanel;
    14.         [SerializeField]
    15.         private GameObject _scrollPanel;
    16.         [SerializeField]
    17.         private GameObject _OpenFormulaBtn;
    18.         [SerializeField]
    19.         private GameObject _formulaExpandCanvas;
    20.  
    21.         [SerializeField]
    22.         private Text _timeIndicator;
    23.         //[SerializeField]
    24.         //private GameObject _buttonText;
    25.  
    26.         [SerializeField]
    27.         private GameObject _characterToggle;
    28.         [SerializeField]
    29.         private GameObject _organizationToggle;
    30.         [SerializeField]
    31.         private GameObject _clueToggle;
    32.         [SerializeField]
    33.         private GameObject _formulaToggle;
    34.         [SerializeField]
    35.         private GameObject _conditionToggle;
    36.  
    37.         private List<CardScriptable> _cardsToShow;
    38.         private List<GameObject> _cardsShownObject;//keep track of all the clones of card prefabs
    39.  
    40.         private bool _cardSelected;
    41.  
    42.         [SerializeField]
    43.         [Tooltip("Transform of the selected card")]
    44.         private Transform _selectedCardDisplay;
    45.  
    46.         [Header("Dialog Panel")]
    47.         [SerializeField]
    48.         GameObject _dialogPanel;
    49.         [SerializeField]
    50.         GameObject _dialogText;
    51.         [SerializeField]
    52.         Transform _characterCardPos;
    53.         [SerializeField]
    54.         Transform _dialogSlotPos;
    55.         [SerializeField]
    56.         GameObject _dialogSlot;
    57.  
    58.         GameObject _characterCard;
    59.  
    60.         DialogScriptable _currDialog;
    61.         //GameObject _dialogSlot;
    62.  
    63.  
    64.  
    65.         // Start is called before the first frame update
    66.         /*void Awake()
    67.         {
    68.            
    69.         }*/
    70.  
    71.         void Start()
    72.         {
    73.             //_selectedScriptable = null;
    74.             //_cardsShownObject = new List<GameObject>();
    75.             UpdateClasses();
    76.             _OpenFormulaBtn.SetActive(false);
    77.  
    78.         }
    79.  
    80.         private void Awake()
    81.         {
    82.             _selectedScriptable = null;
    83.             _cardsShownObject = new List<GameObject>();
    84.             //_buttonText.GetComponent<Text>().text = "Return";
    85.             EventManager.Instance.ShowDialogEvent += ShowDialog;
    86.         }
    87.  
    88.         private void OnDestroy()
    89.         {
    90.             EventManager.Instance.ShowDialogEvent -= ShowDialog;
    91.         }
    92.         // Update is called once per frame
    93.         void Update()
    94.         {
    95.            
    96.  
    97.            
    98.         }
    99.         public void UpdateClasses()
    100.         {
    101.             List<CardScriptable.CardClass> tempClasses = new List<CardScriptable.CardClass>();
    102.             if (_characterToggle.GetComponent<Toggle>().isOn)
    103.             {
    104.                 tempClasses.Add(CardScriptable.CardClass.CHARACTER);
    105.             }
    106.             if (_organizationToggle.GetComponent<Toggle>().isOn)
    107.             {
    108.                 tempClasses.Add(CardScriptable.CardClass.ORGANIZATION);
    109.             }
    110.             if (_clueToggle.GetComponent<Toggle>().isOn)
    111.             {
    112.                 tempClasses.Add(CardScriptable.CardClass.CLUE);
    113.             }
    114.             if (_formulaToggle.GetComponent<Toggle>().isOn)
    115.             {
    116.                 tempClasses.Add(CardScriptable.CardClass.FORMULA);
    117.             }
    118.             if (_conditionToggle.GetComponent<Toggle>().isOn)
    119.             {
    120.                 tempClasses.Add(CardScriptable.CardClass.CONDITION);
    121.             }
    122.  
    123.             _classes = tempClasses.ToArray();
    124.  
    125.             RenewCards();
    126.         }
    127.  
    128.  
    129.  
    130.         public void InstantiateCards()
    131.         {
    132.             //CardScriptable _deselectCard = (CardScriptable)Resources.Load("Scriptables/DeselectCard");
    133.             _cardsToShow = GameProgress.Instance.GetCertainTypeCards(GameProgress.Instance._scriptablesObtained,_classes);
    134.             //_cardsToShow.Insert(0, _deselectCard);
    135.  
    136.             foreach (CardScriptable _scriptable in _cardsToShow)
    137.             {
    138.                 GameObject _tempCard = Instantiate(_scriptable.GetPrefab());
    139.                 if (_tempCard.GetComponent<Card>())
    140.                 {
    141.                     _tempCard.GetComponent<Card>().Initialize(_scriptable);
    142.                 }
    143.                 _tempCard.transform.SetParent(_gridLayoutPanel.transform);
    144.                 _tempCard.AddComponent<Clickable>();
    145.                 _tempCard.GetComponent<Clickable>().AddListener(delegate { CardOnClick(_scriptable); });
    146.                 _cardsShownObject.Add(_tempCard);
    147.  
    148.             }
    149.  
    150.         }
    151.  
    152.         public void CardOnClick(CardScriptable scriptable)
    153.         {
    154.             SetAsSelect(scriptable);
    155.         }
    156.  
    157.         private void SetAsSelect(CardScriptable scriptable)
    158.         {
    159.             //_buttonText.GetComponent<Text>().text = "Save";
    160.             _selectedScriptable = scriptable;
    161.             GameObject card = Instantiate(scriptable.GetPrefab());
    162.             card.GetComponent<Card>().Initialize(scriptable);
    163.             card.transform.SetParent(_scrollPanel.transform);
    164.             card.transform.localScale = new Vector3(5f, 5f, 5f);
    165.             card.transform.localPosition = _selectedCardDisplay.localPosition;
    166.             card.AddComponent<Clickable>();
    167.             card.GetComponent<Clickable>().AddListener(delegate { DeSelect(card); });
    168.             _selectedCard = card;
    169.             SetActiveCards(false);
    170.             if (_selectedScriptable != null)
    171.             {
    172.                 if (_selectedScriptable._class == CardScriptable.CardClass.FORMULA)
    173.                 {
    174.                     _OpenFormulaBtn.SetActive(true);
    175.                 }
    176.             }
    177.         }
    178.         /*public void DeleteSelectedCard()
    179.         {
    180.             if (_selectedScriptable != null)
    181.             {
    182.                 if (_selectedScriptable._class == CardScriptable.CardClass.FORMULA)
    183.                 {
    184.                     _OpenFormulaBtn.SetActive(true);
    185.                 }
    186.             }
    187.         }*/
    188.         public void DeSelect(GameObject card)
    189.         {
    190.             SetActiveCards(true);
    191.             Destroy(card);
    192.             _OpenFormulaBtn.SetActive(false);
    193.             //_buttonText.GetComponent<Text>().text = "Return";
    194.         }
    195.  
    196.         private void SetActiveCards(bool _active)
    197.         {
    198.             foreach (GameObject card in _cardsShownObject)
    199.             {
    200.                 card.GetComponent<Clickable>().ToggleInteractive(_active);
    201.             }
    202.         }
    203.  
    204.         private void RenewCards()
    205.         {
    206.             foreach (GameObject card in _cardsShownObject)
    207.             {
    208.                 Destroy(card);
    209.                
    210.             }
    211.  
    212.  
    213.             _cardsShownObject = new List<GameObject>();
    214.             InstantiateCards();
    215.  
    216.             _timeIndicator.text = "Current Time: " + GameProgress.Instance._time;
    217.         }
    218.  
    219.         public void ExitToHomePage()
    220.         {
    221.             SceneManager.LoadSceneAsync("StartScene", LoadSceneMode.Single);
    222.         }
    223.  
    224.         public void OpenFormulaCanvas()
    225.         {
    226.             GameObject formulaCanvas = Instantiate(_formulaExpandCanvas);
    227.             formulaCanvas.transform.SetParent(null);
    228.             formulaCanvas.GetComponent<FormulaExpandController>()._gameScene = gameObject;
    229.             formulaCanvas.GetComponent<FormulaExpandController>().FixFormulaSlot(_selectedScriptable);
    230.  
    231.         }
    232.  
    233.         public void CheckFormula(CardScriptable formulaScriptable, List<CardScriptable> slots)
    234.         {
    235.             GameProgress.Instance.CheckFormula(formulaScriptable,slots);
    236.  
    237.  
    238.             GameProgress.Instance.TimePast();
    239.             RenewCards();
    240.         }
    241.  
    242.         public void CheckDialog(DialogScriptable dialog, CardScriptable answer)
    243.         {
    244.             GameProgress.Instance.FinishDialog(dialog, answer);
    245.  
    246.             RenewCards();
    247.         }
    248.  
    249.         public void SaveToFile()
    250.         {
    251.             //SimpleFileBrowser.FileBrowser.OpenFolders
    252.  
    253.             string path = FileBrowser.Instance.OpenSingleFolder();
    254.             if (path.Length != 0)
    255.             {
    256.                 GameProgress.Instance.SaveCurrentProgress(path);
    257.             }
    258.            
    259.         }
    260.  
    261.         public void ShowDialog(DialogScriptable dialog)
    262.         {
    263.  
    264.             this._currDialog = dialog;
    265.  
    266.             _dialogPanel.SetActive(true);
    267.             _dialogSlot.SetActive(false);
    268.  
    269.             SetActiveCards(false);
    270.  
    271.             _dialogText.GetComponent<Text>().text = dialog._dialogContent;
    272.  
    273.             _characterCard = Instantiate(dialog._character._cardPrefab);
    274.             _characterCard.transform.SetParent(_dialogPanel.transform);
    275.             _characterCard.transform.localPosition = _characterCardPos.localPosition;
    276.  
    277.             if (dialog._isSlot)
    278.             {
    279.                 _dialogSlot.SetActive(true);
    280.                 _dialogSlot.GetComponent<SlotCanvasController>()._classes = dialog._slotClasses;
    281.             }
    282.  
    283.  
    284.  
    285.  
    286.  
    287.         }
    288.  
    289.         public void FinishDialog(DialogScriptable dialog)
    290.         {
    291.             if (_dialogPanel.activeSelf && _currDialog!=null)
    292.             {
    293.                 if ((dialog._isSlot == false) || (_dialogSlot.GetComponent<SlotController>()._isFilled))
    294.                 {
    295.  
    296.                     SetActiveCards(true);
    297.  
    298.                     _currDialog = null;
    299.                     Destroy(_characterCard);
    300.                     _dialogPanel.SetActive(false);
    301.  
    302.                     CheckDialog(dialog, _dialogSlot.GetComponent<SlotCanvasController>()._selectedScriptable);
    303.  
    304.                 }
    305.  
    306.             }
    307.  
    308.  
    309.  
    310.         }
    311.  
    312.         public void OnPointerClick(PointerEventData pointerEventData)
    313.         {
    314.             FinishDialog(_currDialog);
    315.         }
    316.  
    317.  
    318.     }
    Code (CSharp):
    1. public class StartSceneScript : MonoBehaviour
    2.     {
    3.         private List<CardScriptable> _cardsToShow;
    4.         GameObject _newCardPanel;
    5.  
    6.         //public static SaveDataGeneric<LoadFile> _save = null;
    7.         // Start is called before the first frame update
    8.        
    9.         private void Awake()
    10.         {
    11.             _newCardPanel = GameObject.Find("NewCardPanel");
    12.             _newCardPanel.SetActive(false);
    13.             _cardsToShow = new List<CardScriptable>();
    14.  
    15.             //Object[] _fullDeck = Resources.LoadAll("Scriptables");
    16.  
    17.  
    18.             //new Deck();
    19.            
    20.  
    21.          }
    22.         void Start()
    23.         {
    24.  
    25.             //Deck.Instance.CreateDeck();
    26.             /*
    27.             foreach (Object _cardScriptable in _fullDeck)
    28.             {
    29.                 _cardsToShow.Add((CardScriptable)_cardScriptable);
    30.                
    31.  
    32.             }
    33.  
    34.             foreach (CardScriptable _scriptable in _cardsToShow)
    35.             {
    36.                 GameObject _tempCard = Instantiate(_scriptable._cardPrefab);
    37.                 _tempCard.GetComponent<Card>().Initialize(_scriptable);
    38.             }
    39.             */
    40.         }
    41.  
    42.         // Update is called once per frame
    43.         void Update()
    44.         {
    45.  
    46.         }
    47.  
    48.         public void CreateNewCard()
    49.         {
    50.             _newCardPanel.SetActive(true);
    51.         }
    52.  
    53.         public void CloseNewCard()
    54.         {
    55.             _newCardPanel.SetActive(false);
    56.         }
    57.  
    58.         public void ManageCards()
    59.         {
    60.             SceneManager.LoadSceneAsync("ManageCardsScene", LoadSceneMode.Single);
    61.         }
    62.         public void CreateNewCharacterCard()
    63.         {
    64.             SceneManager.LoadSceneAsync("NewCharacterCardScene", LoadSceneMode.Single);
    65.         }
    66.  
    67.         public void CreateNewOrganizationCard()
    68.         {
    69.             SceneManager.LoadSceneAsync("NewOrganizationScene", LoadSceneMode.Single);
    70.         }
    71.  
    72.         public void CreateNewClueCard()
    73.         {
    74.             SceneManager.LoadSceneAsync("NewClueScene", LoadSceneMode.Single);
    75.         }
    76.  
    77.         public void CreateNewConditionCard()
    78.         {
    79.             SceneManager.LoadSceneAsync("NewConditionScene", LoadSceneMode.Single);
    80.         }
    81.  
    82.         public void CreateNewFormulaCard()
    83.         {
    84.             SceneManager.LoadSceneAsync("NewFormulaScene", LoadSceneMode.Single);
    85.         }
    86.  
    87.         public void CreateNewDialogCard()
    88.         {
    89.             SceneManager.LoadSceneAsync("NewDialogForm", LoadSceneMode.Single);  
    90.         }
    91.  
    92.         public void StartGame()
    93.         {
    94.             SceneManager.LoadScene("GameMainScene", LoadSceneMode.Single);
    95.         }
    96.  
    97.         public void DeleteCurrentGame()
    98.         {
    99.             Deck.Instance.DeleteCurrentSave();
    100.         }
    101.         public void DeleteCurrentProgress()
    102.         {
    103.             GameProgress.Instance.DeleteCurrentProgress();
    104.         }
    105.  
    106.         public void LoadGameProgressFromFile()
    107.         {
    108.             GameProgress.Instance.LoadGameProgressFromFile();
    109.         }
    110.         public void LoadFromFile()
    111.         {
    112.             //string path = FileBrowser.Instance.OpenSingleFile("es3");
    113.  
    114.             Deck.Instance.LoadFromFile();
    115.         }
    116.  
    117.         public void ExportToFile()
    118.         {
    119.             Deck.Instance.ExportToFile();
    120.         }
    121.     }
    I think there is nothing going in updates, here is the code from the scenes I tried to move to and from
     
  5. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Got it. All I might suggest is to liberally place Debug.Log statements throughout your code which will show in the console https://forum.unity.com/threads/tips-for-new-unity-users.701864/#post-5057741 You may be able to spot something