Unity3D 点选物体、框选物体、绘制外边框详解

发布时间 2024-01-03 09:31:08作者: 游戏开发阿博

Unity3D是一款强大的游戏引擎,它提供了丰富的功能和工具,使开发者能够轻松创建出高质量的3D游戏。在游戏中,点选物体、框选物体以及绘制外边框是非常常见的功能,本文将详细介绍这些功能的技术详解以及代码实现。

对啦!这里有个游戏开发交流小组里面聚集了一帮热爱学习游戏的零基础小白,也有一些正在从事游戏开发的技术大佬,欢迎你来交流学习。

一、点选物体
点选物体是指通过鼠标点击游戏场景中的物体来选中该物体。在Unity3D中实现点选物体的方法有很多种,以下是其中一种常见的方法:

  1. 在场景中创建一个名为"Clickable"的空物体,作为所有可点击物体的父物体。
  2. 给所有可点击的物体添加一个脚本,例如"ClickHandler"。在该脚本中,需要实现鼠标点击事件的监听和处理逻辑。
  3. 在"ClickHandler"脚本中,需要使用Raycast来检测鼠标点击的位置是否与可点击物体相交。如果相交,则选中该物体。
  4. 在"ClickHandler"脚本中,可以使用OnMouseDown函数来监听鼠标点击事件。在该函数中,可以使用Raycast的方式来检测鼠标点击的位置是否与可点击物体相交,并进行相应的处理。

以下是一个简单的示例代码:

public class ClickHandler : MonoBehaviour
{
    private void OnMouseDown()
    {
        RaycastHit hit;
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        
        if (Physics.Raycast(ray, out hit))
        {
            if (hit.collider.gameObject == gameObject)
            {
                // 选中物体的处理逻辑
            }
        }
    }
}

在上述代码中,首先使用Raycast来检测鼠标点击的位置是否与物体相交,如果相交则选中该物体。可以根据实际需求在选中物体的处理逻辑中进行相应的操作,例如改变物体的颜色、播放动画等。

二、框选物体
框选物体是指通过鼠标拖动一个矩形框来选中多个物体。在Unity3D中实现框选物体的方法如下:

  1. 在场景中创建一个名为"Selectable"的空物体,作为所有可选物体的父物体。
  2. 给所有可选的物体添加一个脚本,例如"SelectionHandler"。在该脚本中,需要实现鼠标拖动事件的监听和处理逻辑。
  3. 在"SelectionHandler"脚本中,需要使用鼠标的按下、拖动和松开事件来实现框选的功能。
  4. 在"SelectionHandler"脚本中,可以使用OnMouseDown、OnMouseDrag和OnMouseUp函数来分别监听鼠标的按下、拖动和松开事件。在这些函数中,可以根据鼠标的位置和状态来判断是否进行框选,并选中相应的物体。

以下是一个简单的示例代码:

public class SelectionHandler : MonoBehaviour
{
    private Vector3 mouseStartPosition;
    private bool isSelecting = false;

    private void OnMouseDown()
    {
        isSelecting = true;
        mouseStartPosition = Input.mousePosition;
    }

    private void OnMouseDrag()
    {
        if (isSelecting)
        {
            // 绘制框选的矩形
            Rect selectionRect = GetSelectionRect(mouseStartPosition, Input.mousePosition);
            DrawSelectionRect(selectionRect);

            // 检测物体是否被框选
            foreach (Transform selectable in transform)
            {
                if (selectionRect.Contains(Camera.main.WorldToScreenPoint(selectable.position)))
                {
                    // 选中物体的处理逻辑
                }
            }
        }
    }

    private void OnMouseUp()
    {
        isSelecting = false;
    }

    private Rect GetSelectionRect(Vector3 startPos, Vector3 endPos)
    {
        float width = Mathf.Abs(endPos.x - startPos.x);
        float height = Mathf.Abs(endPos.y - startPos.y);
        float left = Mathf.Min(startPos.x, endPos.x);
        float top = Mathf.Max(startPos.y, endPos.y);
        return new Rect(left, top, width, height);
    }

    private void DrawSelectionRect(Rect rect)
    {
        // 绘制矩形框选的逻辑
    }
}

在上述代码中,首先在鼠标按下事件中记录鼠标的起始位置,并设置isSelecting为true表示正在进行框选。在鼠标拖动事件中,根据鼠标的位置和状态来绘制框选的矩形,并检测是否选中了物体。最后,在鼠标松开事件中将isSelecting设置为false表示框选结束。

三、绘制外边框
绘制外边框是指在选中物体时,在其周围绘制一个外边框以突出显示。在Unity3D中实现绘制外边框的方法如下:

  1. 在选中物体的脚本中,可以使用OnRenderObject函数来绘制外边框。
  2. 在OnRenderObject函数中,可以使用GL类的函数来进行绘制。例如,使用GL.Begin和GL.End函数来定义绘制的模式,使用GL.Color函数来设置绘制的颜色,使用GL.Vertex函数来绘制顶点。

以下是一个简单的示例代码:

public class SelectionHandler : MonoBehaviour
{
    private bool isSelected = false;

    private void OnMouseDown()
    {
        isSelected = true;
    }

    private void OnMouseUp()
    {
        isSelected = false;
    }

    private void OnRenderObject()
    {
        if (isSelected)
        {
            GL.PushMatrix();
            GL.Begin(GL.LINES);
            GL.Color(403 Forbidden);

            Vector3[] vertices = GetBoundingBoxVertices();
            for (int i = 0; i < vertices.Length; i++)
            {
                GL.Vertex(vertices[i]);
                GL.Vertex(vertices[(i + 1) % vertices.Length]);
            }

            GL.End();
            GL.PopMatrix();
        }
    }

    private Vector3[] GetBoundingBoxVertices()
    {
        Renderer renderer = GetComponent<Renderer>();
        Bounds bounds = renderer.bounds;
        Vector3 center = bounds.center;
        Vector3 extents = bounds.extents;

        Vector3[] vertices = new Vector3[8];
        vertices[0] = center + new Vector3(-extents.x, -extents.y, -extents.z);
        vertices[1] = center + new Vector3(-extents.x, -extents.y, extents.z);
        vertices[2] = center + new Vector3(extents.x, -extents.y, extents.z);
        vertices[3] = center + new Vector3(extents.x, -extents.y, -extents.z);
        vertices[4] = center + new Vector3(-extents.x, extents.y, -extents.z);
        vertices[5] = center + new Vector3(-extents.x, extents.y, extents.z);
        vertices[6] = center + new Vector3(extents.x, extents.y, extents.z);
        vertices[7] = center + new Vector3(extents.x, extents.y, -extents.z);

        return vertices;
    }
}

在上述代码中,首先在鼠标按下事件中将isSelected设置为true表示选中了物体。在鼠标松开事件中将isSelected设置为false表示取消选中。在OnRenderObject函数中,根据isSelected的状态来绘制外边框。可以使用Renderer的bounds属性来获取物体的包围盒,然后根据包围盒的顶点来绘制外边框。

总结:
本文详细介绍了Unity3D中点选物体、框选物体和绘制外边框的技术详解以及代码实现。通过学习这些技术,开发者可以轻松实现游戏中常见的交互功能,提升游戏的可玩性和用户体验。希望本文对您有所帮助!