Example #1
0
 void Awake()
 {
     if (Instance == null)
     {
         Instance = this;
         DontDestroyOnLoad(gameObject);
     }
     else
     {
         Destroy(gameObject);
     }
 }
Example #2
0
    private IEnumerator LoadScreen(string screenId, string screenPath)
    {
        GameObject newScreen;

        PreviousScreenID = currentScreen.screenId;

        // when running on webgl; only load game screen from resource
        if ((Application.platform != RuntimePlatform.WebGLPlayer && PersistentModel.Instance.RunLocation != PersistentModel.RUN_LOCATION.Client) || screenId == GAME_SCREEN)
        {
            ResourceRequest resourceRequest = Resources.LoadAsync <BaseScreen>(screenPath);
            while (!resourceRequest.isDone)
            {
                yield return(null);
            }

            transitioningScreenId = screenId;
            transitioningScreen   = Instantiate <BaseScreen>(resourceRequest.asset as BaseScreen, transform);
            transitioningScreen.transform.SetAsFirstSibling();
            transitioningScreen.Initialize(screenId);

            // loading is complete
            if (ProgressLoadingPanel)
            {
                ProgressLoadingPanel.GetComponent <ProgressLoadingPanel>().LoadingPercent.text = "100%";
            }
        }
        else
        {
            string          url     = PersistentModel.Instance.AssetBundlesURL + screenId.ToLower();
            UnityWebRequest request = new UnityWebRequest(url)
            {
                downloadHandler    = new DownloadHandlerAssetBundle(url, 1, 0),
                certificateHandler = new SSLAuth()
            };
            request.SendWebRequest();

            while (!request.isDone)
            {
                ProgressLoadingPanel.GetComponent <ProgressLoadingPanel>().LoadingPercent.text = Mathf.CeilToInt(request.downloadProgress * 100).ToString() + "%";
                yield return(waitSecAssetLoad);
            }

            AssetBundle        bundle        = DownloadHandlerAssetBundle.GetContent(request);
            AssetBundleRequest bundleRequest = bundle.LoadAssetAsync(screenId);

            yield return(bundleRequest);

            newScreen = Instantiate <GameObject>(bundleRequest.asset as GameObject);
            bundle.Unload(false);

            ProgressLoadingPanel.GetComponent <ProgressLoadingPanel>().LoadingPercent.text = "100%";

            // Wait for the load
            yield return(newScreen);

            // add Parent, and Add it in the 1st index of Parent
            // so that we can have the new screen behind the current screen
            newScreen.transform.SetParent(transform, false);
            newScreen.transform.SetAsFirstSibling();
            newScreen.GetComponent <BaseScreen>().Initialize(screenId);

            // add to list
            transitioningScreenId = screenId;
            transitioningScreen   = newScreen.GetComponent <BaseScreen>();
        }

        // isLoadingRequiredBeforeDraw is when loading the game screen and track
        if (!transitioningScreen.isLoadingRequiredBeforeDraw)
        {
            if (currentScreen != null)
            {
                Color activeColor = currentScreen.gameObject.GetComponent <Image>().color;
                activeColor.a = 1f;

                // fadeout background image color from screen
                LeanTween.value(1f, 0f, 0.75f)
                .setDelay(0.25f)
                .setEase(LeanTweenType.easeOutQuad)
                .setOnUpdate((float val) =>
                {
                    activeColor.a = val;
                    currentScreen.gameObject.GetComponent <Image>().color = activeColor;
                })
                .setOnComplete(() =>
                {
                    // ignore closing sequence, start transition in right away after race.
                    if (screenId == CONGRATULATIONS_SCREEN)
                    {
                        ShowTransitionedScreen();
                    }
                    else
                    {
                        // scales and fades in side panels
                        // transitioningScreen.ScaleInPanels();

                        LeanTween.delayedCall(0.5f, transitioningScreen.ScaleInPanels);

                        // add event when loading close sequence is complete
                        currentScreen.OnCloseLoadingPanelComplete += CurrentScreen_OnCloseLoadingPanelComplete;

                        // start Close Panel sequence
                        currentScreen.CloseLoadingPanel();
                    }
                });
            }
            else
            {
                Color activeColor = transitioningScreen.gameObject.GetComponent <Image>().color;
                activeColor.a = 1f;

                // fadeout background image color from screen
                LeanTween.value(1f, 0f, 0.15f)
                .setDelay(0.0f)
                .setEase(LeanTweenType.easeOutQuad)
                .setOnUpdate((float val) =>
                {
                    activeColor.a = val;
                    transitioningScreen.gameObject.GetComponent <Image>().color = activeColor;
                })
                .setOnComplete(() =>
                {
                    // scales and fades in side panels
                    transitioningScreen.ScaleInPanels();

                    LeanTween.delayedCall(0.65f, ShowTransitionedScreen);
                });

                // ShowTransitionedScreen();
                yield return(0);
            }
        }
        else
        {
            ShowTransitionedScreen();
        }
    }