在Unity中获取MTP设备

问题描述 投票:-1回答:1

你好,我目前正在为一个课程项目在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)

当我不在Unity中时,代码会检测到设备。所以我的结论是,它不能在unity环境下工作。

你有什么办法解决这个问题,或者有其他方法在有线模式下保存到我的手机上吗? 也许是一个免费的资产?

c# windows unity3d unityscript mtp
1个回答
0
投票

我强烈建议你不要使用 MTP Device 为了你的目的,我可以告诉你做一个TCP Socket来移动你的文件到你的设备。阅读更多 此处

© www.soinside.com 2019 - 2024. All rights reserved.