Unity-WIKI 之 DebugLine
脚本功能
在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);
}
}
Unity-WIKI 之 DebugLine的更多相关文章
- 基于Unity有限状态机框架
这个框架是Unity wiki上的框架.网址:http://wiki.unity3d.com/index.php/Finite_State_Machine 这就相当于是“模板”吧,自己写的代码,写啥都 ...
- Unity 相关经典博客资源总结(持续更新)
就作为一个记录吧,把平时看过的Unity相关的一些好的Blog记录并分享. 好的论坛: Unity官方脚本 点评:这个不用说了,最核心的内容,理解整个Unity引擎的方方面面,梳理结构. Unity ...
- 【Unity3D基础教程】给初学者看的Unity教程(零):如何学习Unity3D
作者:王选易,出处:http://www.cnblogs.com/neverdie/ 欢迎转载,也请保留这段声明.如果你喜欢这篇文章,请点推荐.谢谢! Unity3D有什么优势 Unity3D是一个跨 ...
- 【转】Unity 相关经典博客资源总结(持续更新)
原文:http://blog.csdn.net/prothi/article/details/20123319 就作为一个记录吧,把平时看过的Unity相关的一些好的Blog记录并分享. 好的论坛: ...
- 【Unity Shader】2D动态云彩
写在前面 赶在年前写一篇文章.之前翻看2015年的SIGGRAPH Course(关于渲染的可以去selfshadow的博客里找到,很全)的时候看到了关于体积云的渲染.这个课程讲述了开发者为游戏< ...
- Unity Shaderlab: Object Outlines 转
转 https://willweissman.wordpress.com/tutorials/shaders/unity-shaderlab-object-outlines/ Unity Shader ...
- 学习Unity的步骤
作者:王选易链接:https://www.zhihu.com/question/23790314/answer/46815232来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...
- Unity FSM 有限状态机
翻译了一下unity wiki上对于有限状态机的案例,等有空时在详细写一下.在场景中添加两个游戏物体,一个为玩家并修改其Tag为Player,另一个为NPC为其添加NPCControl脚本,并为其将玩 ...
- unity 3d开发的大型网络游戏
unity 3d开发的大型网络游戏 一.总结 1.unity的官网上面应该有游戏列表 2.unity3D是很好的3d游戏引擎,也支持2d,也能做很多画面精良的3A级游戏 3.范围:电脑游戏,手机游戏, ...
- 2019年Unity学习资源指南[精心整理]
前言 进入一个领域,最直接有效的方法就是,寻找相关综述性文章,首先你需要对你入门的领域有个概括性的了解,这些包括: 1.主流的学习社区与网站. 2.该领域的知名大牛与热心分享的从业者. 3.如何有效的 ...
随机推荐
- javascript宿主对象之window.history
window.historys属性允许我们操作同一个浏览器回话中的已访问页面,例如我们可以看到在这之前我们浏览页面的数量: window.history.length 由于隐私保护,我们无法获取这些页 ...
- CSS选择器特殊性与重要性
特殊性 在编写CSS代码的时候,我们会出现多个样式规则作用于同一个元素的情况,例如 <!-- HTML --> <header> <nav class="nav ...
- ready和onload
ready:jQuery方法,当DOM载入就绪时执行.(不包含图片等非文字媒体文件) 它可以极大地提高web应用程序的响应速度. 有一个参数--对jQuery函数的引用--会传递到这个ready事件处 ...
- andriod Spinner
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout android:orientatio ...
- oracle断电重启之ORA-00600[4194]
1.问题描述 Oracle服务器断电重启以后无法数据库无法正常连接,使用sqlplus envision/envision连接报错.常见的错误有以下这些: ORA-12518: TNS:listene ...
- 推荐几个优秀的java爬虫项目
java爬虫项目 大型的: Nutch apache/nutch · GitHub 适合做搜索引擎,分布式爬虫是其中一个功能. Heritrix internetarchive/heritrix3 ...
- Android——五大布局
Android的五大布局分为: 线性布局 相对布局 帧布局 绝对布局 表格布局 一.线性布局 线性布局在开发中使用最多,具有垂直方向与水平方向的布局方式 通过设置属性"android:ori ...
- App开发流程之图像处理工具类
先罗列一下工具类中提供的方法: /** * 根据原始view和毛玻璃样式,获取模糊视图,并自动作为原view的subview(如果不需要作为子视图,自行调用removeFromSuperview) * ...
- AndroidDevTools下载地址
Android Dev Tools官网地址:www.androiddevtools.cn http://www.androiddevtools.cn/ http://wear.techbrood.co ...
- 【转】Python开发指南:最佳实践精选
总体原则 价值 “为别人开发你也想要使用的工具.” ——Kenneth Reitz "简洁总是胜过可用." ——Pieter Hintjens "满足90%的使用场景.忽 ...