I feel like I've been sleeping a little restlessly lately, and I always wake up suddenly in the middle of the night. Then get up and take a look at your phone, 3 a.m.
Then I found out that the game was full of stamina, and it took 10 minutes to empty the stamina and then nothing dropped.
I traded those ten minutes for a sleepless night, and I didn't know if I could earn it or not.
This caused me to sometimes dream of strange scenes in the afternoon sleep, such as the male character in the game holding my thigh and saying can you not just smoke the female character you old skin.
Or maybe playing a game until the supergod turned out to be a network break, which woke me up all of a sudden, and sleepiness suddenly gave me nothing.
No wonder doctors say that proper napping is good for human physical and mental health, I am stunned today that I did not dare to play a game for fear of reality repeating itself, and my time was all spent studying.
In order to make my pain feel to others, I plan to add these scenes to the player character when I learn to make RPGs.
To get back to business, what I'm going to talk about today is about the knowledge of scenes.
Without further ado, let's go!
I. Scene (P27)
- A game consists of many scenes, such as the start page, the game page, the store page, and so on.
- The most typical newbie village is also a scene, and each scene is also made up of many game objects.
- Each game object consists of various components (scripts).
The above is a general game framework, which used to add components and scripts to the default game scene, in fact, you can also create new game scenes through code or components.
In Unity's hierarchy you can see that there's something called SampleScene, which contains two GameObjects underneath
This SampleScene is a default game scene, and SampleScene is in the Scenes folder in the Project view, so you can create (right mouse button) a new game scene in the Scenes folder.
Double-click to open the newly created scene
1-1. Load the scene through code (load from Sample to My) - scene jump
Open the file→ project - build and settings (Ctrl+Shift+B) in SampleScence→ drag the scene into the build (0 and 1 are the index numbers of the scene)
Next, create an empty object, create a script file, mount it and open it.
First import at the top of the code: using UnityEngine.SceneManagement.
Synchronous loading scene code (index): SceneManager.LoadScene(1);
After returning to Unity, the system will automatically open Scene 1 (MyScene)
In addition to the indexing method, you can also use the name to change the number to "scene name"
SceneManager.LoadScene("MyScene");
1-2. Get the current scene (currently active scene)
Scene scene = SceneManager.GetActiveScene();
The scene in the code is the current SampleScene
In order to verify that the acquisition was successful, verify by printing:
Debug.Log(scene.name);
The scene here is the scene class, and there are some properties to understand for some scene classes.
In addition to the scene name Debug.Log(scene.name) mentioned above;
1-3. Whether the scene is loaded
Debug.Log(scene.isLoaded);
1-4. Scene path
Debug.Log(scene.path);
1-5. Scene index (get the index number in the Build scene panel)
Debug.Log(scene.buildIndex);
1-6. Get all root game objects
scene. GetRootGameObjects();
A host is returned here:
GameObject[] gos = scene. GetRootGameObjects();
You can print it out to see what's in the host (print length):
Debug.Log(gos. Length);
In the following figure, you can see the return of the console in detail
- The first is the scene name
- The second is that the scene has been loaded (ture)
- The third is the path where the scene is stored
- The fourth indicates that the current scene is scene 0
- The fifth one indicates that there are 3 items in the current scene
II. Scenario Management (P27)
2-1. Scene management class (number of currently loaded scenes)
Debug.Log(SceneManager.sceneCount);
2-2. Create a new scene directly (custom scene name in parentheses)
SceneManager.CreateScene("newScene");
Of course, you can also set a return:
Scene newScene = SceneManager.CreateScene("newScene");
You can see that there is a new empty scene called NewScene directly here
If you reverse the order of the above two lines of code, you will find that the currently loaded scene will change from 1 to 2.
2-3. Unload the scene (asynchronous destruction) Put the scene you want to destroy in parentheses
SceneManager.UnloadSceneAsync(newScene);
Asynchronous destruction: Instead of destroying the object immediately, this function adds an identifier to the object, which is still in memory and is destroyed and removed from memory only in the next frame
After running it here, you can see that the activation scene in the lower left corner is still 2, but the newScene scene does not appear.
Because it was directly deleted after it was created.
2-4. Load scene (multi-parameter)
SceneManager.LoadScene("MyScene",LoadSceneModel.Single);
The Single here is a simple replacement load, and after running, it will be directly loaded as MyScene to replace the original SampleScene.
If you replace Single with another:
SceneManager.LoadScene("MyScene",LoadSceneModel.Additive);
You can see that the contents of the two scenes are superimposed
However, this method can easily cause stuttering when loading complex scenes or scenes of large games
Asynchronous loading of complex scenes is explained in the next note.
That's it for today!
Every time I finish writing an article, it is past ten o'clock, and I feel that I haven't done anything every day.
Today I learned the game before I played, and I want to reward myself.
See you in the next note, bye
Link to the original tutorial:
https://www.bilibili.com/video/BV1gQ4y1e7SS/?spm_id_from=333.337.search-card.all.click&vd_source=24f33eddf5587fb40e947798abce6715