Getting Started with Unity Day 7: Timer/Switching Pages

The tank battle is almost done, as the end, talk about the timer and how to switch pages
And attach the complete code link

I was sick for a week, so I pigeon for a week

timer

I learned this from youtube, and it’s not at all awkward in this tank battle.

The code is very simple

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class Timer : MonoBehaviour
{
#First set up the maximum value of the timer, the current time, and display his text, whether the time is over
[SerializeField] private float maxTime;
[SerializeField] private float currentTime;
[SerializeField] private Text timeDisplay;
[SerializeField] private bool isTimeOver;

#Initialization: let the display text equal to the maximum time
private void Awake(){
currentTime = maxTime;
timeDisplay.text = maxTime.ToString();
}
private void Update()
{
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);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement; ##This must be added

public class Option : MonoBehaviour
{
//options
private int choice = 1;
//Instantiate option location
public Transform posOne;
public Transform posTwo;

void Start()
{
}

void Update()
{
//If it is w, choose 1
if (Input.GetKeyDown(KeyCode.W))
{
choice = 1;
transform.position = posOne.position;
}
//If it is s, choose 2
else if (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

Full package link:
https://github.com/yangocean-sudo/Tank-War