脚本功能

在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. [ javascript html Dom image 对象事件加载方式 ] 对象事件加载方式

    <!DOCTYPE html> <html lang='zh-cn'> <head> <title>Insert you title</title ...

  2. 某网SQL注入漏洞实战

      root@kali:~# sqlmap -u http://dn.you.com/shop.php?id=10 -v 1 --dbs   available databases [8]: [*] ...

  3. 解决SSIS中的脚本任务无法调试的问题

    开发环境:操作系统环境为Win7 64Bit,数据库为SQLServer2008R2 问题现象:在SSIS的项目工程中,新建Package包,新建脚本任务,编写脚本程序以后,设置断点无法调试(Debu ...

  4. C语言指针的长度和类型

    本文地址:http://www.cnblogs.com/archimedes/p/point-length-type.html,转载请注明源地址. 如果考虑应用程序的兼容性和可移植性,指针的长度就是一 ...

  5. iOS阅读器实践系列(一)coretext纯文本排版基础

    前言:之前做了公司阅读类的App,最近有时间来写一下阅读部分的实现过程,供梳理逻辑,计划会写一个系列希望能涉及到尽量多的方面与细节,欢迎大家交流.吐槽.拍砖,共同进步. 阅读的排版用的是coretex ...

  6. Android源码分析之HandlerThread

    HandlerThread是一种特殊的Thread,也就是有Looper的thread,既然有looper的话,那我们就可以用此looper来 创建一个Handler,从而实现和它的交互,比如你可以通 ...

  7. iOS学习笔记10-UIView动画

    上次学习了iOS学习笔记09-核心动画CoreAnimation,这次继续学习动画,上次使用的CoreAnimation很多人感觉使用起来很繁琐,有没有更加方便的动画效果实现呢?答案是有的,那就是UI ...

  8. python 格式化字符串的三种方法

    1)%格式化方法 >>> a = "this is %s %s" % ("my", "apple") >>&g ...

  9. CoreAnimation-08-CATransition

    概述 简介 CATransition又称转场动画,是CAAnimation的子类,可以直接使用 转场动画主要用于为图层提供移入/移出屏幕的动画效果 转场动画常见的应用是UINavigationCont ...

  10. C语言-01-基本语法

    一.学前需知 开发工具 windows平台:Visual C++6.0等 mac平台:Xcode6.0等 以下文章内容皆是以Xcode6.0为开发工具,clang编译器. Xcode的一些常用快捷键 ...