• Unity测量周长小工具


    实现效果如下:
    我们这里鼠标点击使用的是圆点的预制体,大家可以根据自己项目需求制作预制体

    点击两个点可以测出两个点之间的距离,并计算总长度

    实现代码如下:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using System;
    using UnityEngine.UI;
    
    public class DistanceTest : MonoBehaviour
    {
        public Camera camera;
        static Material lineMaterial;
        public Text Count;
        public Text Length;
        bool IsStart = false;
        public void sta()
        {
            IsStart = true;
        }
        public void clo()
        {
            IsStart = false;
        }
        static void CreateLineMaterial()
        {
            if (!lineMaterial)
            {
    
                Shader shader = Shader.Find("Hidden/Internal-Colored");
                lineMaterial = new Material(shader);
                lineMaterial.hideFlags = HideFlags.HideAndDontSave;
                // Turn on alpha blending
                lineMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
                lineMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
                // Turn backface culling off
                lineMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
                // Turn off depth writes
                lineMaterial.SetInt("_ZWrite", 0);
            }
        }
    
        public void OnRenderObject()
        {
            CreateLineMaterial();
            lineMaterial.SetPass(0);
            GL.PushMatrix();
            GL.Begin(GL.LINES);
            for (int i = 0; i < lv.Count; i++)
            {
                GL.Vertex3(lv[i].x, lv[i].y, lv[i].z);
            }
            GL.End();
            GL.PopMatrix();
    
        }
        bool sb = false;
        //圆点的预制体
        public GameObject aim;
        //GL 绘制的顶点数组  顺序是  0->1  2->3 4->5    取法 0 1 3 5 7 9
        //参考UI界面
        public List<Vector3> lv;
        private List<GameObject> aims;
        public Vector3 V3;
        void Start()
        {
            lv = new List<Vector3>();
            aims = new List<GameObject>();
            V3 = Vector3.zero;
        }
        void Update()
        {
            if (IsStart)
            {
                if (Input.GetMouseButtonDown(0))
                {
                    Vector3 point = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 4.0f);//获得鼠标点击点               
                    point = camera.ScreenToWorldPoint(point);//从屏幕空间转换到世界空间
                                                             // point = Camera.main.ScreenToWorldPoint(point);//从屏幕空间转换到世界空间
                    GameObject go = Instantiate(aim);//生成点位           
                    go.transform.position = point;
                    aims.Add(go);
                    aims.Add(Instantiate(aim, new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z), Quaternion.Euler(90, 0, 0)) as GameObject);
                    if (lv.Count >= 2)
                    {
                        lv.Add(lv[lv.Count - 1]);
                        lv.Add(point);
                    }
                    else
                    {
                        lv.Add(point);
                    }
                }
            }
        }
        void OnGUI()
        {
            // 利用gui 为了实时动态更新画线数据
            if (lv.Count >= 2)
            {
                float dis = 0;
                for (int i = 0; i < lv.Count - 1; i = i + 2)//除了第一个点和最后个点,其它点都是存了两遍
                {
                    Vector3 s = new Vector3((lv[i].x + lv[i + 1].x) / 2, (lv[i].y + lv[i + 1].y) / 2, (lv[i].z + lv[i + 1].z) / 2);
                    Vector3 a = camera.WorldToScreenPoint(s);
                    //注意屏幕坐标系与GUI的ui坐标系y轴相反,ToString(".000")保留小数点后3位数,几个零几位数
                    GUI.Label(new Rect(a.x, Screen.height - a.y, 100, 20), "<color=red>" + (Vector3.Distance(lv[i], lv[i + 1]) > 1 ? Vector3.Distance(lv[i], lv[i + 1]).ToString(".000") : 0 + Vector3.Distance(lv[i], lv[i + 1]).ToString(".000")) + "</color>");
                    //GUI.Label(new Rect(a.x, Screen.height - a.y, 100, 20),  Vector3.Distance(lv[i], lv[i + 1]).ToString(".000")  + "<color=blue>" + "米" + "</color>");
    
                }
            }
            if (lv.Count >= 1)
            {
                //Debug.Log(lv + "lv");
                //GUI.Label(new Rect(15, 15, 120, 40), ComputePolygonLength(lv).ToString(".000"));
                Length.text = ComputePolygonLength(lv) > 1 ? ComputePolygonLength(lv).ToString(".000") : 0 + ComputePolygonLength(lv).ToString(".000");
                Count.text = (lv.Count < 2 ? 1 : (lv.Count / 2 + 1)).ToString();
            }
        }
        public void ClearLines()
        {
    
            for (int i = 0; i < aims.Count; i++)
            {
                GameObject.Destroy(aims[i]);
            }
            lv.Clear();
            aims.Clear();
            Length.text = "0.0";
            Count.text = "0";
    
        }
    
        double ComputePolygonLength(List<Vector3> points)
        {
            // Debug.Log((points.Count)/2 +1);       
            float s = 0;
            if ((points.Count + 2) < 2)
                return s;
            else
            {
                for (int i = 0; i < points.Count - 1; i++)
                    s += Vector3.Distance(points[i], points[i + 1]);
            }
            return Mathf.Abs(s);
        }
    }
    //public class DistanceTest : MonoBehaviour
    //{
    //    Vector3 point;
    //    public GameObject target;
    //    private List<GameObject> targets;
    //    void Start()
    //    {
    //    }
    
    //    void Update()
    //    {
    //        if (Input.GetMouseButtonDown(0))
    //        {
    //            point = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 4.0f);//获得鼠标点击点
    //            point = Camera.main.ScreenToWorldPoint(point);//从屏幕空间转换到世界空间
    //            GameObject go = Instantiate(target);//生成点位           
    //            go.transform.position = point;
    //        }
    //    }
    
    //    public void Clear()
    //    {
    
    
    //        for (int i = 0; i < targets.Count; i++)
    //        {
    //            GameObject.Destroy(targets[i]);
    //        }
    //        targets.Clear();
    //    }
    //    double ComputePolygonLength(List<Vector3> points)
    //    {
    //        float s = 0;
    //        int point_num = points.Count;
    //        if (point_num < 1)
    //            return s;
    //        for (int i = 0; i < point_num; ++i)
    //            s += Vector3.Distance(points[i], points[i+1]);
    //        return Mathf.Abs(s);
    //    }
    //}
    
  • 相关阅读:
    Oracle存储过程
    Oracle触发器
    Oracle伪列
    Oracle索引
    Oracle视图
    Oracle数据库链路
    异常处理之动手动脑
    四则运算自动出题之javaweb版
    继承与多态之动手动脑
    javaweb之添加学生信息
  • 原文地址:https://www.cnblogs.com/AranNice/p/16401248.html
Copyright © 2020-2023  润新知