Unity实战案例全解析:RTS游戏的框选和阵型功能(2) 生成选择框

前篇:Unity实战案例全解析:RTS游戏的框选和阵型功能(1) 基础要素-****博客

本案例来源于unity唐老狮,有兴趣的小伙伴可以去泰克在线观看该课程

【唐老狮】Unity实现 即时战略游戏 阵型功能 - 泰课在线 -- 志存高远,稳如泰山 - 国内专业的在线学习平台|Unity3d培训|Unity教程|Unity教程 Unreal 虚幻 AR|移动开发|美术CG - Powered By EduSoho (taikr.net)

我只是对重要功能进行分析和做出笔记分享,并未无师自通,吃水不忘打井人

本案例的实现流程图 

框选功能要素图 

本文实现可视框部分,其中重置框作为一个小函数,并不用详解

分析

对于可视化线条,自然而然想到的是Line Render组件

unity保姆级教程之 Line Renderer_unity linerenderer-****博客

当鼠标左键按下的时候,就记录第一个点,鼠标抬起就记录最后一个点

然后根据这两个点的坐标,进行矩形的四角分配,其中z轴可以自行分配值

基本字段

    //画线组件
    private LineRenderer lineRenderer;
    //鼠标按下检测
    private bool isMouseDown;
    //四个点
    private Vector3 leftUpPos;//1
    private Vector3 rightUpPos;//2
    private Vector3 leftDownPos;//3
    private Vector3 rightDownPos;//4

但是,还有一个问题,目前的四个点是在相机面前的也就是2D的,而项目是3D的

可能出现绘制出来了线框但是并没有出现在实际3D世界的位置中,而且相机旋转也可能导致绘制框的不准确,所以需要将其转为世界坐标系

画线函数

    private void DrawLine()
        { 
        if(Input.GetMouseButtonDown(0))
            {
            isMouseDown=true;
            leftUpPos = Input.mousePosition;
            leftUpPos.z = 5; // 设置z轴值
            RayBegin();
        }
        if(Input.GetMouseButtonUp(0))
            {
            isMouseDown = false;
            lineRenderer.positionCount = 0;
            ResetSelect();
            RaySelect();
        }
       
        FourFarmePos();
    }

 处理四点的函数

    /// <summary>
    /// 处理框选框四个点的位置
    /// </summary>
    private void FourFarmePos()
        {
        if (isMouseDown){
            rightDownPos = Input.mousePosition;
            rightDownPos.z = 5;

            rightUpPos = new Vector3(rightDownPos.x, leftUpPos.y, 5);
            leftDownPos = new Vector3(leftUpPos.x, rightDownPos.y, 5);

            lineRenderer.positionCount = 4;
            lineRenderer.SetPosition(0, Camera.main.ScreenToWorldPoint(leftUpPos));
            lineRenderer.SetPosition(1, Camera.main.ScreenToWorldPoint(rightUpPos));
            lineRenderer.SetPosition(2, Camera.main.ScreenToWorldPoint(rightDownPos));
            lineRenderer.SetPosition(3, Camera.main.ScreenToWorldPoint(leftDownPos));    
            }
    }

总览 

    private void Start() {
        lineRenderer = GetComponent<LineRenderer>();
        isMouseDown =false;
        layermask = LayerMask.GetMask("Bk");
        soliderObjs = new List<SoliderObj>();
    }

    private void Update() {
        DrawLine();
    }
    
    private void DrawLine()
        { 
        if(Input.GetMouseButtonDown(0))
            {
            isMouseDown=true;
            leftUpPos = Input.mousePosition;
            leftUpPos.z = 5; // 设置z轴值
            RayBegin();
        }
        if(Input.GetMouseButtonUp(0))
            {
            isMouseDown = false;
            lineRenderer.positionCount = 0;
            ResetSelect();
            RaySelect();
        }
       
        FourFarmePos();
    }
    /// <summary>
    /// 处理框选框四个点的位置
    /// </summary>
    private void FourFarmePos()
        {
        if (isMouseDown){
            rightDownPos = Input.mousePosition;
            rightDownPos.z = 5;

            rightUpPos = new Vector3(rightDownPos.x, leftUpPos.y, 5);
            leftDownPos = new Vector3(leftUpPos.x, rightDownPos.y, 5);

            lineRenderer.positionCount = 4;
            lineRenderer.SetPosition(0, Camera.main.ScreenToWorldPoint(leftUpPos));
            lineRenderer.SetPosition(1, Camera.main.ScreenToWorldPoint(rightUpPos));
            lineRenderer.SetPosition(2, Camera.main.ScreenToWorldPoint(rightDownPos));
            lineRenderer.SetPosition(3, Camera.main.ScreenToWorldPoint(leftDownPos));    
            }
    }

上一篇:idea 创建多模块项目


下一篇:【HTTPS】中间人攻击和证书的验证