Unity入门积累(实践项目变种-坦克大战:世界远征)【暂停更新】

50 阅读2分钟

一、知识点补充

1、场景切换和退出

切换: 使用SceneManager.LoadScene("场景名称");来进行场景切换。注意:需要引入SceneManager的相关命名空间,并在File-Build Setting-点击Add Open Scenes添加当前打开场景(可以拖入层级窗口中的场景)。

退出:

Application.Quit();//编辑模式下无效

2、鼠标相关

显示和隐藏:

//鼠标隐藏 false 隐藏 true 显示 Cursor.visible = false;

锁定鼠标:

//鼠标锁定 None:不锁定 Locked:隐藏并锁定在中心位置(ESC退出) Confined:锁定在窗口内(ESC退出) Cursor.lockState = CursorLockMode.Confined;

设置鼠标图片

image.png

3、随机数和委托(Unity中)

随机数:

#region Unity随机数
//整形int随机 左包含 右不包含
int randomNum = Random.Range(1, 101);
print(randomNum);
//浮点数float随机 左右都包含
float randomFloatNum = Random.Range(1f, 100f);
print(randomFloatNum);
#endregion
#region C#随机数
//需要实例化,并且指明命名空间 左包含 右不包含
System.Random rand = new System.Random();
rand.Next(0, 100);
#endregion

Unity委托:

#region Unity委托
UnityAction<string> ua = (s) =>
{
    s += "123";
};
#endregion

4、模型导入

image.png

二、开始场景

1、开始界面

需要制作场景:新建一个场景,之后放平台,平台上放一些模型,并给需要动的物体挂载脚本如:

image.png

管理开始场景运动脚本:

public class BeginSceneRotate : MonoBehaviour
{
    public float rotateSpeeed;
    public float moveSpeeed;
    Transform parentTransform;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        this.transform.Rotate(Vector3.up, rotateSpeeed * Time.deltaTime);
        if(this.gameObject.name == "TurretObject")
        {
            parentTransform = this.transform.parent;
            parentTransform.transform.Translate(Vector3.forward * moveSpeeed * Time.deltaTime, Space.Self);
        }
    }
}

之后写基类脚本,用于作为展示和隐藏:

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

public class BasePanel<T> : MonoBehaviour where T : class//where T : class 是对泛型类型T的约束,表示T必须是一个引用类型(类、接口、委托或数组)。
{
    static T instance;//声明一个静态变量instance,类型为T。用于存储单例实例。
    public static T Instance => instance;//定义一个公共静态属性Instance,使用表达式体定义(只读),返回instance。这是获取单例实例的方式。
    private void Awake()//Awake 是 Unity 的生命周期方法,在对象初始化时调用
    {
        instance = this as T;//将当前对象转换为 T 类型并赋值给 instance 字段
    }
    public virtual void ShowMe()
    {
        this.gameObject.SetActive(true);
    }
    public virtual void HideMe()
    {
        this.gameObject.SetActive(false);
    }
}

开始面板类:管理开始面板的UI:

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

public class BeginPanel : BasePanel<BeginPanel>
{
    public CustomGUIButton buttonBegin;
    public CustomGUIButton buttonSetting;
    public CustomGUIButton buttonEnd;
    public CustomGUIButton buttonRank;
    // Start is called before the first frame update
    void Start()
    {
        //开始游戏
        buttonBegin.clickEvent += () =>
        {
            SceneManager.LoadScene("GameScene");
        };
        //设置游戏
        buttonSetting.clickEvent += () =>
        {

        };
        //结束游戏
        buttonEnd.clickEvent += () =>
        {
            Application.Quit();
        };
        //排行榜
        buttonRank.clickEvent += () =>
        {

        };
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}