简体   繁体   English

Android 设备和 Unity 连接失败

[英]Android device and Unity connection failure

I'm trying to debug Unity game from PC with Windows 10 to Android phone:我正在尝试从装有 Windows 10 的 PC 到 Android 手机调试 Unity 游戏:

  1. In Unity from File > Build Settings I've Switch Platform to Android在 Unity 中从File > Build Settings我已经将Switch Platform到 Android
  2. In Android device I've downloaded and installed Unity Remote 5 application.在 Android 设备中,我已经下载并安装了 Unity Remote 5 应用程序。
  3. In phone Settings/About phone with 7 taps on Build number I get Developer Options and turned on USB debugging and tap on Revoke USB debugging authorization在手机Settings/About phone ,点击 7 次Build number我得到Developer Options并打开 USB 调试,然后点击撤销 USB 调试授权
  4. In Select USB Configuration I've checked PTP (Picture Transfer Protocol)在选择 USB 配置中,我检查了 PTP(图片传输协议)

I've checked 2 different USB cables with 3 different phones and each device is successfully detected and recognized by PC OS, Unity Remote says "Connect this device with a USB Cable to your computer. Press PLAY in the Unity Editor to test" , but Unity Editor by some reason does not shows any of my devices in Edit > Project Settings > Editor device:我已经用 3 部不同的手机检查了 2 条不同的 USB 电缆,并且每个设备都被 PC 操作系统成功检测和识别,Unity Remote 说“使用 USB 电缆将此设备连接到您的计算机。在 Unity 编辑器中按 PLAY 进行测试” ,但是由于某种原因,Unity Editor 没有在Edit > Project Settings > Editor device 中显示我的任何设备:

在此处输入图片说明

edit:编辑:

I've downloaded both Android Studio and separately sdk-tools-windows-4333796.exe from Command line tools only .只从命令行工具下载了Android Studio和单独的sdk-tools-windows-4333796.exe

First I've installed from Java SE Development Kit 8u241 jdk-8u241-windows-x64.exe for Windows x64首先,我从Java SE Development Kit 8u241 jdk-8u241-windows-x64.exe for Windows x64 安装

In Windows 10 OS Environment Variables/System Variables/Path , I've added C:\\Program Files\\Java\\jdk1.8.0_241\\bin and C:\\Program Files\\Java\\jre1.8.0_241\\bin在 Windows 10 OS Environment Variables/System Variables/Path ,我添加了C:\\Program Files\\Java\\jdk1.8.0_241\\binC:\\Program Files\\Java\\jre1.8.0_241\\bin

In User Variables I've added Variable name : JAVA_HOME and Variable value : C:\\Program Files\\Java\\jdk1.8.0_241\\bin在用户变量中,我添加了变量名称JAVA_HOME变量值C:\\Program Files\\Java\\jdk1.8.0_241\\bin

So, first I did not used sdk-tools-windows-4333796.exe and have installed Android Studio, here is what Edit > Preferences > External Tools shows:所以,首先我没有使用sdk-tools-windows-4333796.exe并安装了 Android Studio,这是Edit > Preferences > External Tools显示的内容:

在此处输入图片说明

Then, I've removed Android Studio and tried to use only sdk-tools-windows-4333796.exe , I've extracted tools folder to previously created folders path C:\\Android\\android-sdk , then from C:\\Android\\android-sdk\\tools\\bin load sdkmanager.bat with "platform;android-25" or "platform;android-26" argument, but error says:然后,我删除了 Android Studio 并尝试仅使用sdk-tools-windows-4333796.exe ,我已将tools文件夹提取到以前创建的文件夹路径C:\\Android\\android-sdk ,然后从C:\\Android\\android-sdk\\tools\\bin使用"platform;android-25""platform;android-26"参数加载sdkmanager.bat ,但错误显示:

Warning: File C:\\Users\\User.android\\repositories.cfg could not be loaded.警告:无法加载文件 C:\\Users\\User.android\\repositories.cfg。 Warning: Failed to find package platform;android-25 [===警告:找不到包平台;android-25 [===
] 10% Computing updates... C:\\Android\\android-sdk\\tools\\bin> ] 10% 计算更新... C:\\Android\\android-sdk\\tools\\bin>

And finally I've tried install with Unity Hub:最后我尝试使用 Unity Hub 进行安装:

在此处输入图片说明

but it same result, device does not appears in Editor, and External Tools shows checked:但同样的结果,设备没有出现在编辑器中,并且外部工具显示选中:

在此处输入图片说明

and if unchecked:如果未选中:

在此处输入图片说明

I'm not quite sure, what I've missed or what I'm doing incorrectly我不太确定,我错过了什么或我做错了什么

Any guide, advice or example would be helpful任何指南、建议或示例都会有所帮助

For Unity Remote to work, you need to have the Android SDK on your development machine.要使 Unity Remote 工作,您需要在开发机器上安装 Android SDK。 Do you have it?你有吗? If yes, then to specify the location of your Android SDK, go to Edit > Preferences.如果是,则要指定 Android SDK 的位置,请转到“编辑”>“首选项”。 Unity Docs will help you. Unity Docs将为您提供帮助。

To use Visual Studio 2019 to debug a Unity app running on an Android device connected to USB:要使用 Visual Studio 2019 调试在连接到 USB 的 Android 设备上运行的 Unity 应用程序:

  1. Install Unity and Visual Studio 2019 (any edition including Community Edition).安装 Unity 和 Visual Studio 2019(任何版本,包括社区版)。 Verify in the Visual Studio installer that Game development with Unity is included.在 Visual Studio 安装程序中验证是否包含使用 Unity 的游戏开发。
  2. Set the Android device in Developer Mode.将 Android 设备设置为开发者模式。 In Settings |在设置 | About Tablet |关于平板 | Software Information, tab the Build number field 8 times.软件信息,点击 Build number 字段 8 次。 In Settings |在设置 | Developer options: enable USB Debugging;开发者选项:开启USB调试; enable Stay Awake.启用保持清醒。
  3. Make sure Visual Studio 2019 is the external tool used by Unity.确保 Visual Studio 2019 是 Unity 使用的外部工具。 This way, Unity will generate a solution file containing all script assets and .net packages.这样,Unity 将生成一个包含所有脚本资产和 .net 包的解决方案文件。 If you don't (and just open the editor based on the file extension), you don't get intellisense and no debugging when you start/attach the debugger.如果不这样做(并且只是根据文件扩展名打开编辑器),则在启动/附加调试器时不会获得智能感知,也不会进行调试。 Use Edit |使用编辑 | Preferences.喜好。 外部编辑
  4. Copy the Android SDK path from the version of Unity that you use for editing your current project, into Visual Studio.将用于编辑当前项目的 Unity 版本中的 Android SDK 路径复制到 Visual Studio 中。 Copy the path from Unity "Edit | Preferences | External Tools | Android SDK Tools installed with Unity" to Visual Studio "Tools | Options | Tools for Unity | General | Preferred Android Sdk Root".将路径从 Unity“编辑 | 首选项 | 外部工具 | 与 Unity 一起安装的 Android SDK 工具”复制到 Visual Studio“工具 | 选项 | Unity 工具 | 常规 | 首选 Android Sdk 根”。 This path is similar to this, depending your Unity version: "C:\\Unity\\2020.3.12f1\\Editor\\Data\\PlaybackEngines\\AndroidPlayer\\SDK".此路径与此类似,具体取决于您的 Unity 版本:“C:\\Unity\\2020.3.12f1\\Editor\\Data\\PlaybackEngines\\AndroidPlayer\\SDK”。 Note that this is a critical step for the USB connected device to show up in Visual Studio.请注意,这是 USB 连接设备在 Visual Studio 中显示的关键步骤。 See also https://answers.unity.com/questions/1320966/android-debug-usb.html .另请参阅https://answers.unity.com/questions/1320966/android-debug-usb.html SDK路径
  5. In Unity File |在 Unity 文件中 | Build Settings, select Development Build, Script Debugging and Wait for Managed Debugger.构建设置,选择开发构建、脚本调试和等待托管调试器。 构建设置
  6. In the Unity Status Bar (bottom Right) enable Debug Mode.在 Unity 状态栏(右下角)中启用调试模式。 The bug icon needs to be yellow.错误图标需要为黄色。 调试模式
  7. Build and Run from Unity.从 Unity 构建和运行。 Wait for the android device to show the app with dialog “Debug |等待 android 设备显示带有对话框“调试 | ”的应用程序。 You can attach a managed debugger now if you want”.如果需要,您现在可以附加托管调试器”。
  8. Open a C# Script from Unity, this opens the Visual Studio solution从 Unity 打开 C# 脚本,这将打开 Visual Studio 解决方案
  9. In Visual Studio, use Debug |在 Visual Studio 中,使用 Debug | Attach Unity Debugger to see the running instances.附加 Unity Debugger 以查看正在运行的实例。 在此处输入图片说明
  10. Select the USB device that is running the App and awaiting the managed debugger to connect.选择正在运行应用程序并等待托管调试器连接的 USB 设备。 选择设备

Place breakpoints in the code for single stepping and watching and inspecting variables as needed.在代码中放置断点,以便根据需要进行单步执行以及观察和检查变量。 🎉 🎉

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM