Unity图形化调试

编程入门 行业动态 更新时间:2024-10-09 23:13:52

Unity<a href=https://www.elefans.com/category/jswz/34/1760992.html style=图形化调试"/>

Unity图形化调试

原文链接:.html


                        图形化调试 

【前言】

图形化调试可以加速开发。

例如在战斗中,可能需要知道所有单位的仇恨值,如果这些信息全打log的话,很难有直观感受,

而如果在Scene窗口里,单位头顶有一个球,越红表示仇恨越高,越暗表示仇恨越低,那么调试起来比打log直观多了。

 

【一 图形化调试】

Unity中图形化调试主要4种

Debug.Draw

Gizmos.Draw

Graphic.DrawMesh

GL

只需在Scene窗口显示的调试图像

     一直显示的 OnDrawGizmos + Gizmos.Draw

     选中显示的 OnDrawGizmosSelected + Gizmos.Draw

     脚本控制的 Update + Debug.Draw

需要在实际设备屏幕显示的调试图像

    Update+Graphic.DrawMesh

    OnRenderObject+GL

 

Graphic.DrawMesh和Debug.Draw   调用一致,都是在Update系里

Graphic.DrawMesh和GL       显示类似,都在各个窗口显示,并且可以设置材质。

四种方式比较

(1)Debug.Draw

=1=一般在Update/Fixed Update/LateUpdate里调用

=2=只在Scene窗口里显示

=3=并且不能设置材质

     void Update()
    {

        Debug.DrawLine (worldPos1, worldPos2,Color.yellow);

    }

 

(2)Gizmos.Draw

=1=在OnDrawGizmos /OnDrawGizmosSelected里调用

=2=只在Scene窗口里显示

=3=并且不能设置材质

    public void OnDrawGizmosSelected() {

        Gizmos.DrawLine(Vector3.zero, new Vector3(0,3f,0));

    }

(3)Graphic.DrawMesh

=1=一般在Update/Fixed Update/LateUpdate里调用

=2=实际屏幕和Scene窗口都能显示

=3=可以设置材质

画Mesh Ok

         void Update()
        {

            Graphics.DrawMesh(mesh, worldPos, worldRotation, material, 0);

        }

(4)GL,

=1=一般在物体的OnRenderObject 或者相机的OnPostRender里调用

=2=实际屏幕和Scene窗口都能显示

=3=可以设置材质

一个GL.Begin/GL.End里的渲染是自动合并的,一般是一个Drawcall

画一些线,三角可以。用 GL.TRIANGLES 显示整个Mesh的话会超卡。

例:渲染线框

      void OnRenderObject()
    {

        mat.SetPass(0);

         GL.wireframe true;

         GL.Color (new Color (1,1, 0, 0.8F));
        GL.PushMatrix();

        GL.Begin(GL.TRIANGLES);

        for(int i=0;i<</span>mesh.triangles.Length-2;i+=3)

        {

            GL.Vertex(mesh.vertices[mesh.triangles[i]]);

            GL.Vertex(mesh.vertices[mesh.triangles[i+1]]);

            GL.Vertex(mesh.vertices[mesh.triangles[i+2]]);

        }

        GL.End();

        GL.PopMatrix();

              GL.wireframe false;
    }

 

 

【二 GL】

GL除了可以用来调试,可以拿来做功能,例如LineRenderer,地格等。

 

GL即Graphics Library。Low-Level Graphics Library。计算matrices,发出类似OpenGL的immediate模式的渲染指令,和其他低级图像任务。Graphic.DrawMesh()比GL更高效。

GL立即绘制函数只用当前material的设置。因此除非你显示指定mat,否则mat可以是任何材质。并且GL可能会改变材质。

GL是立即执行的,如果你在Update()里调用,它们将在相机渲染前执行,相机渲染将会清空屏幕,GL效果将无法看到。

通常GL用法是
在camera上贴脚本,并在OnPostRender()里执行。
也可以挂在任何GameObject上,在OnRenderObject()里执行。
或者挂在物体上
注意:  1.GL的线等基本图元并没有uv. 所有是没有贴图纹理影射的,shader里仅仅做的是单色计算或者对之前的影像加以处理。 2.GL所使用的shader里必须有Cull off指令,否则显示会变成如下
3. 如果是线,颜色是GL .Color ( new Color ( 1 , 1 , 1 , 0.5f ) );设置的颜色  如果是GL.TRIANGLES或者是 GL.QUADS,则颜色是shader里的颜色。
1. GL.PushMatrix() 保存matrices至matrix stack上。 GL.PopMatrix() 从matrix stack上读取matrices。
2. GL.LoadPixelMatrix() 改变MVP矩阵,使得transform里的xy 直接对应像素,(0,0)表示屏幕viewport的左下角,z的范围是(-1,1),该函数改变camera的参数,所以需要GL.PushMatrix()保存和GL.PopMatrix()读取。 GL.Vertex3()的取值范围从左下角的(0,0,0) 至右上角的(Screen.width,Screen.height,0)
GL.LoadOrtho() 设置ortho perspective,即水平视角。After calling LoadOrtho, the viewing frustum goes from (0,0,-1) to (1,1,100). 主要用于在纯2D里绘制图元。 GL .Vertex3()的取值范围从左下角的(0,0,0) 至右上角的(1,1,0)
3. OnPostRender() 只有物体上有激活的摄像机时,才会调用的函数,当摄像机完成渲染场景,绘制了所有物体以后调用。 OnPostRender可以变成co-routine,加yield语句即可。
WaitForEndOfFrame() 等待至 所有绘制之后,end of frame, 就在展示frame到屏幕之前。可以做截图。可以在任何物体上使用该函数。
例1:屏幕画线

 

  1. using UnityEngine;
  2. using System.Collections;

  3. public class GLTest : MonoBehaviour {

  4.   public Material mat;
  5.     void OnPostRender() {
  6.         if (!mat) {
  7.             Debug.LogError("Please Assign a material on the inspector");
  8.             return;
  9.         }
  10.         GL.PushMatrix(); //保存当前Matirx
  11.         mat.SetPass(0); //刷新当前材质
  12.         GL.LoadPixelMatrix();//设置pixelMatrix
  13.         GL.Color(Color.yellow);
  14.         GL.Begin(GL.LINES);
  15.         GL.Vertex3(0, 0, 0);
  16.         GL.Vertex3(Screen.width, Screen.height, 0);
  17.         GL.End();
  18.         GL.PopMatrix();//读取之前的Matrix
  19.     }
  20. }


例2:截图

 

  1. using System.IO;
  2. using UnityEngine;
  3. using System.Collections;

  4. public class ScreenShot : MonoBehaviour {
  5.     void Start() {
  6.         StartCoroutine(UploadPNG() );
  7.     }
  8.     IEnumerator UploadPNG() {
  9.         yield return new WaitForEndOfFrame();
  10. print ("yuuuuu");
  11.         int width = Screen.width;
  12.         int height = Screen.height;
  13.         Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);
  14.         tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
  15.         tex.Apply();
  16.         byte[] bytes = tex.EncodeToPNG();
  17. File.WriteAllBytes(Application.dataPath+"/ss.png",bytes);
  18. UnityEditor.AssetDatabase.Refresh();
  19.     }
  20. }
例3:展示Alpha

 

  1. using UnityEngine;
  2. using System.Collections;

  3. public class GLTest : MonoBehaviour {
  4. public Shader shader;
  5. public Texture2D t2d;
  6.   private Material mat;
  7. void Start()
  8. {
  9. mat = new Material(shader);
  10. mat.mainTexture = t2d;
  11. }
  12.     void OnPostRender() {
  13.         if (!mat) {
  14.             Debug.LogError("Please Assign a material on the inspector");
  15.             return;
  16.         }
  17.         GL.PushMatrix();
  18.         mat.SetPass(0);
  19.         GL.LoadOrtho();
  20.         GL.Begin(GL.QUADS);
  21.         GL.Vertex3(0, 0, 0.1F);
  22.         GL.Vertex3(1f, 0, 0.1F);
  23.         GL.Vertex3(1f, 1, 0.1F);
  24.         GL.Vertex3(0, 1, 0.1F);
  25.         GL.End();
  26.         GL.PopMatrix();
  27.     }
  28. }
  1. Shader "Custom/GLDrawLine" {
  2. Properties {
  3. _MainTex ("Base (RGB)", 2D) = "white" {}
  4. }
  5. SubShader {
  6.     Pass {
  7. Cull off
  8. Blend DstAlpha zero
  9. Color(1,1,1,1)
  10.     }
  11. }
  12. }

 

 

分享:

打开微信“扫一扫”

打开网页后点击屏幕

右上角“分享”按钮

15

1

        </div><div class="clearit"></div></div><div class="articalInfo"><!-- 分享到微博 {$t_blog} --><div>阅读<span id="r_471132920101gxzf" class="SG_txtb">(10359)</span><em class="SG_txtb">┊</em> <a href="#commonComment">评论</a> <span id="c_471132920101gxzf" class="SG_txtb">(3)</span><em class="SG_txtb">┊</em>				<a href="javascript:;" onclick="$articleManage('471132920101gxzf',5);return false;">收藏</a><span id="f_471132920101gxzf" class="SG_txtb">(1)</span><em class="SG_txtb">┊</em><a href="#" id="quote_set_sign" onclick="return false ;">转载</a><a href="#" id="z_471132920101gxzf" onclick="return false ;" class="zznum">(9)</a>				<span id="fn_【风宇冲】图形化调试" class="SG_txtb"></span><em class="SG_txtb">┊</em><a onclick="return false;" href="javascript:;"><cite id="d1_digg_471132920101gxzf">喜欢</cite></a><a id="d1_digg_down_471132920101gxzf" href="javascript:;"><b>▼</b></a><em class="SG_txtb">┊</em><a href=".html?blog_id=blog_471132920101gxzf" target="_blank">打印</a><em class="SG_txtb">┊</em><a id="q_471132920101gxzf" onclick="report('471132920101gxzf');return false;" href="#">举报/Report</a></div><div class="IR"><table><tbody><tr><!--<th class="SG_txtb" scope="row">已投稿到:</th><td><div class="IR_list"><span><img class="SG_icon SG_icon36" src=".gif" width="15" height="15" title="排行榜" align="absmiddle" /> <a href=".html" class="SG_linkb" target="_blank">排行榜</a></span>							</div></td>--></tr></tbody></table></div></div><div class="clearit"></div><div class="blogzz_zzlist borderc" id="blog_quote" style="display:none"><h3><a href="#" onclick="return false" title="关闭" id="ql_close471132920101gxzf" class="blogzz_closepic SG_floatR"></a>转载列表:</h3>                <ul class="ul_zzlist" id="ql_content471132920101gxzf">                </ul>				<ul style="display:none"><li id="ql_tip471132920101gxzf"></li></ul>                <div class="SG_clearB"></div>                <div class="blogzz_btn">					<a id="btnArticleQuote471132920101gxzf" href="#" onclick="scope.article_quote &amp;&amp; scope.article_quote.check('471132920101gxzf');return false;" class="SG_aBtn SG_aBtn_ico SG_turn"><cite><img class="SG_icon SG_icon111" id="quoteList_quote471132920101gxzf" src=".gif" width="15" height="15" align="absmiddle">转载</cite></a>					<p id="quoteDescription471132920101gxzf" class="SG_turntxt" style="display: none;">转载是分享博文的一种常用方式...</p>				</div>				<div id="ql_page471132920101gxzf" class="blogzz_paged"></div>				<div class="clearit"></div></div><div class="articalfrontback SG_j_linedot1 clearfix" id="new_nextprev_471132920101gxzf"><div><span class="SG_txtb">前一篇:</span><a href=".html">【风宇冲】ImageEffects_Twirl</a></div><div><span class="SG_txtb">后一篇:</span><a href=".html">Mono做DLL</a></div></div><div class="clearit"></div><div id="loginFollow"></div><div class="allComm"><div class="allCommTit"><div class="SG_floatL"><strong>评论</strong><span id="commAd_1" style="display: inline-block;"><span style="margin-left:15px; width:220px; display:inline-block;"><a target="_blank" href=".html">重要提示:警惕虚假中奖信息</a></span></span></div><div class="SG_floatR"><a class="CP_a_fuc" href="#post">[<cite>发评论</cite>]</a></div></div><ul id="article_comment_list" class="SG_cmp_revert"><!-- 循环始 --><li>评论加载中,请稍候...</li><!-- 循环终  --></ul><div class="clearit"></div><div class="myCommPages SG_j_linedot1"><div class="SG_page" id="commentPaging" style="display:none;"><ul class="SG_pages"></ul></div><div class="clearit"></div></div><a name="post"></a><div class="writeComm"><div class="allCommTit"><div class="SG_floatL"><strong>发评论</strong><span></span></div><div class="SG_floatR"></div></div><div class="wrCommTit"><div class="SG_floatL" id="commentNick" style="display:none;"></div></div><div class="formTextarea"><div style="float:left;" id="commonComment"><iframe id="postCommentIframe" frameborder="0" style="border:1px solid #C7C7C7;height:158px;width:448px;maring-top:1px;background-color:white;" src=".html"></iframe><textarea id="commentArea" tabindex="1" style="display:none;"></textarea></div><div id="mobileComment" style="float:left;display:none;"><textarea id="mbCommentTa" style="width:438px;height:150px;border:1px solid #C7C7C7;line-height:18px;padding:5px;"></textarea></div><div class="faceblk" id="faceWrap"><div id="smilesSortShow" class="faceline1"><div class="facestyle" id="recomm_1574682587069"><a href="#" key="302"><img src=".gif" alt="小新小浪" title="小新小浪"></a><a href="#" key="308"><img src=".gif" alt="炮炮兵" title="炮炮兵"></a><a href="#" key="315"><img src=".gif" alt="张富贵" title="张富贵"></a><a href="#" key="316"><img src=".gif" alt="旺狗" title="旺狗"></a><a href="#" key="331"><img src=".gif" alt="悠嘻猴" title="悠嘻猴"></a><a href="#" key="351"><img src=".gif" alt="酷巴熊" title="酷巴熊"></a></div><span class="SG_more"><a href="#">更多&gt;&gt;</a></span><div class="clearit"></div></div><ul id="smilesRecommended" class="faceline01"><li><a href="#"><img src=".gif" alt="就不买你" title="就不买你" height="50" width="50"></a></li><li><a href="#"><img src=".gif" alt="股市" title="股市" height="50" width="50"></a></li><li><a href="#"><img src=".gif" alt="发霉" title="发霉" height="50" width="50"></a></li><li><a href="#"><img src=".gif" alt="陈水边" title="陈水边" height="50" width="50"></a></li><li><a href="#"><img src=".gif" alt="裁员" title="裁员" height="50" width="50"></a></li><li><a href="#"><img src=".gif" alt="音乐" title="音乐" height="50" width="50"></a></li><li><a href="#"><img src=".gif" alt="贴你" title="贴你" height="50" width="50"></a></li><li><a href="#"><img src=".gif" alt="抢车位" title="抢车位" height="50" width="50"></a></li></ul></div><div class="clearit"></div></div><div class="formLogin"><div class="SG_floatL"> <p id="commentlogin" style="display: block;"><span>登录名:</span><input type="text" style="width: 115px;" id="login_name" tabindex="2">   <span>密码:</span><input type="password" style="width: 115px;" id="login_pass" tabindex="3">   <a href=".html" target="_blank">找回密码</a>   <a href=".php?entry=blog&amp;src=blogicp&amp;srcuid=1192309394" target="_blank">注册</a>	<input type="checkbox" id="login_remember"><label for="login_remember" style="display:inline-block;" title="建议在网吧/公用电脑上取消该选项">记住登录状态</label></p><p id="commentloginM" style="display:none;"><span>昵&nbsp;&nbsp;&nbsp;称:</span><input type="text" style="width: 115px;" id="comment_anonyous" value="新浪网友" tabindex="2" disabled=""></p><p id="quote_comment_p"><!--<input type="checkbox" id="bb"> <label for="bb"><img height="18" align="absmiddle" width="18" title="" src=".gif" class="SG_icon SG_icon110">分享到微博 <img height="15" align="absmiddle" width="15" title="新" src=".gif" class="SG_icon SG_icon11"></label>&nbsp;&nbsp;&nbsp;--><input type="checkbox" id="cbCommentQuote"><label for="cbCommentQuote">评论并转载此博文</label><img class="SG_icon SG_icon11" src=".gif" width="15" height="15" title="新" align="absmiddle"></p><p id="geetest-box"></p></div><span style="display: none; color: rgb(153, 153, 153); margin-left: 10px;" id="login_remember_caution"></span><!--<div class="SG_floatR" id="anonymity_cont"><input type="checkbox" id="anonymity"/><label for="anonymity">匿名评论</label></div>--></div><div class="formBtn"><a href="javascript:;" onclick="return false;" class="SG_aBtn" tabindex="5"><cite id="postcommentid">发评论</cite></a><p class="SG_txtc">以上网友发言只代表其个人观点,不代表新浪网的观点或立场。</p></div></div></div><div class="clearit"></div><div class="articalfrontback articalfrontback2 clearfix"><div class="SG_floatL"><span class="SG_txtb">&lt;&nbsp;前一篇</span><a href=".html">【风宇冲】ImageEffects_Twirl</a></div><div class="SG_floatR"><span class="SG_txtb">后一篇&nbsp;&gt;</span><a href=".html">Mono做DLL</a></div></div><div class="clearit"></div></div>
<!--博文正文 end --><script type="text/javascript">var voteid="";</script></div>       <div class="SG_connFoot"></div></div>
<!--第三列start-->
<div id="column_3" class="SG_colWnone"><div style="width:0px;height:0.1px;margin:0px;">&nbsp;&nbsp;</div></div>
<!--第三列end--></div>
<div id="diggerFla" style="position:absolute;left:0px;top:0px;width:0px"><embed pluginspage="" type="application/x-shockwave-flash" src=".swf" width="1" height="1" style="undefined" id="digger" name="digger" bgcolor="#00ff00" quality="high" wmode="transparent" allowscriptaccess="always"></div>
<div class="sinablogfooter" id="sinablogfooter" style="position:relative;"><p class="SG_linka"><a href="/" target="_blank">新浪BLOG意见反馈留言板</a> 电话:4000520066 提示音后按1键(按当地市话标准计费) 欢迎批评指正</p><p class="SG_linka"><a href="/" target="_blank">新浪简介</a> | <a href="/" target="_blank">About Sina</a> | <a href="/" target="_blank">广告服务</a> | <a href=".html" target="_blank">联系我们</a> | <a href=".html" target="_blank">招聘信息</a> | <a href=".shtml" target="_blank">网站律师</a> | <a href="" target="_blank">SINA English</a> | <a href="/" target="_blank">会员注册</a> | <a href="/" target="_blank">产品答疑</a> </p><p class="copyright SG_linka"> Copyright © 1996 - 2018 SINA Corporation,  All Rights Reserved</p><p class="SG_linka"> 新浪公司 <a href=".shtml" target="_blank">版权所有</a></p><a href=".jsp" target="_blank" class="gab_link"></a>
</div>

更多推荐

Unity图形化调试

本文发布于:2024-03-06 16:00:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1715746.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图形化   Unity

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!