从unity3D上传文件解析错误

本文关键字:错误 文件 unity3D | 更新日期: 2023-09-27 18:02:24

我正在开发的一款游戏中使用Parse。一切都很好,直到我尝试上传一个文件,无论他的扩展名我总是得到这个错误:"get_version只能从主线程调用"

URL = http://www.hostingpics.net/viewer.php?id=764075parseError.png

,这是我的脚本:

byte[] data = System.Text.Encoding.UTF8.GetBytes("Working at Parse is great!");
            ParseFile file = new ParseFile("resume.txt", data);
            Task saveTask = file.SaveAsync();
            var player = new ParseObject ("FilesLibrary");
            player ["Number"] = 155;
            player ["Files"] = file;
            saveTask = player.SaveAsync();

我试着把这个脚本在不同的地方,但我总是得到同样的问题。

从unity3D上传文件解析错误

在尝试将文件保存到db对象之前,需要确保先将文件上传到Parse。

解析Unity文档不是很好,因为它不适合Unity的线程模型。我建议使用协同例程来完成所有的Parse调用。

我也把这个贴在了Unity Answers上,但是你懂的。

// The file first needs to be uploaded to Parse before it can be saved to the object in the database
 ParseFile _playerFile;
 public void CreatePlayer()
 {
     StartCoroutine (UploadPlayerFile ((response) => {
         if(response == 1)
             StartCoroutine(CreateProjectAsync());                        
         else
             Debug.LogError("The file could not be uploaded");
     }));
 }        
 IEnumerator UploadPlayerFile(Action <int> callback)
 {
     var fileBytes = System.IO.File.ReadAllBytes (@"C:'myfile.jpg");
     _playerFile = new ParseFile ("file.jpg",  fileBytes, "image/jpeg");
     var saveTask = _playerFile.SaveAsync ();
     while (!saveTask.IsCompleted)
         yield return null;        
     if (saveTask.IsFaulted) {
         Debug.LogError ("An error occurred while uploading the player file : " + saveTask.Exception.Message);
         callback (-1);
     } else {            
         callback (1);
     }    
 }
 IEnumerator CreateProjectAsync()
 {        
     ParseObject player = new ParseObject ("Player");
     player ["Number"] = 111;
     player ["Files"] = _playerFile;
     var saveTask = player.SaveAsync ();
     while (!saveTask.IsCompleted)
         yield return null;        
     if (saveTask.IsFaulted) {
         Debug.LogError ("An error occurred while creating the player object : " + saveTask.Exception.Message);
     } else {    
         Debug.Log ("Player created successfully");
     }    
 }

我猜你需要强制你的解析查询在Main Thread上执行。您可以通过

实现这一点
public readonly static Queue<Action> ExecuteOnMainThread = new Queue<Action> ();

在此队列中添加您的查询方法。像

ExecuteOnMainThread.Enqueue (() => {
                StartCoroutine (SaveData (ID, playerName)); });

在你的SaveData() Coroutine中,你需要执行你的Parse查询。

最后:

void Update()
{
    while (ExecuteOnMainThread != null && ExecuteOnMainThread.Count > 0)
    {
        Action currentAction = ExecuteOnMainThread.Dequeue();
        if (currentAction != null) 
        {
            currentAction.Invoke ();
        }
    }
}

奇怪,同样的脚本在unity 4.6下工作得很好,但在5.1下就不行了!