一 Unity環(huán)境配置
1.1 Untity資源官網(wǎng)下載:https://unity.cn/releases
1.2 Unity Hub集成環(huán)境,包含工具和項(xiàng)目的管理
1.3 Unity Editor編輯器
1.4?Visual Studio 2022腳本編輯器
1.5 AndroidSKD,JDK,NDK工具,用于android環(huán)境的運(yùn)行
二 創(chuàng)建Unity項(xiàng)目
2.1 新建2D模板項(xiàng)目
2.2 新建2D物體
2.3 新建C#腳本文件?
2.4 腳本文件拖拽到物理區(qū)域,關(guān)聯(lián)物體?
2.5 點(diǎn)擊腳本打開(kāi)?Visual Studio 進(jìn)行編輯
2.6 輸入Debug.Log(gameObject.name);獲取物體的名字,點(diǎn)擊運(yùn)行?
2.7 調(diào)試?,腳本文件保存后,可以看到UnityEditor里面的腳本文件會(huì)同步變化
2.9 點(diǎn)擊頂部運(yùn)行按鈕就可以在控制臺(tái)看到日志輸出信息,可以看到打印出了物理對(duì)象的名字和標(biāo)簽
三 運(yùn)行問(wèn)題
3.1?第一次運(yùn)行可能會(huì)出現(xiàn)錯(cuò)誤,顯示Unity腳本顯示“雜項(xiàng)文件”,并且無(wú)語(yǔ)法提示的問(wèn)題
3.2? 解決方法:點(diǎn)擊 編輯(Edit)>首選項(xiàng)(Preferences)打開(kāi)首選項(xiàng)窗口?
3.3 在首選項(xiàng)窗口中,選擇 外部工具(External Tools)選項(xiàng)卡,將 外部腳本編輯器(External Script Editor)的設(shè)置改為 Visual Studio 2019等編輯器?
3.4 可以看到語(yǔ)法能夠正常顯示了?
四,物體組件認(rèn)識(shí)
4.1? 一個(gè)物理有很多組件,點(diǎn)擊物理,默認(rèn)組件信息就會(huì)出來(lái)
4.2 如下可以給物理新加組件信息,比如給物體新加聲音組件
4.3 腳本關(guān)聯(lián)物體后,也也屬于物體的一個(gè)組件?,可以在腳本中獲取物體的其它組件和控制物體的組件
4.4? 物體下面還可以創(chuàng)建多個(gè)物體,我們創(chuàng)建一個(gè)膠囊子物體,那膠囊就屬于子組件
4.5 腳本獲取基礎(chǔ)組件和子組件,父組件。如下獲取物體和組件實(shí)例:
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;
public class main : MonoBehaviour
{
public GameObject Capsule;//膠囊組件
public GameObject Prefab;//預(yù)設(shè)體
// Start is called before the first frame update
void Start()
{
//拿到當(dāng)前腳本所掛載的游戲物體
//GameObject go = this.gameObject;
//名稱
UnityEngine.Debug.Log(gameObject.name);
//tag
UnityEngine.Debug.Log(gameObject.tag);
//layer
UnityEngine.Debug.Log(gameObject.layer);
//膠囊的名稱
UnityEngine.Debug.Log(Capsule.name);
//膠囊當(dāng)前真正的激活狀態(tài)
UnityEngine.Debug.Log(Capsule.activeInHierarchy);
//膠囊當(dāng)前自身激活狀態(tài)
UnityEngine.Debug.Log(Capsule.activeSelf);
//獲取Transform組件
//Transform trans = this.transform;
UnityEngine.Debug.Log(transform.position);
//獲取其他組件
BoxCollider bc = GetComponent<BoxCollider>();
//獲取當(dāng)前物體的子物體身上的某個(gè)組件
GetComponentInChildren<CapsuleCollider>(bc);
//獲取當(dāng)前物體的父物體身上的某個(gè)組件
GetComponentInParent<BoxCollider>();
//添加一個(gè)組件
Capsule.AddComponent<AudioSource();
//通過(guò)游戲物體的名稱來(lái)獲取游戲物體
//GameObject test = GameObject.Find("Test");
//通過(guò)游戲標(biāo)簽來(lái)獲取游戲物體
GameObject test = GameObject.FindWithTag("Enemy");
test.SetActive(false);
UnityEngine.Debug.Log(test.name);
//通過(guò)預(yù)設(shè)體來(lái)實(shí)例化一個(gè)游戲物體
GameObject go = Instantiate(Prefab, Vector3.zero, Quaternion.identity);
//銷毀
Destroy(go);
}
// Update is called once per frame
void Update()
{
}
}
五 鼠標(biāo)和觸摸事件
5.1 鼠標(biāo)事件
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;
using UnityEngine.Windows;
using Input = UnityEngine.Input;
public class main : MonoBehaviour
{
void Start()
{
}
// Update is called once per frame
void Update()
{
//鼠標(biāo)的點(diǎn)擊
//按下鼠標(biāo) 0左鍵 1右鍵 2滾輪
if (Input.GetMouseButtonDown(0)){
UnityEngine.Debug.Log("按下了鼠標(biāo)左鍵");
}
//持續(xù)按下鼠標(biāo)
if (Input.GetMouseButton(0)) {
UnityEngine.Debug.Log("持續(xù)按下鼠標(biāo)左鍵");
}
//抬起鼠標(biāo)
if (Input.GetMouseButtonUp(0)) {
UnityEngine.Debug.Log("抬起了鼠標(biāo)左鍵");
//按下鍵盤按鍵
}
if (Input.GetKeyDown(KeyCode.A)) {
UnityEngine.Debug.Log("按下了A");
}
//持續(xù)按下按鍵
if (Input.GetKey(KeyCode.A)) {
UnityEngine.Debug.Log("持續(xù)按下A");
}
//抬起鍵盤按鍵
if (Input.GetKeyUp("a")){
UnityEngine.Debug.Log("松開(kāi)了A");
}
}
}
5.2 保存運(yùn)行后可以看到控制臺(tái)有對(duì)應(yīng)的日志輸出
5.3 手機(jī)單點(diǎn),多點(diǎn)觸控、
using UnityEngine;
using Input = UnityEngine.Input;
public class main : MonoBehaviour
{
void Start()
{
//開(kāi)啟多點(diǎn)觸控
Input.multiTouchEnabled = true;
}
// Update is called once per frame
void Update()
{
//判斷單點(diǎn)觸摸
if (Input.touchCount == 1)
{
//觸摸對(duì)象
Touch touch = Input.touches[0];//觸摸位置
UnityEngine.Debug.Log(touch.position);//觸摸階段
switch (touch.phase)
{
case UnityEngine.TouchPhase.Began:
break;
case UnityEngine.TouchPhase.Moved:
break;
case UnityEngine.TouchPhase.Stationary:
break;
case UnityEngine.TouchPhase.Ended:
break;
case UnityEngine.TouchPhase.Canceled:
break;
}
}
//判斷單點(diǎn)觸摸
if (Input.touchCount == 2)
{
//觸摸對(duì)象1
Touch touch0 = Input.touches[0];
//觸摸對(duì)象1
Touch touch1 = Input.touches[1];
}
}
}
5.4 物體向量移動(dòng),添加物體控制組件
編寫向量移動(dòng)腳本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlarerControll : MonoBehaviour
{
public CharacterController characterController;
// Start is called before the first frame update
void Start()
{
characterController = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
//水平軸
float horizontal = Input.GetAxis("Horizontal");
//垂直軸
float vertical = Input.GetAxis("Vertical");
//創(chuàng)建成一個(gè)方向向量
Vector2 dir = new Vector2(horizontal,vertical);
Debug.DrawRay(transform.position, dir, Color.red);
characterController.SimpleMove(dir);
}
}
六?鼠標(biāo)控制物體移動(dòng)
6.1 2D用transform屬性控制移動(dòng)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlarerControll : MonoBehaviour
{
public CharacterController characterController;
// Start is called before the first frame update
void Start()
{
characterController = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButton(0))
{
//目前的鼠標(biāo)二維坐標(biāo)轉(zhuǎn)為三維坐標(biāo)
Vector2 curMousePos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
//目前的鼠標(biāo)三維坐標(biāo)轉(zhuǎn)為世界坐標(biāo)
curMousePos = Camera.main.ScreenToWorldPoint(curMousePos);
transform.position = curMousePos ;
}
}
}
6.2 在攜程里面控制物體移動(dòng)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlarerControll : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
StartCoroutine(OnMouseDown());//在Start方法中調(diào)用StartCoroutine(要調(diào)用的協(xié)程方法)
}
// Update is called once per frame
void Update()
{
}
//協(xié)程
IEnumerator OnMouseDown()
{
//1. 得到物體的屏幕坐標(biāo)
Vector3 cubeScreenPos = Camera.main.WorldToScreenPoint(transform.position);
//2. 計(jì)算偏移量
//鼠標(biāo)的三維坐標(biāo)
Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, cubeScreenPos.z);
//鼠標(biāo)三維坐標(biāo)轉(zhuǎn)為世界坐標(biāo)
mousePos = Camera.main.ScreenToWorldPoint(mousePos);
Vector3 offset = transform.position - mousePos;
//3. 物體隨著鼠標(biāo)移動(dòng)
while (Input.GetMouseButton(0))
{
//目前的鼠標(biāo)二維坐標(biāo)轉(zhuǎn)為三維坐標(biāo)
Vector3 curMousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, cubeScreenPos.z);
//目前的鼠標(biāo)三維坐標(biāo)轉(zhuǎn)為世界坐標(biāo)
curMousePos = Camera.main.ScreenToWorldPoint(curMousePos);
//物體世界位置
transform.position = curMousePos + offset;
yield return new WaitForFixedUpdate(); //這個(gè)很重要,循環(huán)執(zhí)行
}
}
}
6.3 用Translate滑動(dòng)鼠標(biāo)移動(dòng)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlarerControll : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// 定義了一個(gè)名為sizespeed的公共(public)浮點(diǎn)型(float)變量,初始值為1
public float sizespeed = 1;
// 定義了一個(gè)名為mouseSpeed的公共浮點(diǎn)型變量,初始值為10
public float mouseSpeed = 10;
// 定義了一個(gè)名為lastMousePosition的私有(private)Vector3類型變量
private Vector3 lastMousePosition;
// Update is called once per frame
void Update()
{
// 獲取鼠標(biāo)滾輪的輸入值,并將其賦值給名為mouse的局部(local)浮點(diǎn)型變量
float mouse = -Input.GetAxis("Mouse ScrollWheel");
// 鼠標(biāo)中鍵按住拖動(dòng)
if (Input.GetMouseButton(0))
{
// 獲取當(dāng)前鼠標(biāo)位置和上一次鼠標(biāo)位置之間的差值,并將其賦值給名為deltaMousePosition的局部Vector3類型變量
Vector3 deltaMousePosition = Input.mousePosition - lastMousePosition;
// 將攝像機(jī)的位置向左右和上下移動(dòng),移動(dòng)的距離由鼠標(biāo)的移動(dòng)距離和鼠標(biāo)速度決定
transform.Translate(deltaMousePosition.x * mouseSpeed * Time.deltaTime, deltaMousePosition.y * mouseSpeed * Time.deltaTime, 0);
}
// 將攝像機(jī)的位置向上或向下移動(dòng),移動(dòng)的距離由鼠標(biāo)滾輪的輸入值和大小速度決定
transform.Translate(new Vector3(0, mouse * sizespeed, 0) * Time.deltaTime, Space.World);
// 將鼠標(biāo)當(dāng)前位置賦值給lastMousePosition變量,以便下一幀計(jì)算鼠標(biāo)位置差值
lastMousePosition = Input.mousePosition;
}
}
七 向量的認(rèn)識(shí)
7.1 向量在游戲角色世界是非常重要的一個(gè)概念,上面大部分物體的移動(dòng)都是通過(guò)向量Vector3?
7.2?向量指一個(gè)同時(shí)具有大小和方向的量. 它通常畫(huà)為一個(gè)帶箭頭的線段(如下圖).線段的長(zhǎng)度可以表示向量的大小,而向量的方向也就是箭頭所指的方向.物理學(xué)中的位移、速度、力等都是矢量
7.3?只要向量的大小和方向相同, 即視為相等的向量, 如下圖所示都是相同的向量.
7.4?向量的加法可以用幾種三種法則來(lái)解釋, 比如下面的三角形法則
7.5?向量的減法也有類似運(yùn)算法則, 三角形法則和平行四邊形, 記得箭頭總是由減數(shù)指向被減數(shù):
7.6?向量 b 與一個(gè)標(biāo)量(實(shí)數(shù))相乘還是一個(gè)向量, 觀察下面的當(dāng)標(biāo)量改變時(shí)候, 向量 a 的變化:
八 示例,碰撞物體
8.1 創(chuàng)建一個(gè)角色
8.2 給角色添加剛體和碰撞體,把重力設(shè)為0,不然會(huì)向下移動(dòng)出場(chǎng)景
8.3 新建紅色障礙物碰撞體,同時(shí)也添加碰撞體
?8.3? 在腳本里面編寫鍵盤按鍵控制物體移動(dòng)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MyPlayer : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
public float speed = 5f;//移動(dòng)速度
void Update()
{
float moveX = Input.GetAxisRaw("Horizontal");//控制水平移動(dòng)方向 A:-1 D:1 0
float moveY = Input.GetAxisRaw("Vertical");//控制垂直移動(dòng)方向 W: 1 S:-1 0
Vector2 position = transform.position;
position.x += moveX * speed * Time.deltaTime;
position.y += moveY * speed * Time.deltaTime;
transform.position = position;
}
}
8.5 運(yùn)行可以看到碰撞到障礙物停止的效果
8.6 優(yōu)化,發(fā)現(xiàn)角色碰到物體會(huì)抖動(dòng)和旋轉(zhuǎn),旋轉(zhuǎn)需要勾選上腳色剛體的旋轉(zhuǎn)約束屬性
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-757214.html
8.7 抖動(dòng)問(wèn)題需要編寫腳本,用剛體的移動(dòng)替換腳色的移動(dòng),修改如下:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-757214.html
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MyPlayer : MonoBehaviour
{
public Rigidbody2D rbody;
// Start is called before the first frame update
void Start()
{
rbody = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
public float speed = 10f;//移動(dòng)速度
void Update()
{
float moveX = Input.GetAxisRaw("Horizontal");//控制水平移動(dòng)方向 A:-1 D:1 0
float moveY = Input.GetAxisRaw("Vertical");//控制垂直移動(dòng)方向 W: 1 S:-1 0
Vector2 position = rbody.position;
position.x += moveX * speed * Time.deltaTime;
position.y += moveY * speed * Time.deltaTime;
//transform.position = position;
rbody.MovePosition( position);
}
}
到了這里,關(guān)于Unity之創(chuàng)建第一個(gè)2D游戲項(xiàng)目的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!