Unity 實(shí)現(xiàn)框選游戲戰(zhàn)斗單位的思路詳解
?? Preface
本文簡單介紹如何實(shí)現(xiàn)即時戰(zhàn)略游戲中框選戰(zhàn)斗單位的功能,如圖所示:

?? 實(shí)現(xiàn)思路:
本文將該功能的實(shí)現(xiàn)拆分為以下部分:
- 在屏幕坐標(biāo)系中繪制框選范圍;
- 根據(jù)框選范圍定位其在世界坐標(biāo)系中對應(yīng)的區(qū)域;
- 在該區(qū)域內(nèi)進(jìn)行物理檢測。
? 如何在屏幕坐標(biāo)系內(nèi)繪制框選框
使用Line Renderer光線渲染器組件來進(jìn)行范圍繪制,當(dāng)鼠標(biāo)按下時,可以獲得框選范圍的起始點(diǎn),鼠標(biāo)持續(xù)按下時,鼠標(biāo)位置則是框選范圍的結(jié)束點(diǎn),根據(jù)這兩個點(diǎn)的坐標(biāo)可以求得另外兩個頂點(diǎn)的坐標(biāo),如圖所示:

首先設(shè)置Line Renderer光線渲染器的屬性:

Enable:默認(rèn)設(shè)為false,當(dāng)鼠標(biāo)按下時將其設(shè)為true;Loop:設(shè)為true,為了讓第三個頂點(diǎn)與起始點(diǎn)相連形成閉環(huán);Size:設(shè)為4,框選范圍有4個頂點(diǎn);Width:設(shè)為0.001即可,線框不需要很粗,可適當(dāng)調(diào)整;
代碼部分:
using UnityEngine;
using SK.Framework;
using System.Collections.Generic;
public class Example : MonoBehaviour
{
//光線渲染器組件
private LineRenderer lineRenderer;
//屏幕坐標(biāo)系起始點(diǎn)
private Vector3 screenStartPoint;
//屏幕坐標(biāo)系結(jié)束點(diǎn)
private Vector3 screenEndPoint;
private void Start()
{
//獲取光線渲染器組件
lineRenderer = GetComponent<LineRenderer>();
}
private void Update()
{
//鼠標(biāo)按下
if (Input.GetMouseButtonDown(0))
{
//激活光線渲染器
lineRenderer.enabled = true;
//屏幕坐標(biāo)系起始點(diǎn)
screenStartPoint = Input.mousePosition;
screenStartPoint.z = 1;
}
//鼠標(biāo)持續(xù)按下
if (Input.GetMouseButton(0))
{
//屏幕坐標(biāo)系結(jié)束點(diǎn)
screenEndPoint = Input.mousePosition;
screenEndPoint.z = 1;
//求得框選框的另外兩個頂點(diǎn)的位置
Vector3 point1 = new Vector3(screenEndPoint.x, screenStartPoint.y, 1);
Vector3 point2 = new Vector3(screenStartPoint.x, screenEndPoint.y, 1);
//接下來使用光線渲染器畫出框選范圍
lineRenderer.SetPosition(0, Camera.main.ScreenToWorldPoint(screenStartPoint));
lineRenderer.SetPosition(1, Camera.main.ScreenToWorldPoint(point1));
lineRenderer.SetPosition(2, Camera.main.ScreenToWorldPoint(screenEndPoint));
lineRenderer.SetPosition(3, Camera.main.ScreenToWorldPoint(point2));
}
//鼠標(biāo)抬起
if (Input.GetMouseButtonUp(0))
{
//取消光線渲染器
lineRenderer.enabled = false;
}
}
}如圖所示,已經(jīng)實(shí)現(xiàn)框選范圍的繪制:

?? 根據(jù)框選范圍定位其在世界坐標(biāo)系中對應(yīng)的區(qū)域
該部分的實(shí)現(xiàn)主要依靠物理射線檢測,在鼠標(biāo)位置發(fā)出射線,檢測與地面的碰撞點(diǎn),首先為Plane地面設(shè)置Layer層級:

在鼠標(biāo)按下時根據(jù)射線檢測信息確定世界坐標(biāo)系中的起始點(diǎn):
//鼠標(biāo)按下
if (Input.GetMouseButtonDown(0))
{
//激活光線渲染器
lineRenderer.enabled = true;
//屏幕坐標(biāo)系起始點(diǎn)
screenStartPoint = Input.mousePosition;
screenStartPoint.z = 1;
//射線檢測
if (Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out hit, 1 << LayerMask.NameToLayer("Ground")))
{
//世界坐標(biāo)系起始點(diǎn)
worldStartPoint = hit.point;
}
}在鼠標(biāo)抬起時根據(jù)射線檢測信息確定世界坐標(biāo)系中的結(jié)束點(diǎn):
//鼠標(biāo)抬起
if (Input.GetMouseButtonUp(0))
{
//取消光線渲染器
lineRenderer.enabled = false;
//射線檢測
if (Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out hit, 1 << LayerMask.NameToLayer("Ground")))
{
//世界坐標(biāo)系結(jié)束點(diǎn)
worldEndPoint = hit.point;
}
}?? 在該區(qū)域內(nèi)進(jìn)行物理檢測
該部分用的的核心API:

可以理解為創(chuàng)建一個碰撞盒來檢測該范圍內(nèi)的碰撞體,首先計算出該API需要傳入的參數(shù):
center:該盒子的中心點(diǎn);halfExtents:該盒子長寬高的一半。
//盒子中心點(diǎn) Vector3 center = new Vector3((worldEndPoint.x + worldStartPoint.x) * .5f, 1f, (worldEndPoint.z + worldStartPoint.z) * .5f); //盒子長寬高的一半 Vector3 halfExtents = new Vector3(Mathf.Abs(worldEndPoint.x - worldStartPoint.x) * .5f, 1f, Mathf.Abs(worldEndPoint.z - worldStartPoint.z) * .5f);
有了這兩個參數(shù),調(diào)用該API可以獲得該區(qū)域內(nèi)的所有碰撞體,遍歷判斷碰撞體身上如果包含指定的組件,則將其選中,這里使用Outline高亮組件來表示:
//盒子中心點(diǎn)
Vector3 center = new Vector3((worldEndPoint.x + worldStartPoint.x) * .5f, 1f, (worldEndPoint.z + worldStartPoint.z) * .5f);
//盒子長寬高的一半
Vector3 halfExtents = new Vector3(Mathf.Abs(worldEndPoint.x - worldStartPoint.x) * .5f, 1f, Mathf.Abs(worldEndPoint.z - worldStartPoint.z) * .5f);
//檢測到盒子內(nèi)的碰撞器
Collider[] colliders = Physics.OverlapBox(center, halfExtents);
for (int i = 0; i < colliders.Length; i++)
{
var collider = colliders[i];
var outline = collider.GetComponent<Outline>();
if (outline != null)
{
outline.enabled = true;
}
}如圖所示,我們已經(jīng)實(shí)現(xiàn)了基本的框選功能:

在框選時,還需要清除上一次框選的內(nèi)容,因此我們使用一個List列表來記錄當(dāng)前框選的戰(zhàn)斗單位,框選前遍歷該列表來清除框選記錄,完整代碼如下:
public class Example : MonoBehaviour
{
//光線渲染器組件
private LineRenderer lineRenderer;
//屏幕坐標(biāo)系起始點(diǎn)
private Vector3 screenStartPoint;
//屏幕坐標(biāo)系結(jié)束點(diǎn)
private Vector3 screenEndPoint;
//主相機(jī)
private Camera mainCamera;
//碰撞信息
private RaycastHit hit;
//世界坐標(biāo)系起始點(diǎn)
private Vector3 worldStartPoint;
//世界坐標(biāo)系結(jié)束點(diǎn)
private Vector3 worldEndPoint;
//框選記錄列表
private List<Outline> list = new List<Outline>();
private void Start()
{
//獲取光線渲染器組件
lineRenderer = GetComponent<LineRenderer>();
//獲取主相機(jī)
mainCamera = Camera.main != null ? Camera.main : FindObjectOfType<Camera>();
}
private void Update()
{
//鼠標(biāo)按下
if (Input.GetMouseButtonDown(0))
{
//激活光線渲染器
lineRenderer.enabled = true;
//屏幕坐標(biāo)系起始點(diǎn)
screenStartPoint = Input.mousePosition;
screenStartPoint.z = 1;
//射線檢測
if (Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out hit, 1 << LayerMask.NameToLayer("Ground")))
{
//世界坐標(biāo)系起始點(diǎn)
worldStartPoint = hit.point;
}
}
//鼠標(biāo)持續(xù)按下
if (Input.GetMouseButton(0))
{
//屏幕坐標(biāo)系結(jié)束點(diǎn)
screenEndPoint = Input.mousePosition;
screenEndPoint.z = 1;
//求得框選框的另外兩個頂點(diǎn)的位置
Vector3 point1 = new Vector3(screenEndPoint.x, screenStartPoint.y, 1);
Vector3 point2 = new Vector3(screenStartPoint.x, screenEndPoint.y, 1);
//接下來使用光線渲染器畫出框選范圍
lineRenderer.SetPosition(0, Camera.main.ScreenToWorldPoint(screenStartPoint));
lineRenderer.SetPosition(1, Camera.main.ScreenToWorldPoint(point1));
lineRenderer.SetPosition(2, Camera.main.ScreenToWorldPoint(screenEndPoint));
lineRenderer.SetPosition(3, Camera.main.ScreenToWorldPoint(point2));
}
//鼠標(biāo)抬起
if (Input.GetMouseButtonUp(0))
{
//取消光線渲染器
lineRenderer.enabled = false;
//首先清除上一次的框選記錄
for (int i = 0; i < list.Count; i++)
{
list[i].enabled = false;
}
list.Clear();
//射線檢測
if (Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out hit, 1 << LayerMask.NameToLayer("Ground")))
{
//世界坐標(biāo)系結(jié)束點(diǎn)
worldEndPoint = hit.point;
//盒子中心點(diǎn)
Vector3 center = new Vector3((worldEndPoint.x + worldStartPoint.x) * .5f, 1f, (worldEndPoint.z + worldStartPoint.z) * .5f);
//盒子長寬高的一半
Vector3 halfExtents = new Vector3(Mathf.Abs(worldEndPoint.x - worldStartPoint.x) * .5f, 1f, Mathf.Abs(worldEndPoint.z - worldStartPoint.z) * .5f);
//檢測到盒子內(nèi)的碰撞器
Collider[] colliders = Physics.OverlapBox(center, halfExtents);
for (int i = 0; i < colliders.Length; i++)
{
var collider = colliders[i];
var outline = collider.GetComponent<Outline>();
if (outline != null)
{
list.Add(outline);
outline.enabled = true;
}
}
}
}
}
}到此這篇關(guān)于Unity 如何實(shí)現(xiàn)框選游戲戰(zhàn)斗單位的文章就介紹到這了,更多相關(guān)Unity框選游戲戰(zhàn)斗單位內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C# 定時器保活機(jī)制引起的內(nèi)存泄露問題解決
這篇文章主要介紹了C# 定時器?;顧C(jī)制引起的內(nèi)存泄露問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
C#利用Task實(shí)現(xiàn)任務(wù)超時多任務(wù)一起執(zhí)行的方法
這篇文章主要給大家介紹了關(guān)于C#利用Task實(shí)現(xiàn)任務(wù)超時,多任務(wù)一起執(zhí)行的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友下面來一起看看吧。2017-12-12
C#基于ScottPlot實(shí)現(xiàn)可視化的示例代碼
這篇文章主要為大家詳細(xì)介紹了C#如何基于ScottPlot實(shí)現(xiàn)可視化效果,文中的示例代碼講解詳細(xì),具有一定的借鑒價值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01
C#實(shí)現(xiàn)讓窗體永遠(yuǎn)在窗體最前面顯示的實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)讓窗體永遠(yuǎn)在窗體最前面顯示,功能非常實(shí)用,需要的朋友可以參考下2014-07-07

