您好,我目前正在为一个课程项目开发一个关于 Unity 的游戏,我绝对必须能够从我的计算机上将我的备份文件存储在我的手机上,而且我必须能够检索它们。 我在带有安卓手机的 Windows 10 和 Unity 2019.3.14f1 上。
我尝试使用 NuGet MediaDevice 包,如下所示:
public static void CopySaveFromDevice()
{
var devices = MediaDevice.GetDevices();
Debug.Log(devices);
using (var device = devices.First(d => d.FriendlyName == "Mi Note 10"))
{
Debug.Log(device);
device.Connect();
var savesDir = device.GetDirectoryInfo(@"/Treen/Saves");
var files = savesDir.EnumerateFiles("*.tree", SearchOption.TopDirectoryOnly);
foreach (var file in files)
{
MemoryStream memoryStream = new System.IO.MemoryStream();
device.DownloadFile(file.FullName, memoryStream);
memoryStream.Position = 0;
WriteStreamToDisk(Application.persistentDataPath + "/Saves/" + file.Name, memoryStream);
}
device.Disconnect();
}
}
static void WriteStreamToDisk(string filePath, MemoryStream memoryStream)
{
using (FileStream file = new FileStream(filePath, FileMode.Create, System.IO.FileAccess.Write))
{
byte[] bytes = new byte[memoryStream.Length];
memoryStream.Read(bytes, 0, (int)memoryStream.Length);
file.Write(bytes, 0, bytes.Length);
memoryStream.Close();
}
}
但我有这个错误:
NullReferenceException: Object reference not set to an instance of an object
MediaDevices.MediaDevice.GetDevices () (at <016781043aa441d5b0169b9d5116ee77>:0)
ManageSaves.CopySaveFromDevice () (at Assets/Scripts/ManageSaves.cs:65)
ManageSaves.Testing () (at Assets/Scripts/ManageSaves.cs:57)
MainMenu.Testing () (at Assets/Scripts/MainMenu.cs:69)
UnityEngine.Events.InvokableCall.Invoke () (at <23a7799da2e941b88c6db790c607d655>:0)
UnityEngine.Events.UnityEvent.Invoke () (at <23a7799da2e941b88c6db790c607d655>:0)
UnityEngine.UI.Button.Press () (at C:/Program Files/Unity/Hub/Editor/2019.3.14f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Button.cs:68)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/Program Files/Unity/Hub/Editor/2019.3.14f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Button.cs:110)
UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/Program Files/Unity/Hub/Editor/2019.3.14f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/ExecuteEvents.cs:50)
UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at C:/Program Files/Unity/Hub/Editor/2019.3.14f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/ExecuteEvents.cs:261)
UnityEngine.EventSystems.EventSystem:Update() (at C:/Program Files/Unity/Hub/Editor/2019.3.14f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/EventSystem.cs:377)
当我不在统一中时,代码会检测设备。所以我的结论是它在统一环境中不起作用:/
您知道如何解决此问题或其他以有线模式保存到手机的方法吗? 也许是免费资产?
我强烈建议您不要使用MTP Device
文件路径。 为了您的目的,我可以告诉您制作一个TCP套接字以将文件移动到您的设备。在此处阅读更多内容