【前言】
圖形化調試可以加速開發。
例如在戰鬥中,可能需要知道所有單位的仇恨值,如果這些信息全打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效果將無法看到。
在camera上貼腳本,並在OnPostRender()裏執行。
也可以掛在任何GameObject上,在OnRenderObject()裏執行。
或者掛在物體上
- 例1:屏幕畫線
- 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
- }
- }
- 例2:截圖
- 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();
- }
- }
- 例3:展示Alpha
- 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)
- }
- }
- }