Shell脚本 线程 DHCP arrays mysqli dns compilation Select2 admin框架 vm虚拟化引擎 java解析json数组 python抛异常 java运行环境配置 java怎么学 xp系统修复 stata软件 python队列 js轮播图代码 vue上传文件 系统维护工具 js保留两位小数 pdf密码移除 微信临时链接多久失效 painter下载 0000008e 幽灵推 苹果手机耳机没声音 电脑内录软件 wps苹果电脑版 ansys安装 软件编程软件 怎么用打印机扫描文件 attrs dos常用命令 心蓝软件 ppt取色器 cad电子签名怎么做 php源代码 word图表制作 美图秀秀2013
当前位置: 首页 > 学习教程  > 编程语言

框选

2020/11/24 9:48:19 文章标签: 测试文章如有侵权请发送至邮箱809451989@qq.com投诉后文章立即删除

https://www.cnblogs.com/firepad-magic/p/6012957.html using UnityEngine; using System.Collections.Generic;public class DrawRectangle : MonoBehaviour {[Header("相框颜色")]public Color rectColor Color.green;[Header("渲染相机")]public Cam…

https://www.cnblogs.com/firepad-magic/p/6012957.html

using UnityEngine;
using System.Collections.Generic;

public class DrawRectangle : MonoBehaviour
{
    [Header("相框颜色")]
    public Color rectColor = Color.green;
    [Header("渲染相机")]
    public Camera camera;

    private Vector3 start = Vector3.zero;//记下鼠标按下位置
    private Material rectMat = null;//画线的材质 不设定系统会用当前材质画线 结果不可控
    private bool drawRectangle = false;//是否开始画线标志

    private List<GameObject> characters = new List<GameObject>();

    // Use this for initialization
    void Start()
    {
        rectMat = new Material("Shader \"Lines/Colored Blended\" {" +
            "SubShader { Pass { " +
            "    Blend SrcAlpha OneMinusSrcAlpha " +
            "    ZWrite Off Cull Off Fog { Mode Off } " +
            "    BindChannels {" +
            "      Bind \"vertex\", vertex Bind \"color\", color }" +
            "} } }");//生成画线的材质
        rectMat.hideFlags = HideFlags.HideAndDontSave;
        rectMat.shader.hideFlags = HideFlags.HideAndDontSave;

        GameObject[] goes = GameObject.FindGameObjectsWithTag("Player");
        for (int i = 0; i < goes.Length; i++)
            characters.Add(goes[i]);
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            drawRectangle = true;//如果鼠标左键按下 设置开始画线标志
            start = Input.mousePosition;//记录按下位置
        }
        else if (Input.GetMouseButtonUp(0))
        {
            drawRectangle = false;//如果鼠标左键放开 结束画线
            CheckSelection(start, Input.mousePosition);//框选物体
        }
    }

    void OnPostRender()
    {
        //画线这种操作推荐在OnPostRender()里进行 而不是直接放在Update,所以需要标志来开启
        if (drawRectangle)
        {
            Vector3 end = Input.mousePosition;//鼠标当前位置
            GL.PushMatrix();//保存摄像机变换矩阵

            if (!rectMat)
                return;

            rectMat.SetPass(0);
            GL.LoadPixelMatrix();//设置用屏幕坐标绘图

            GL.Begin(GL.QUADS);
            GL.Color(new Color(rectColor.r, rectColor.g, rectColor.b, 0.1f));//设置颜色和透明度,方框内部透明
            GL.Vertex3(start.x, start.y, 0);
            GL.Vertex3(end.x, start.y, 0);
            GL.Vertex3(end.x, end.y, 0);
            GL.Vertex3(start.x, end.y, 0);
            GL.End();

            GL.Begin(GL.LINES);
            GL.Color(rectColor);//设置方框的边框颜色 边框不透明
            GL.Vertex3(start.x, start.y, 0);
            GL.Vertex3(end.x, start.y, 0);
            GL.Vertex3(end.x, start.y, 0);
            GL.Vertex3(end.x, end.y, 0);
            GL.Vertex3(end.x, end.y, 0);
            GL.Vertex3(start.x, end.y, 0);
            GL.Vertex3(start.x, end.y, 0);
            GL.Vertex3(start.x, start.y, 0);
            GL.End();

            GL.PopMatrix();//恢复摄像机投影矩阵
        }
    }
    void CheckSelection(Vector3 start, Vector3 end)
    {
        Vector3 p1 = Vector3.zero;
        Vector3 p2 = Vector3.zero;
        if (start.x > end.x)
        {
            //这些判断是用来确保p1的xy坐标小于p2的xy坐标,因为画的框不见得就是左下到右上这个方向的
            p1.x = end.x;
            p2.x = start.x;
        }
        else
        {
            p1.x = start.x;
            p2.x = end.x;
        }
        if (start.y > end.y)
        {
            p1.y = end.y;
            p2.y = start.y;
        }
        else
        {
            p1.y = start.y;
            p2.y = end.y;
        }

        foreach (GameObject obj in characters)
        {
            //把可选择的对象保存在characters数组里
            Vector3 location = camera.WorldToScreenPoint(obj.transform.position);//把对象的position转换成屏幕坐标
            if (location.x < p1.x || location.x > p2.x || location.y < p1.y || location.y > p2.y
                || location.z < camera.nearClipPlane || location.z > camera.farClipPlane)//z方向就用摄像机的设定值,看不见的也不需要选择了
            {
                Disselecting(obj);//上面的条件是筛选 不在选择范围内的对象,然后进行取消选择操作,比如把物体放到default层,就不显示轮廓线了
            }
            else
            {
                Selecting(obj);//否则就进行选中操作,比如把物体放到画轮廓线的层去
            }
        }
    }

    void Disselecting(GameObject obj)
    {
        obj.GetComponent<ChangeShader>().DisSelect();
    }

    void Selecting(GameObject obj)
    {
        obj.GetComponent<ChangeShader>().Select();
    }
}
using UnityEngine;
using System.Collections;

public class ChangeShader : MonoBehaviour
{

    private Material material;
    private Shader originShader;
    public Shader rimShader;

    void Start()
    {
        material = GetComponentInChildren<SkinnedMeshRenderer>().material;
        originShader = material.shader;
    }

    public void Select()
    {
        material.shader = rimShader;
    }

    public void DisSelect()
    {
        material.shader = originShader;
    }
}

 

 


本文链接: http://www.dtmao.cc/news_show_400125.shtml

附件下载

相关教程

    暂无相关的数据...

共有条评论 网友评论

验证码: 看不清楚?