Unity-WIKI 之 DebugLine
2024-08-26 00:13:56
脚本功能
在Unity中要使用画线功能,需要添加LineRenderer 组件,或打开Gizmos功能,下面这个组件为开发带来了方便
功能预览
搭建步骤
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);
}
}
最新文章
- B样条基函数的定义和性质
- HDU5456 Matches Puzzle Game(DP)
- 【集合框架】JDK1.8源码分析之ArrayList(六)
- CyclicBarrier类合唱演绎
- JavaMail入门第二篇 创建邮件
- 模拟 2013年山东省赛 J Contest Print Server
- [Hibernate] - Interceptors and events
- 【JS】Beginner3 &; 4 &; 5 &; 6:Maths &; Logic &; Conditonal &; Looping
- eclipse 中修改 M2_REPO的值--转载
- Iis 日志文件默认路径
- Android应用程序中Activity的生命周期
- Net Core- 配置组件
- 学习Java第一篇——Java 安装及环境搭配
- iOS项目之模拟请求数据
- 数据结构--二叉查找树的java实现
- 软件推荐-c#绘图插件echart
- Oracle使用——oracle表锁住,杀掉锁表进程
- 牛客第二场A-run
- TCP/IP协议(3):数据链路层
- MySQL与Oracle集群主从复制工具
热门文章
- html alert 的三种方式
- mysql 5.6.33发布
- xCode删除storyboard,新建window并启动
- Python基础(11)--面向对象1
- Emacs常用命令汇总
- 无需cygwin,使用NDK进行开发
- JS 中html 动态替换
- 【等待事件】序列等待事件总结(enq: SQ - contention、row cache lock、DFS lock handle和enq: SV - contention)
- Eclipse启动报错:An internal error occurred during: ";Initializing Java Tooling";.
- Oracle 数据库二 基本查询