publicclassTimer : MonoBehaviour { #First set up the maximum value of the timer, the current time, and display his text, whether the time is over [SerializeField] privatefloat maxTime; [SerializeField] privatefloat currentTime; [SerializeField] private Text timeDisplay; [SerializeField] privatebool isTimeOver;
#Initialization: let the display text equal to the maximum time privatevoidAwake(){ currentTime = maxTime; timeDisplay.text = maxTime.ToString(); } privatevoidUpdate() { if(!isTimeOver){ #If there is no time, decrement every second currentTime -= Time.deltaTime; #Sync to display text timeDisplay.text = ((int)currentTime).ToString(); }
//If less than 5 seconds, the font color is more eye-catching (red) if (currentTime <= 5){ timeDisplay.color = new Vector4(200,0,0,255);//Red, green, blue, alpha } // less than 0 seconds, the time is over if(currentTime <= 0){ isTimeOver = true; } } }
switch pages
The operation is to select 1 with the W key, select 2 with the S key, and confirm with a space.
But the main method is: SceneManager.LoadScene(1);
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; ##This must be added
publicclassOption : MonoBehaviour { //options privateint choice = 1; //Instantiate option location public Transform posOne; public Transform posTwo;
voidStart() { }
voidUpdate() { //If it is w, choose 1 if (Input.GetKeyDown(KeyCode.W)) { choice = 1; transform.position = posOne.position; } //If it is s, choose 2 elseif (Input.GetKeyDown(KeyCode.S)) { choice = 2; transform.position = posTwo.position; }
//If it is 1 and blank, switch to the game if (choice == 1 && Input.GetKeyDown(KeyCode.Space)) { //load the game scene SceneManager.LoadScene(1); #Switch the soul of the scene } } }
Through this method, the two games of the group work have been successfully put together, which is very satisfying. It should be noted that we need to import LoadScene first, the specific method is: File -> BildSettings –> Insert the scenes in order –> Cross out