图形化调试可以加速开发。
例如在战斗中,可能需要知道所有单位的仇恨值,如果这些信息全打log的话,很难有直观感受,
而如果在Scene窗口里,单位头顶有一个球,越红表示仇恨越高,越暗表示仇恨越低,那么调试起来比打log直观多了。
【一 图形化调试】
Unity中图形化调试主要4种
Debug.Draw
Gizmos.Draw
Graphic.DrawMesh
GL
只需在Scene窗口显示的调试图像
需要在实际设备屏幕显示的调试图像
Graphic.DrawMesh和Debug.Draw
Graphic.DrawMesh和GL
四种方式比较
(1)Debug.Draw
=1=一般在Update/Fixed Update/LateUpdate里调用
=2=只在Scene窗口里显示
=3=并且不能设置材质
(2)Gizmos.Draw
=1=在OnDrawGizmos /OnDrawGizmosSelected里调用
=2=只在Scene窗口里显示
=3=并且不能设置材质
(3)Graphic.DrawMesh
=1=一般在Update/Fixed Update/LateUpdate里调用
=2=实际屏幕和Scene窗口都能显示
=3=可以设置材质
画Mesh Ok
(4)GL,
=1=一般在物体的OnRenderObject 或者相机的OnPostRender里调用
=2=实际屏幕和Scene窗口都能显示
=3=可以设置材质
一个GL.Begin/GL.End里的渲染是自动合并的,一般是一个Drawcall
画一些线,三角可以。用 GL.TRIANGLES 显示整个Mesh的话会超卡。例:渲染线框
【二 GL】
GL除了可以用来调试,可以拿来做功能,例如LineRenderer,地格等。
GL即Graphics Library。Low-Level Graphics Library。计算matrices,发出类似OpenGL的immediate模式的渲染指令,和其他低级图像任务。Graphic.DrawMesh()比GL更高效。
GL立即绘制函数只用当前material的设置。因此除非你显示指定mat,否则mat可以是任何材质。并且GL可能会改变材质。
GL是立即执行的,如果你在Update()里调用,它们将在相机渲染前执行,相机渲染将会清空屏幕,GL效果将无法看到。
在camera上贴脚本,并在OnPostRender()里执行。
也可以挂在任何GameObject上,在OnRenderObject()里执行。
或者挂在物体上
- using UnityEngine;
- using System.Collections;
-
- public class GLTest : MonoBehaviour {
-
-
public Material mat; -
void OnPostRender() { -
if (!mat) { -
Debug.LogError("Please Assign a material on the inspector"); -
return; -
} -
GL.PushMatrix(); //保存当前Matirx -
mat.SetPass(0); //刷新当前材质 -
GL.LoadPixelMatrix();//设置pixelMatrix -
GL.Color(Color.yellow); -
GL.Begin(GL.LINES); -
GL.Vertex3(0, 0, 0); -
GL.Vertex3(Screen.width, Screen.height, 0); -
GL.End(); -
GL.PopMatrix();//读取之前的Matrix -
} - }
- using System.IO;
- using UnityEngine;
- using System.Collections;
-
- public class ScreenShot : MonoBehaviour {
-
void Start() { -
StartCoroutine(UploadPNG() ); -
} -
IEnumerator UploadPNG() { -
yield return new WaitForEndOfFrame(); - print ("yuuuuu");
-
int width = Screen.width; -
int height = Screen.height; -
Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false); -
tex.ReadPixels(new Rect(0, 0, width, height), 0, 0); -
tex.Apply(); -
byte[] bytes = tex.EncodeToPNG(); - File.WriteAllBytes(Application.dataPath+"/ss.png",bytes);
- UnityEditor.AssetDatabase.Refresh();
-
} - }
- using UnityEngine;
- using System.Collections;
-
- public class GLTest : MonoBehaviour {
- public Shader shader;
- public Texture2D t2d;
-
private Material mat; - void Start()
- {
- mat = new Material(shader);
- mat.mainTexture = t2d;
- }
-
void OnPostRender() { -
if (!mat) { -
Debug.LogError("Please Assign a material on the inspector"); -
return; -
} -
GL.PushMatrix(); -
mat.SetPass(0); -
GL.LoadOrtho(); -
GL.Begin(GL.QUADS); -
GL.Vertex3(0, 0, 0.1F); -
GL.Vertex3(1f, 0, 0.1F); -
GL.Vertex3(1f, 1, 0.1F); -
GL.Vertex3(0, 1, 0.1F); -
GL.End(); -
GL.PopMatrix(); -
} - }
- Shader "Custom/GLDrawLine" {
- Properties {
- _MainTex ("Base (RGB)", 2D) = "white" {}
- }
- SubShader {
-
Pass { - Cull off
- Blend DstAlpha zero
- Color(1,1,1,1)
-
} - }
- }