unity做一个标记线

简单的使用 GL 来画线,OnGui来显示两点长度数据 写的比较乱有时间再整理一下吧
在这里插入图片描述

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<Item_Test>().myStr = (v1 - v2).sqrMagnitude.ToString();
        tf2.GetComponent<Item_Test>().myStr = (v2 - v3).sqrMagnitude.ToString();
        tf3.GetComponent<Item_Test>().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;
    }
}

在这里插入图片描述




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);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值