我们这里有3种调试方法,Unity Remote,Android Studio和第三方模拟器
准备工作
(1)Android Studio:安装Google USB Driver
(2)手机安装Unity Remote
安装Unity Remote后,可以将手机与PC用USB线连接,在Unity界面运行,并在手机上显示画面,这样可以不用打包就能测试大部分功能,但是缺点是不能测试全部功能,有一些还是检测不到的。
调试
(1)使用Unity Remote调试
首先设置Unity中的Editor Settings,依次打开Edit > Project Settings > Editor
打开手机,确保开发人员选项打开,并且在USB调试模式下,打开Unity Remote,然后启动Unity项目,手机上就会显示
(2)使用AVD + [Logcat] 调试(Android Virtual Device)
打开Android Studio,选择Tools > AVD Manager
创建一个虚拟设备
这个根据需要选择型号
这一步注意,选择一个稳定的,存在的
创建好后如下
选择运行,就出现一个我们创建好的虚拟设备
最后,在Unity中Build And Run,以前是Bulid,这里会创建好一个apk,并且在创建的时候unity会搜索连接的Android设备,并将apk安装上去。这个时候我们的虚拟设备上就运行了这个包。
在虚拟设备运行的时候,可以在这里选择看到Android的打印显示
(3)使用真机 + [Logcat] 调试
和第2种方法不一样的事,这里不适用虚拟设备,直接使用我们的手机来调试。手机用数据线和电脑连接,和第2种方法一样的打包,但是这里会在我们的手机上面安装一个包运行。同样可以在Android Studio中看到Logcat
(4)使用模拟器调试
安装模拟器,将打出的包安装上去,运行就行了。
移动端相关API:
using UnityEngine;
using UnityEngine.UI;
public class Test : MonoBehaviour
{
public Text infoText;
string platform = string.Empty;
string info = string.Empty;
void Update()
{
info = string.Empty;
//使用预编译的宏命令检测平台
//Editor模式下目标平台选Android也会触发UNITY_ANDROID的宏
#if UNITY_ANDROID
platform = "UNITY_ANDROID";
#elif UNITY_EDITOR
platform = "UNITY_EDITOR";
#endif
Debug.Log("platform : " + platform);
info += "platform : " + platform + "
";
//使用Application.platform检测平台
Debug.Log("platform : " + Application.platform);
info += "platform : " + Application.platform + "
";
//获取当前设备分辨率
Debug.Log("currentResolution : " + Screen.currentResolution);
info += "currentResolution : " + Screen.currentResolution + "
";
//获取当前DPI
Debug.Log("dpi : " + Screen.dpi);
info += "dpi : " + Screen.dpi + "
";
//获取是否全屏
Debug.Log("fullScreen : " + Screen.fullScreen);
info += "fullScreen : " + Screen.fullScreen + "
";
//获取游戏窗口的宽高
Debug.Log("height : " + Screen.height);
info += "height : " + Screen.height + "
";
Debug.Log("width : " + Screen.width);
info += "width : " + Screen.width + "
";
//获取屏幕方向
Debug.Log("orientation : " + Screen.orientation);
info += "orientation : " + Screen.orientation + "
";
//获取屏幕超时时间(仅在移动端有效)
Debug.Log("sleepTimeout : " + Screen.sleepTimeout);
info += "sleepTimeout : " + Screen.sleepTimeout + "
";
//任意键按下
Debug.Log("anyKey : " + Input.anyKey);
info += "anyKey : " + Input.anyKey + "
";
//获取最后一次加速度计报告的加速度
Debug.Log("acceleration : " + Input.acceleration);
info += "acceleration : " + Input.acceleration + "
";
//获取本帧加速度计报告的次数
Debug.Log("accelerationEventCount : " + Input.accelerationEventCount);
info += "accelerationEventCount : " + Input.accelerationEventCount + "
";
//获取本帧加速度计的所有报告信息
for (int i = 0; i < Input.accelerationEvents.Length; i++)
{
Debug.Log("accelerationEvents" + i + " : " + Input.accelerationEvents[i].acceleration + " , " + Input.accelerationEvents[i].deltaTime);
info += "accelerationEvents" + i + " : " + Input.accelerationEvents[i].acceleration + " , " + Input.accelerationEvents[i].deltaTime + "
";
}
//获取电子罗盘向量
Debug.Log("compass : " + Input.compass.rawVector);
info += "compass : " + Input.compass.rawVector + "
";
//获取设备方向
Debug.Log("deviceOrientation : " + Input.deviceOrientation);
info += "deviceOrientation : " + Input.deviceOrientation + "
";
//获取陀螺仪重力
Debug.Log("gyro : " + Input.gyro.gravity);
info += "gyro : " + Input.gyro.gravity + "
";
//获取位置服务状态
Debug.Log("location : " + Input.location.status);
info += "location : " + Input.location.status + "
";
//获取设备是否带有鼠标
Debug.Log("mousePresent : " + Input.mousePresent);
info += "mousePresent : " + Input.mousePresent + "
";
//获取平台是否支持多点触控
Debug.Log("multiTouchEnabled : " + Input.multiTouchEnabled);
info += "multiTouchEnabled : " + Input.multiTouchEnabled + "
";
//获取是否支持笔触(可检测压感、触摸夹角等)
Debug.Log("stylusTouchSupported : " + Input.stylusTouchSupported);
info += "stylusTouchSupported : " + Input.stylusTouchSupported + "
";
//获取是否支持压感
Debug.Log("touchPressureSupported : " + Input.touchPressureSupported);
info += "touchPressureSupported : " + Input.touchPressureSupported + "
";
//获取是否支持触摸
Debug.Log("touchSupported : " + Input.touchSupported);
info += "touchSupported : " + Input.touchSupported + "
";
if (Input.touchCount > 0)
{
//当前帧的触摸数
Debug.Log("touchCount : " + Input.touchCount);
info += "touchCount : " + Input.touchCount + "
";
for (int i = 0; i < Input.touchCount; i++)
{
//获取当前Touch对象的一系列属性
Debug.Log("touch" + i + "altitudeAngle : " + Input.GetTouch(i).altitudeAngle);
info += "touch" + i + "altitudeAngle : " + Input.GetTouch(i).altitudeAngle + "
";
Debug.Log("touch" + i + "azimuthAngle : " + Input.GetTouch(i).azimuthAngle);
info += "touch" + i + "azimuthAngle : " + Input.GetTouch(i).azimuthAngle + "
";
Debug.Log("touch" + i + "deltaPosition : " + Input.GetTouch(i).deltaPosition);
info += "touch" + i + "deltaPosition : " + Input.GetTouch(i).deltaPosition + "
";
Debug.Log("touch" + i + "deltaTime : " + Input.GetTouch(i).deltaTime);
info += "touch" + i + "deltaTime : " + Input.GetTouch(i).deltaTime + "
";
Debug.Log("touch" + i + "fingerId : " + Input.GetTouch(i).fingerId);
info += "touch" + i + "fingerId : " + Input.GetTouch(i).fingerId + "
";
Debug.Log("touch" + i + "maximumPossiblePressure : " + Input.GetTouch(i).maximumPossiblePressure);
info += "touch" + i + "maximumPossiblePressure : " + Input.GetTouch(i).maximumPossiblePressure + "
";
Debug.Log("touch" + i + "phase : " + Input.GetTouch(i).phase);
info += "touch" + i + "phase : " + Input.GetTouch(i).phase + "
";
Debug.Log("touch" + i + "position : " + Input.GetTouch(i).position);
info += "touch" + i + "position : " + Input.GetTouch(i).position + "
";
Debug.Log("touch" + i + "pressure : " + Input.GetTouch(i).pressure);
info += "touch" + i + "pressure : " + Input.GetTouch(i).pressure + "
";
Debug.Log("touch" + i + "radius : " + Input.GetTouch(i).radius);
info += "touch" + i + "radius : " + Input.GetTouch(i).radius + "
";
Debug.Log("touch" + i + "radiusVariance : " + Input.GetTouch(i).radiusVariance);
info += "touch" + i + "radiusVariance : " + Input.GetTouch(i).radiusVariance + "
";
Debug.Log("touch" + i + "rawPosition : " + Input.GetTouch(i).rawPosition);
info += "touch" + i + "rawPosition : " + Input.GetTouch(i).rawPosition + "
";
Debug.Log("touch" + i + "tapCount : " + Input.GetTouch(i).tapCount);
info += "touch" + i + "tapCount : " + Input.GetTouch(i).tapCount + "
";
Debug.Log("touch" + i + "type : " + Input.GetTouch(i).type);
info += "touch" + i + "type : " + Input.GetTouch(i).type + "
";
}
}
infoText.text = info;
}
}