Unity-WIKI 之 DebugLine

简介:



脚本功能

在Unity中要使用画线功能,需要添加LineRenderer 组件,或打开Gizmos功能,下面这个组件为开发带来了方便

功能预览

image image image

搭建步骤

1、创建“Plugins” 文件夹

2、在“Plugins”中创建"DebugLine.cs"

3、提供两个方法:DrawLine() or DebugLine.DrawRay()

注意:场景中必须有一个相机的tag是MainCamera

组件源码

using UnityEngine;
using System.Collections;

public class UseDebugLine : MonoBehaviour {

    // Use this for initialization
    void Start () {
        DebugLine.DrawLine(new Vector3(0, 0, 0), new Vector3(5, 5, -5), Color.cyan, 4, 2);
    }
    
    // Update is called once per frame
    void Update () {
        DebugLine.DrawRay(transform.position, transform.forward * 10, Color.blue);
        DebugLine.DrawRay(transform.position, transform.up * 10, Color.green);
        DebugLine.DrawRay(transform.position, transform.right * 10, Color.red);
    }
}

DebugLine.cs

/* Author: Spencer Evans
 * 
 * Description:
 * DebugLine.cs provides 2 useful functions for debugging : DrawLine and DrawRay. These functions are almost identical to those provided in the Unity 3D 
 * engine Debug class (http://docs.unity3d.com/Documentation/ScriptReference/Debug.html), except in the following ways:
 * 1. The lines are drawn in the Game window, not the Scene editor window.
 * 2. A "width" option is provided - allowing the user to choose the width in pixels of the line drawn.
 * 3. The "depthTest" parameter in the Debug.DrawLine() and Debug.DrawRay() functions is unavailable; all lines are obscured by objects closer to the camera.
 * 
 * Usage:
 * 1. Create a new folder titled "Plugins", and move it to your project's "/Assets" folder (if you haven't already).
 * 2. Create and place "DebugLine.cs" inside the "/Plugins" folder.
 * 3. Call the DebugLine.DrawLine() or DebugLine.DrawRay() functions just as you would with the Debug class.
 * 
 * Notes:
 * 1. You must have a Camera in your scene tagged "MainCamera" for these debug functions to work properly.
 * 2. You do NOT need Unity Pro for this to work.
 */
 
using UnityEngine;
using System.Collections;
 
public class DebugLine : MonoBehaviour
{
    //used to make calls from both Update and FixedUpdate function properly
    public float destroy_time = float.MaxValue;
    public float fixed_destroy_time = float.MaxValue;
 
    //draw ray functions - http://docs.unity3d.com/Documentation/ScriptReference/Debug.DrawRay.html
    public static void DrawRay(Vector3 start, Vector3 dir)
    {
        DebugLine.DrawLine(start, start + dir, Color.white);
    }
    public static void DrawRay(Vector3 start, Vector3 dir, Color color, float duration = 0, float width = 1)
    {
        DebugLine.DrawLine(start, start + dir, color, duration, width);
    }
 
    //draw line functions - http://docs.unity3d.com/Documentation/ScriptReference/Debug.DrawLine.html
    public static void DrawLine(Vector3 start, Vector3 end)
    {
        DebugLine.DrawLine(start, end, Color.white);
    }
    public static void DrawLine(Vector3 start, Vector3 end, Color color, float duration = 0, float width = 1)
    {
        //early out if there is no Camera.main to calculate the width
        if (!Camera.main)
            return;
 
        GameObject line = new GameObject("debug_line");
 
        //set the params of the line renderer - http://docs.unity3d.com/Documentation/ScriptReference/LineRenderer.html
        LineRenderer lineRenderer = line.AddComponent<LineRenderer>();
        lineRenderer.material = new Material(Shader.Find("Mobile/Particles/Additive"));
        lineRenderer.SetPosition( 0, start );
        lineRenderer.SetPosition( 1, end );
        lineRenderer.SetColors( color, color );
        lineRenderer.SetWidth( DebugLine.CalcPixelHeightAtDist( (start - Camera.main.transform.position).magnitude) * width,
                               DebugLine.CalcPixelHeightAtDist( (end - Camera.main.transform.position).magnitude) * width    );
 
        //add the MonoBehaviour instance
        DebugLine debugLine = line.AddComponent<DebugLine>();
 
        //set the time to expire - default is 0 (will only draw for one update cycle)
        //use Update or FixedUpdate depending on the time of the invoking call
        if (Time.deltaTime == Time.fixedDeltaTime)
            debugLine.fixed_destroy_time = Time.fixedTime + duration;
        else    debugLine.destroy_time = Time.time + duration;
    }
 
    //utility function to calculate the world height of a single pixel given the following info:
    // - Camera.main.fov
    // - Camera.main.pixelHeight
    // - distance to the camera
    public static float CalcPixelHeightAtDist(float dist)
    {
        //early out if there is no Camera.main to calculate the width
        if (!Camera.main)
            return 0;
 
        //http://docs.unity3d.com/Documentation/Manual/FrustumSizeAtDistance.html
        float frustumHeight = 2 * dist * Mathf.Tan(Camera.main.fieldOfView * 0.5f * Mathf.Deg2Rad);
 
        return frustumHeight / Camera.main.pixelHeight;
    }
 
    //check to see if should be destroyed yet
    public void Update ()
    {
        if (Time.time > destroy_time)
            Destroy(transform.gameObject);
    }
    public void FixedUpdate ()
    {
        if (Time.fixedTime > fixed_destroy_time)
            Destroy(transform.gameObject);
    }
}

本文转自赵青青博客园博客,原文链接:http://www.cnblogs.com/zhaoqingqing/p/3648861.html,如需转载请自行联系原作者
相关文章
|
算法 图形学
Unity 开发入门指南
学习 Unity 开发接近一年了,从开始对 Unity 一无所知,到现在能模仿制作出一个完整游戏,对个人成长来说,也算是一个不大不小的里程碑。这篇是对自己学习的总结与回顾,希望对 Unity 初学者有所帮助。
1322 0
|
JavaScript 前端开发
|
数据可视化 C# 图形学
|
机器学习/深度学习
Wiki
TensorFlow官方文件机器学习之训练集_验证集_测试集 探寻有趣之事!
997 0