开发环境的安装与配置
unity2021
链接:https://unity.cn/
vs2022
链接:https://visualstudio.microsoft.com/zh-hans/
游戏场景搭建
场景搭建创建四个3D物体Cube,Sphere,Cylinder,Capsule
素材制作与场景完善
将四个3D物体Cube,Sphere,Cylinder,Capsule设为预制体放置在Resources/Prefabs文件夹下
使用unity的UGUI组件中的Text搭建好屏幕中央的“+”与物体名字显示文本
游戏应用程序功能实现
实现移动摄像机一项功能
public class MirMove : MonoBehaviour
{
private GameObject m_Camera;
public float Speed=30;
void Awake()
{
//找到主相机
m_Camera = GameObject.Find("Main Camera");
Txt_ModleName = transform.parent.Find("ShowName").GetComponent<Text>();
Cube = Resources.Load<GameObject>("Prefabs/Cube");
Sphere = Resources.Load<GameObject>("Prefabs/Sphere");
Cylinder = Resources.Load<GameObject>("Prefabs/Cylinder");
Capsule = Resources.Load<GameObject>("Prefabs/Capsule");
}
//相机移动
private void MirMoveto() {
h = Input.GetAxis("Horizontal"); ;
v = Input.GetAxis("Vertical");
Vector3 moveAt = new Vector3(h, v, 0) * Speed * Time.deltaTime;
m_Camera.transform.Translate(moveAt);
}
}
实现屏幕射线检测物体,并在文本中显示物体名字一项功能
//射线检测
private GameObject RayPut() {
Vector3 Pos = Camera.main.transform.position;
Ray ray = new Ray(Pos, Camera.main.transform.forward);
RaycastHit hit;
GameObject modle = null;
if (Physics.Raycast(ray, out hit, raycastDistance))
{
modle = hit.collider.gameObject;
Debug.Log("Hit object: " + modle.name);
Txt_ModleName.text = "已经瞄准:" + modle.name;
}
if (modle == null)
{
Txt_ModleName.text = "";
}
return modle == null?null: modle;
}
实现鼠标左键点击后销毁物体一项功能
//鼠标点击销毁物体
private void isMouseDown() {
if (Input.GetMouseButtonDown(0)) {
GameObject game = RayPut();
if (game != null) {
string name = game.name;
Transform tr=game.transform;
Destroy(game);
StartCoroutine(Countdown(name,tr));
}
}
}
实现物体销毁后自动重新生成一项功能
//协程实现倒计时两秒后重新生成销毁物体
IEnumerator Countdown(string name,Transform tr)
{
for (int i = time; i >= 0; i--)
{
yield return new WaitForSeconds(1f);
Debug.Log("Countdown: " + i + " seconds");
}
GameObject obj = null;
switch (name)
{
case "Cube":
obj = Instantiate(Cube, tr);
obj.name = "Cube";
break;
case "Sphere":
obj = Instantiate(Sphere, tr);
obj.name = "Sphere";
break;
case "Cylinder":
obj = Instantiate(Cylinder, tr);
obj.name = "Cylinder";
break;
case "Capsule":
obj = Instantiate(Capsule, tr);
obj.name = "Capsule";
break;
default:
break;
}
}
每帧更新
void Update()
{
MirMoveto();
RayPut();
isMouseDown();
}
全部代码
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public class MirMove : MonoBehaviour
{
private GameObject m_Camera;
public float Speed=30;
private float raycastDistance = 100.0f;
private int time = 2;
private Text Txt_ModleName;
private float h;
private float v;
private GameObject Cube;
private GameObject Sphere;
private GameObject Cylinder;
private GameObject Capsule;
void Awake()
{
m_Camera = GameObject.Find("Main Camera");
Txt_ModleName = transform.parent.Find("ShowName").GetComponent<Text>();
Cube = Resources.Load<GameObject>("Prefabs/Cube");
Sphere = Resources.Load<GameObject>("Prefabs/Sphere");
Cylinder = Resources.Load<GameObject>("Prefabs/Cylinder");
Capsule = Resources.Load<GameObject>("Prefabs/Capsule");
}
void Start()
{
//StartCoroutine(Countdown(2)); // 启动一个2秒的倒计时协程
}
IEnumerator Countdown(string name,Transform tr)
{
for (int i = time; i >= 0; i--)
{
yield return new WaitForSeconds(1f);
Debug.Log("Countdown: " + i + " seconds");
}
GameObject obj = null;
switch (name)
{
case "Cube":
obj = Instantiate(Cube, tr);
obj.name = "Cube";
break;
case "Sphere":
obj = Instantiate(Sphere, tr);
obj.name = "Sphere";
break;
case "Cylinder":
obj = Instantiate(Cylinder, tr);
obj.name = "Cylinder";
break;
case "Capsule":
obj = Instantiate(Capsule, tr);
obj.name = "Capsule";
break;
default:
break;
}
}
private void MirMoveto() {
h = Input.GetAxis("Horizontal"); ;
v = Input.GetAxis("Vertical");
Vector3 moveAt = new Vector3(h, v, 0) * Speed * Time.deltaTime;
m_Camera.transform.Translate(moveAt);
}
private GameObject RayPut() {
Vector3 Pos = Camera.main.transform.position;
Ray ray = new Ray(Pos, Camera.main.transform.forward);
RaycastHit hit;
GameObject modle = null;
if (Physics.Raycast(ray, out hit, raycastDistance))
{
modle = hit.collider.gameObject;
Debug.Log("Hit object: " + modle.name);
Txt_ModleName.text = "已经瞄准:" + modle.name;
}
if (modle == null)
{
Txt_ModleName.text = "";
}
return modle == null?null: modle;
}
private void isMouseDown() {
if (Input.GetMouseButtonDown(0)) {
GameObject game = RayPut();
if (game != null) {
string name = game.name;
Transform tr=game.transform;
Destroy(game);
StartCoroutine(Countdown(name,tr));
}
}
}
void Update()
{
MirMoveto();
RayPut();
isMouseDown();
}
}