Android下调试Unity3D应用


目前貌似不支持断点调试,但可以通过日志打印(logcat)来跟踪。

在Android SDK中有个adb工具,使用此工具来跟踪运行的android应用:

  1. adb logcat  

启动logcat,并将设备上运行的android应用的运行时信息全部打印出来。

  1. adb logcat -s Unity  

如果只想打印Unity的输出信息,使用此命令。

  1. adb logcat -d > logcat.txt  

将打印信息输出为文件。 

当然,更直接的做法是在应用中集成自己的调试信息窗口,将如下代码关联到一个gameobject:

  1. using UnityEngine;  
  2. using System.Collections;</p><p>public class GuiTextDebug : MonoBehaviour   
  3. {  
  4.  private float windowPosition = -440.0f;  
  5.  private int positionCheck = 2;  
  6.  private static string windowText = "";  
  7.  private Vector2 scrollViewVector = Vector2.zero;  
  8.  private GUIStyle debugBoxStyle;  
  9.    
  10.  private float leftSide = 0.0f;  
  11.  private float debugWidth = 420.0f;  
  12.    
  13.  public bool debugIsOn = false;  
  14.    
  15.  public static void debug(string newString)  
  16.  {  
  17.   windowText = newString + "\n" + windowText;  
  18.   UnityEngine.Debug.Log(newString);  
  19.  }  
  20.     
  21.  void Start()   
  22.     {  
  23.   debugBoxStyle = new GUIStyle();  
  24.   debugBoxStyle.alignment = TextAnchor.UpperLeft;  
  25.   leftSide = 120;  
  26.  }  
  27.     
  28.    
  29.  void OnGUI()   
  30.     {  
  31.   if (debugIsOn)   
  32.         {  
  33.    GUI.depth = 0;    
  34.    GUI.BeginGroup(new Rect(windowPosition, 40.0f, leftSide, 200.0f));  
  35.      
  36.    scrollViewVector = GUI.BeginScrollView(new Rect (0, 0.0f, debugWidth, 200.0f),   
  37.                                                    scrollViewVector,   
  38.                                                    new Rect (0.0f, 0.0f, 400.0f, 2000.0f));  
  39.    GUI.Box(new Rect(0, 0.0f, debugWidth - 20.0f, 2000.0f), windowText, debugBoxStyle);  
  40.    GUI.EndScrollView();  
  41.      
  42.    GUI.EndGroup ();  
  43.      
  44.    if (GUI.Button(new Rect(leftSide, 0.0f,75.0f,40.0f), "调试"))  
  45.             {  
  46.     if (positionCheck == 1)  
  47.                 {  
  48.      windowPosition = -440.0f;  
  49.      positionCheck = 2;  
  50.     }  
  51.     else   
  52.                 {  
  53.      windowPosition = leftSide;  
  54.      positionCheck = 1;  
  55.     }  
  56.    }  
  57.      
  58.    if (GUI.Button(new Rect(leftSide + 80f,0.0f,75.0f,40.0f),"清除"))  
  59.             {  
  60.     windowText = "";  
  61.    }  
  62.   }  
  63.  }  
  64. }  

相关内容