因为经常要直接把工程文件推入到手机上跑真机测试,就做了一个,在工程内选中文件,推送到手机的简单脚本。
这里的根据项目需要,按文件的目录结够push进手机,如果只是推buddle,会更简单点,不做拓展了。
核心部分,unity调用ADB命令, 文件目录。
using UnityEngine;
using UnityEditor;
using System.Diagnostics;
using System.IO;public class PushFileToPhone : EditorWindow
{[MenuItem("Tools/ADB/Push Selected File To Phone")]public static void OpenWindow(){GetWindow<PushFileToPhone>("Push File To Phone");}private string luaFolderPath = "Assets/Lua"; // Lua文件夹路径private string phonePath = "/storage/emulated/0/Android/data/com.xxx.dev/files/"; // 修改为目标可写目录void OnGUI(){GUILayout.Label("Push Selected File To Phone", EditorStyles.boldLabel);luaFolderPath = EditorGUILayout.TextField("Lua Folder Path", luaFolderPath);phonePath = EditorGUILayout.TextField("Phone Path", phonePath);if (GUILayout.Button("Push File")){PushSelectedFile();}if (GUILayout.Button("Open Phone Path")){OpenPhonePath();}}private void PushSelectedFile(){try{// 获取选中的文件路径string selectedFilePath = AssetDatabase.GetAssetPath(Selection.activeObject);if (string.IsNullOrEmpty(selectedFilePath)){UnityEngine.Debug.LogError("No file selected.");return;}UnityEngine.Debug.Log("Selected File Path: " + selectedFilePath);// 判断选中文件是否为txt文件if (!selectedFilePath.EndsWith(".txt")){UnityEngine.Debug.LogError("Selected file is not a txt file.");return;}// 获取相对Lua文件夹的路径string relativePath = GetRelativePath(selectedFilePath, luaFolderPath);if (string.IsNullOrEmpty(relativePath)){UnityEngine.Debug.LogError("Selected file is not in the Lua folder.");return;}// 构建目标路径string targetPath = Path.Combine(phonePath, relativePath);UnityEngine.Debug.Log("Target Path: " + targetPath);// 创建目标目录string targetDir = Path.GetDirectoryName(targetPath);UnityEngine.Debug.Log("Target Directory: " + targetDir);CreateDirectory(targetDir);// 构建ADB命令string adbPath = GetAdbPath();string command = $"push \"{selectedFilePath}\" \"{targetPath}\"";// 执行ADB命令ProcessStartInfo processStartInfo = new ProcessStartInfo{FileName = adbPath,Arguments = command,UseShellExecute = false,RedirectStandardOutput = true,RedirectStandardError = true,CreateNoWindow = true};Process process = new Process { StartInfo = processStartInfo };process.Start();// 输出命令执行结果string output = process.StandardOutput.ReadToEnd();string error = process.StandardError.ReadToEnd();process.WaitForExit();if (string.IsNullOrEmpty(error)){UnityEngine.Debug.Log("File pushed successfully: " + output);}else{UnityEngine.Debug.LogError("Error pushing file: " + error);}}catch (System.Exception ex){UnityEngine.Debug.LogError("Exception: " + ex.Message);}}private string GetRelativePath(string filePath, string baseFolderPath){// 确保基础文件夹路径以斜杠结尾if (!baseFolderPath.EndsWith("/")){baseFolderPath += "/";}// 判断文件是否在基础文件夹内if (!filePath.StartsWith(baseFolderPath)){return null;}// 获取相对路径string relativePath = filePath.Substring(baseFolderPath.Length);relativePath = relativePath.Replace("\\", "/");return relativePath;}private void CreateDirectory(string dirPath){try{// 构建ADB命令string adbPath = GetAdbPath();string command = $"shell mkdir -p \"{dirPath}\"";// 执行ADB命令ProcessStartInfo processStartInfo = new ProcessStartInfo{FileName = adbPath,Arguments = command,UseShellExecute = false,RedirectStandardOutput = true,RedirectStandardError = true,CreateNoWindow = true};Process process = new Process { StartInfo = processStartInfo };process.Start();// 输出命令执行结果string output = process.StandardOutput.ReadToEnd();string error = process.StandardError.ReadToEnd();process.WaitForExit();if (!string.IsNullOrEmpty(error)){UnityEngine.Debug.LogError("Error creating directory: " + error);}else{UnityEngine.Debug.Log("Directory created successfully: " + dirPath);}}catch (System.Exception ex){UnityEngine.Debug.LogError("Exception creating directory: " + ex.Message);}}private string GetAdbPath(){// 这里假设ADB工具已安装在系统环境变量中,直接返回"adb"即可// 如果ADB工具未安装在环境变量中,需要指定ADB工具的完整路径return "adb";}private void OpenPhonePath(){try{// 构建ADB命令string adbPath = GetAdbPath();string command = $"shell am start -a android.intent.action.VIEW -d \"file://{phonePath}\"";// 执行ADB命令ProcessStartInfo processStartInfo = new ProcessStartInfo{FileName = adbPath,Arguments = command,UseShellExecute = false,RedirectStandardOutput = true,RedirectStandardError = true,CreateNoWindow = true};Process process = new Process { StartInfo = processStartInfo };process.Start();// 输出命令执行结果string output = process.StandardOutput.ReadToEnd();string error = process.StandardError.ReadToEnd();process.WaitForExit();if (string.IsNullOrEmpty(error)){UnityEngine.Debug.Log("Phone path opened successfully: " + output);}else{UnityEngine.Debug.LogError("Error opening phone path: " + error);}}catch (System.Exception ex){UnityEngine.Debug.LogError("Exception opening phone path: " + ex.Message);}}
}