脚本功能

在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. android: DOC命令:查看后台运行的activity:

    DOC命令:查看后台运行的activity: adb shell dumpsys activity running activity: 模拟器曾经运行过的 activity:

  2. XML的文档声明

    1.XML的文档声明 <?xml version="1.0" encoding="utf-8"?> 文档声明必须写在第一行第一列 属性: versi ...

  3. javascript-this,call,apply,bind简述2

    上节我们一起研究了this这个小兄弟,得出一个结论,this指向调用this所在函数(或作用域)的那个对象或作用域.不太理解的朋友可以看看上节的内容,这次我们主要探讨一下call(),apply(), ...

  4. DevExpress中透明玻璃效果

    Aero玻璃效果 下图左是DevExpress无玻璃效果,图右是Windows自带玻璃效果. Windows Aero 是从 Windows Vista 开始使用的新型用户界面,透明玻璃感让用户一眼贯 ...

  5. R语言学习笔记:向量

    向量是R语言最基本的数据类型. 单个数值(标量)其实没有单独的数据类型,它只不过是只有一个元素的向量. x <- c(1, 2, 4, 9) x <- c(x[1:3], 88, x[4] ...

  6. android内存泄露调试,Heap,MAT

    三.内存监测工具 DDMS --> Heap 无论怎么小心,想完全避免bad code是不可能的,此时就需要一些工具来帮助我们检查代码中是否存在会造成内存泄漏的地方.Android tools中 ...

  7. 实战1--应用EL表达式访问JavaBean的属性

    (1)编写index.jsp页面,用来收集用户的注册信息 <%@ page language="java" pageEncoding="GBK"%> ...

  8. C语言指针传递详解

    传递指针可以让多个函数访问指针所引用的对象,而不用把对象声明为全局可访问,要在某个函数中修改数据,需要用指针传递数据,当数据是需要修改的指针的时候,就要传递指针的指针,传递参数(包括指针)的时候,传递 ...

  9. 【读书笔记】iOS-NSPredicate

    一,Cocoa提供了一个名为NSPredicate的类,它用于指定过滤器的条件.可以创建NSPredicate对象,通过该对象准确地描述所需的条件,对每个对象通过谓词进行筛选,判断它们是否与条件相匹配 ...

  10. iOS--APP 登录界面图(xuer)

    ViewController.h #import "ViewController.h" @interface ViewController () @property(strong, ...