作业需求
前置准备
要先熟悉场景切换跟 Slider 用法以及在画面上建立一个图片按钮
建立一个 UI 上的图片按钮
切换场景
Slider Bar
按钮图片下载
建立两个场景
首先先做一个图片按钮
另存成一个场景,名为「Scene1」
并且在资源管理器的目录「Assets」「Scenes」目录下按滑鼠右键「建立」「场景」命名为「Scene2」
先编辑 Scene1 ,点两下进入修改 Scene1 的背景颜色
再来编辑 Scene2 修改背景颜色

在 Scene1 的按钮按下后跳到 Scene2
建立一个 C# 脚本,命名为「Jump2」
编辑 Jump2 脚本,加入以下程式码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Jump2 : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void MENU_ACTION_GOTO_PAGE()
{
Application.LoadLevel("Scene2");
}
}将 Jump2 脚本拉到物件「Main Camera」底下
将「Button」物件的 OnClick() (滑鼠按一下()) 设定为 Main Camera
依序按下「档案」「建置设定..」将 Scene1 加入
按下 Ctrl + S 储存所有设定
在 Scene2 加入 Slider 以及倒数计时并跳回 Scene1
进入 Scene2 场景,「阶层」「UI」「Slider」加入 Slider
设定 Slider 物件的属性
- Min Value: 0
- Max Value: 10
- 整数打勾
- Value: 10
「阶层」「UI」「文字」加入 Text
设定文字物件的属性
Scene2 设定完成
建立一个脚本,名为「SliderCountdown」
编辑脚本并加入以下程式码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class SliderCountdown : MonoBehaviour
{
// Start is called before the first frame update
private int First_Second;
private Slider SliderUI;
private Text TextUI;
private float countdowntimerdelay;
private float countdowntimerstarttime;
void Start()
{
SliderUI = GameObject.Find("Slider").transform.GetComponent<Slider>();
TextUI = GameObject.Find("Text").transform.GetComponent<Text>();
First_Second = 11;
CountdownTimerRestet(First_Second);
}
// Update is called once per frame
void Update()
{
int timeleft = (int)CountdownTimersecre();
SliderUI.value = timeleft;
TextUI.text = timeleft.ToString();
if (timeleft == 0) Goto_Scene1();
}
private void Goto_Scene1() { SceneManager.LoadScene("Scene1"); }
private void CountdownTimerRestet(float delaysec)
{
countdowntimerdelay = delaysec;
countdowntimerstarttime = Time.time;
}
private float CountdownTimersecre()
{
float elapsedsec = Time.time - countdowntimerstarttime;
float timeleft = countdowntimerdelay - elapsedsec;
return timeleft;
}
}将脚本拉入 Slider 物件底下
将脚本拉入 Text 物件底下
依序按下「档案」「建置设定..」将 Scene2 加入
按下 Ctrl + S 储存所有设定
完成,看看效果吧!

Latest posts by SHXJ (see all)
- 受保护的内容: NAS 版 Mathbot 管理网站与 Linebot 启动方法 - 2024 年 11 月 15 日
- Realtime 啥鬼的 - 2021 年 6 月 15 日
- nodejs 数学游戏 - 2021 年 6 月 8 日
















