脚本功能

在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的更多相关文章

  1. 基于Unity有限状态机框架

    这个框架是Unity wiki上的框架.网址:http://wiki.unity3d.com/index.php/Finite_State_Machine 这就相当于是“模板”吧,自己写的代码,写啥都 ...

  2. Unity 相关经典博客资源总结(持续更新)

    就作为一个记录吧,把平时看过的Unity相关的一些好的Blog记录并分享. 好的论坛: Unity官方脚本  点评:这个不用说了,最核心的内容,理解整个Unity引擎的方方面面,梳理结构. Unity ...

  3. 【Unity3D基础教程】给初学者看的Unity教程(零):如何学习Unity3D

    作者:王选易,出处:http://www.cnblogs.com/neverdie/ 欢迎转载,也请保留这段声明.如果你喜欢这篇文章,请点推荐.谢谢! Unity3D有什么优势 Unity3D是一个跨 ...

  4. 【转】Unity 相关经典博客资源总结(持续更新)

    原文:http://blog.csdn.net/prothi/article/details/20123319 就作为一个记录吧,把平时看过的Unity相关的一些好的Blog记录并分享. 好的论坛: ...

  5. 【Unity Shader】2D动态云彩

    写在前面 赶在年前写一篇文章.之前翻看2015年的SIGGRAPH Course(关于渲染的可以去selfshadow的博客里找到,很全)的时候看到了关于体积云的渲染.这个课程讲述了开发者为游戏< ...

  6. Unity Shaderlab: Object Outlines 转

    转 https://willweissman.wordpress.com/tutorials/shaders/unity-shaderlab-object-outlines/ Unity Shader ...

  7. 学习Unity的步骤

    作者:王选易链接:https://www.zhihu.com/question/23790314/answer/46815232来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...

  8. Unity FSM 有限状态机

    翻译了一下unity wiki上对于有限状态机的案例,等有空时在详细写一下.在场景中添加两个游戏物体,一个为玩家并修改其Tag为Player,另一个为NPC为其添加NPCControl脚本,并为其将玩 ...

  9. unity 3d开发的大型网络游戏

    unity 3d开发的大型网络游戏 一.总结 1.unity的官网上面应该有游戏列表 2.unity3D是很好的3d游戏引擎,也支持2d,也能做很多画面精良的3A级游戏 3.范围:电脑游戏,手机游戏, ...

  10. 2019年Unity学习资源指南[精心整理]

    前言 进入一个领域,最直接有效的方法就是,寻找相关综述性文章,首先你需要对你入门的领域有个概括性的了解,这些包括: 1.主流的学习社区与网站. 2.该领域的知名大牛与热心分享的从业者. 3.如何有效的 ...

随机推荐

  1. mvc项目架构搭建之UI层的搭建

    项目架构搭建之UI层的搭建 Contents 系列一[架构概览] 0.项目简介 1.项目解决方案分层方案 2.所用到的技术 3.项目引用关系 系列二[架构搭建初步] 4.项目架构各部分解析 5.项目创 ...

  2. .NET破解之太乐地图下载器【非暴破】

    不知不觉,接触破解逆向已经三个月了,从当初的门外汉到现在的小白,这个过程只有经历过才知道其中的苦与乐: 有无知.困惑.痛苦.惊喜.彻悟.欣慰…… 有无助的软件脱壳,茫然的代码分析,有无趣的反复测试, ...

  3. [leetcode] Contains Duplicate

    Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...

  4. C语言可变参数函数实现原理

    一.可变参数函数实现原理 C函数调用的栈结构: 可变参数函数的实现与函数调用的栈结构密切相关,正常情况下C的函数参数入栈规则为__stdcall, 它是从右到左的,即函数中的最右边的参数最先入栈. 本 ...

  5. Android软键盘与输入框的设置

    大家开发Android或者用app的时候会发现转到输入框就会自动弹出软键盘,切换别的页面就会自动的隐藏,下面几行代码用的熟练了就行了: 1.方法一(如果输入法在窗口上已经显示,则隐藏,反之则显示) I ...

  6. JAVA基础学习day14--集合一

    一.集合的出现 1.1.集合简述 面向对象语言对事物的体现都是以对象形式,为了方便对多个对象的操作,就对象对象进行存储,集合就是存仪储对象最常用的一种试 1.2.数组和集合都是容器 数组也存对象,存储 ...

  7. 【转】Xcode托管代码到oschina中的教程

    本文的以下内容较旧,常常会出现第一次配置成功可以用,但关机重启后就一直提示“username or password”不正确的bug,所以建议弃ssh方式转向https方式,更加稳定.传送门:点我. ...

  8. iOS 验证邮箱手机号格式

    做登录界面时,用户在UITextfield中输入输入邮箱账号后,我们应该在本地验证格式是否正确,再将参数传给服务器验证. 最简单的就是利用系统的NSPredicate //利用正则表达式验证 -(BO ...

  9. iOS 中的 NSTimer

    iOS 中的 NSTimer NSTimer fire 我们先用 NSTimer 来做个简单的计时器,每隔5秒钟在控制台输出 Fire .比较想当然的做法是这样的: @interface Detail ...

  10. office2010安装报错

    有没有童鞋,在第一次安装office 2010的时候,中途不管是何原因导致中断或者未安装成功的 然后从第二次开始就一直安装报错??? 哈哈,我最近就遇到了 其他很简单,网上有很多方法,也有很多步骤,包 ...