Unity main scene setting, scene switching, full screen setting, background music setting (start playing as soon as the scene is loaded)

main scene settings

the easiest way

Under normal circumstances, we have a lot of scenes. At this time, we will have problems when we enter the game. Which one should Unity load first?
Unity gives us several solutions, one of which is to change the priority of the game scene in the file - build settings , where 0 is the highest priority (of course, you must first add all the scenes to the build, or if you An error will be reported if an external jump is made to a scene without build).
insert image description here

The second method

via script method

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;//场景头部引入

public class ChageScene : MonoBehaviour
{
    
    
    [RuntimeInitializeOnLoadMethod]
    static void Initialize()
    {
    
    
        string startSceneName = "GameScene";
        Scene scene = SceneManager.GetActiveScene();
        if (scene.name.Equals(startSceneName))
        {
    
    
            return;
        }
        SceneManager.LoadScene(startSceneName);
    }

    
}

The script does not need to be hung in any object, the [RuntimeInitializeOnLoadMethod] function will be compiled as soon as the game is loaded

scene switch

Add the following code to the script hanging on the button and press the monitor to jump to the scene

    public void ClickTollGame(){
    
    
        /* gameObject.SetActive(true); */
        SceneManager.LoadScene("GameScene");  //另一个场景名叫GameScene
    }

insert image description here

full screen settings

The full screen setting is also written in a script that does not need to be hung on any object

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FullScreen : MonoBehaviour {
    
    
    private void Awake()
    {
    
    
        //获取设置当前屏幕分辩率 
        Resolution[] resolutions = Screen.resolutions;
        //设置当前分辨率 
        Screen.SetResolution(resolutions[resolutions.Length - 1].width, resolutions[resolutions.Length - 1].height, true);

        Screen.fullScreen = true;  //设置成全屏
      
    }
}

finally background music

Its functions are:
1. Start playing after the scene is loaded
2. It has the function of pause and play (need to be hung on the button)
put the code first

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class music : MonoBehaviour
{
    
    
    public AudioSource audi;  //相当于定义一个音频?

    void Start()
    {
    
    
        audi = GetComponent<AudioSource>(); //获取音频
        audi.Play();     //播放
//        Debug.Log("111");
    }

    public void estimateMusic () {
    
    
        if (audi.isPlaying)//正在播放背景音乐时
        {
    
    
            //audi.enabled = false;
            audi.Pause();
        }
        else//未播放背景音乐时
        {
    
    
            //audi.enabled = true;
            audi.Play();
        }

    }

}

insert image description here
Hang on a button, when the button is triggered, it will run.
insert image description here
Add an Audio Source component to this button, put music, so that the script can get the audio. The
last reminder is that the button to mount music must be activated (display status ), or the music will not be loaded. If you want to not display this button, you can change its color to transparent.

Guess you like

Origin blog.csdn.net/qq_19829077/article/details/130427433