
public class BGMove : MonoBehaviour
{
float speed = 1;
//规定重复点
Vector3 reset;
void Start()
{
reset = new Vector3(0, 10, 0);
}
void Update()
{
//向下运动
transform.Translate(Vector3.down * Time.deltaTime * speed,Space.World);
//超出屏幕从规定点开始
if (transform.position.y <= -9.5)
{
transform.position = reset;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
/// <summary>
/// 开始界面
/// </summary>
public class StartUI : MonoBehaviour
{
//定义开始界面
public GameObject startPanel;
//定义排行榜界面
public GameObject ratePanel;
public GameObject rankPanel;
//定义分数与最高分数文本
Text scoreText;
Text bestText;
void Start()
{
//找到分数与最高分数文本位置
scoreText = GameObject.Find("Canvas").transform.GetChild(2).GetChild(1).GetChild(1).GetComponent<Text>();
bestText = GameObject.Find("Canvas").transform.GetChild(2).GetChild(1).GetChild(2).GetComponent<Text>();
}
void Update()
{
//显示分数与最高分数文本
scoreText.text = GameSingleton.getInstance.score.ToString();
bestText.text = GameSingleton.getInstance.bestScore.ToString();
}
//打开准备界面
public void OpenRatePanel()
{
//activeSelf:查看自身状态,返回bool类型
if (!ratePanel.activeSelf)
{
//准备界面显示
ratePanel.SetActive(true);
}
if (startPanel.activeSelf)
{
//开始界面隐藏
startPanel.SetActive(false);
}
}
//排行界面打开,开始界面隐藏
public void OpenRankPanel()
{
if (!rankPanel.activeSelf)
{
//排行界面显示
rankPanel.SetActive(true);
}
if (startPanel.activeSelf)
{
//开始界面隐藏
startPanel.SetActive(false);
}
}
//准备界面返回开始界面-->开始界面打开,排行界面隐藏
public void RateBackStartPanel()
{
if (!startPanel.activeSelf)
{
//开始界面显示
startPanel.SetActive(true);
}
if (ratePanel.activeSelf)
{
//排行界面隐藏
ratePanel.SetActive(false);
}
}
//准备界面返回开始界面-->开始界面打开,准备界面隐藏
public void RankBackStartPanel()
{
if (!startPanel.activeSelf)
{
//开始界面显示
startPanel.SetActive(true);
}
if (rankPanel.activeSelf)
{
//准备界面隐藏
rankPanel.SetActive(false);
}
}
//跳转开始界面
public void MainScene()
{
//分数重置
GameSingleton.getInstance.score = 0;
//跳转开始界面
SceneManager.LoadScene("Main");
}
//退出游戏
public void QuitGame()
{
//应用程序退出,只有在打包发布之后才会有效果
Application.Quit();
}
}

这篇博客介绍了如何使用Unity创建一个2D飞机大战游戏。`BGMove`类实现了背景的循环滚动,而`StartUI`类则包含了游戏开始、排行榜和分数显示的交互逻辑。代码中使用了Singleton模式来管理分数,并通过`SceneManager`进行场景切换。

2873

被折叠的 条评论
为什么被折叠?



