Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
197 views
in Technique[技术] by (71.8m points)

c# - How to freeze the screen?

In Unity, how can I freeze the screen in one scene, change scene, and then unfreeze it in the other scene? With freezing the screen I simply mean that it doesn't get updated while it's frozen. With screen I simply mean this: https://i.gyazo.com/10f8673d28da776c064fbb0e716866ad.jpg

I've tried to set Time.timeScale = 0, but that didn't work.

The thing is that I have different screen orientations in the scenes: portrait in the first and landscape in the other. This causes the second scene to twist/ruin the loading screen that was loaded in the first scene. The stuff being initialized in the second scene demands landscape orientation, so I can't just change the screen orientation after everything have been initialized.

I just want to freeze the screen after the loading image has loaded and then unfreeze it after the second scene has been initialized. Is this possible?

This is my code at the moment, generalized:

// Script for the first scene
class firstClass(){
    IEnumerator ChangeScene(){
        // Display loading screen in portrait orientation
        loadingScreen.SetActive(true);
        // Load the second scene
        SceneManager.LoadSceneAsync("secondScene");
    }
}

// Script for the second scene
class secondClass(){
     Start(){
         Screen.orientation = ScreenOrientation.LandscapeLeft; // twists the loading screen :(
         Init(a lot of stuff) // Needs landscape orientation to be initialized properly

         // Init done, hide loading screen
         loadingScreen.SetActive(false);

     }
}

This is how I want it to work:

class firstClass(){
    IEnumerator ChangeScene(){
        // Display loading screen in portrait orientation
        loadingScreen.SetActive(true);
        FreezeScreen();
        SceneManager.LoadSceneAsync("secondScene");
    }
}

class secondClass(){
     Start(){
         Screen.orientation = ScreenOrientation.LandscapeLeft; // doesn't affect the loading screen because the screen is frozen!
         Init(a lot of stuff);
         UnfreezeScreen();
         // Init done, hide loading screen
         loadingScreen.SetActive(false);

     }
}

Here's an illustration: https://gyazo.com/864303465be750b7970636415ddf070d

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
void freezeOrientation(bool freezeOrnt = true)
{
    Screen.autorotateToLandscapeLeft = freezeOrnt;
    Screen.autorotateToLandscapeRight = freezeOrnt;
    Screen.autorotateToPortrait = freezeOrnt;
    Screen.autorotateToPortraitUpsideDown = freezeOrnt;
}

Freeze Orientation

freezeOrientation(true);

Un-freeze Orientation

freezeOrientation(false);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...