unity做一个标记线

简单的使用 GL 来画线,OnGui来显示两点长度数据 写的比较乱有时间再整理一下吧
unity做一个标记线_第1张图片

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GUI_test : MonoBehaviour
{
    public GUIStyle mystyle;
    public Material LineMat;
    public Color LineColor;
    public float _Thickness = 1.0f;
    public Vector3 v1;
    public Vector3 v2;
    public Vector3 v3;
    
    public Vector3 vff1;
    public Vector3 vff2;
    public Vector3 vff3;

    public Transform tf1;
    public Transform tf2;
    public Transform tf3;
    
    // Use this for initialization
    void Start()
    {
        mystyle.fontSize = 30;
        mystyle.normal.textColor = Color.white;
    }

    private void OnGUI()
    {
        Vector3 ScreenPos = Camera.main.WorldToScreenPoint(transform.position); //模型位置
        //Vector3 ScreenPos = Input.mousePosition;//鼠标位置
        Vector3 GUIPos = new Vector3(ScreenPos.x, Screen.height - ScreenPos.y, 0);

        float _fd = Vector3.Distance(Vector3.zero, new Vector3(this.v1.x, this.v2.y, this.v3.z));

        tf1.transform.localPosition = GetBetweenPoint(v1,v2,0.5f);
        tf2.transform.localPosition = GetBetweenPoint(v2, v3, 0.5f);
        tf3.transform.localPosition = GetBetweenPoint(v3, v1, 0.5f);
        tf1.GetComponent().myStr = (v1 - v2).sqrMagnitude.ToString();
        tf2.GetComponent().myStr = (v2 - v3).sqrMagnitude.ToString();
        tf3.GetComponent().myStr = (v3 - v1).sqrMagnitude.ToString();

        //GUI.Label(new Rect(GUIPos.x, GUIPos.y, 0, 0), _fd.ToString("f2"), mystyle);
    }

    private void OnRenderObject()
    {
        Debug.Log("render");
        LineMat.SetColor("_LineColor", LineColor);
        LineMat.SetFloat("_Thickness", _Thickness);
        LineMat.SetPass(0);

        GL.PushMatrix();
        //转换到世界坐标
        GL.MultMatrix(transform.localToWorldMatrix);
        GL.Begin(GL.LINES);

        GL.Vertex(v1);//2
        GL.Vertex(v2);

        GL.Vertex(v2);//3
        GL.Vertex(v3);

        GL.Vertex(v3);//6
        GL.Vertex(v1);

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

    private Vector3 GetBetweenPoint(Vector3 start, Vector3 end, float percent)
    {
        Vector3 normal = (end - start).normalized;
        float distance = Vector3.Distance(start, end);
        return normal * (distance * percent) + start;
    }
}

unity做一个标记线_第2张图片




using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Item_Test : MonoBehaviour {
    public GUIStyle mystyle;
    public string myStr;
    // Use this for initialization
    void Start ()
    {
        mystyle.normal.textColor = Color.white;
        mystyle.fontSize = 30;
    }

    private void OnGUI()
    {
        Vector3 ScreenPos = Camera.main.WorldToScreenPoint(transform.position); //模型位置
        //Vector3 ScreenPos = Input.mousePosition;//鼠标位置
        Vector3 GUIPos = new Vector3(ScreenPos.x, Screen.height - ScreenPos.y, 0);

        GUI.Label(new Rect(GUIPos.x, GUIPos.y, 100, 30), myStr, mystyle);
    }
}

你可能感兴趣的:(UNITY)