Unity scene jump, hidden mouse and other common function code collection

Preface

Unity has some basic function code implementation methods, these functions are not flexible and relatively fixed, and can be written down for use

One, hide the mouse

It is often necessary to hide the mouse when making 3D games, which can be achieved by the following commands:

Cursor.lockState = CursorLockMode.Locked;//锁定指针到视图中心
Cursor.visible = false;//隐藏指针

Two, realize the scene jump

Scene jump is a common way to switch scenes:

 UnityEngine.SceneManagement.SceneManager.LoadScene(1); #数字对应场景排序的数字

To realize the jump of scenes, drag the scenes in Bulid Setting and sort them, so that the data at the end can be used to achieve

Three, end the game

Ending the game is also a necessary function in the game, the code is :

#if UNITY_EDITOR
            UnityEditor.EditorApplication.isPlaying=false;
#else
   Application.Quit();                  //确保游戏打包后依旧可以实现该功能
#endif

Note: The code here must be written completely. If there is only the previous if judgment, the function of ending the game can only be realized in the compiler. After the game is packaged, a BUG will occur.

Guess you like

Origin blog.csdn.net/xinzhilinger/article/details/109209108