简体   繁体   English

上传文件以从unity3D解析时出错

[英]error uploading file to parse from unity3D

I am using Parse for a game that I'm developing. 我正在使用Parse开发自己的游戏。 Everything is ok until I try to uplad a file, no matters his extension I always get this Error : "get_version can only be called from main thread" 一切正常,直到我尝试上载文件为止,无论他的扩展名如何,我总是会收到此错误:“只能从主线程调用get_version”

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

and this is my script : 这是我的脚本:

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();

I have tried to place this script in different places but I always get the same problem. 我曾尝试将此脚本放置在不同的位置,但是我总是遇到相同的问题。

I guess you need force your Parse queries to execute on Main Thread . 我猜您需要强制您的Parse查询在Main Thread上执行。 you can achieve this by 你可以通过实现

public readonly static Queue<Action> ExecuteOnMainThread = new Queue<Action> ();

Add your query Method in this Queue. 在此队列中添加您的查询方法。 Like 喜欢

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

In your SaveData() Coroutine you need to execute your Parse queries. 在您的SaveData() Coroutine您需要执行解析查询。

and finally: 最后:

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

很奇怪,同一脚本在unity 4.6上可以正常使用,但在5.1下不能正常使用!

You need to make sure the file is uploaded to Parse first, before you try to save it to the db object. 您需要先将文件上载到Parse,然后再尝试将其保存到db对象。

The Parse Unity documentation is not very good as it doesn't fit Unity's threading model. Parse Unity文档不是很好,因为它不适合Unity的线程模型。 I would suggest using co-routines to do all your Parse calls. 我建议使用协同例程进行所有的Parse调用。

I posted this on Unity Answers as well, but you get the idea.. 我也将其发布在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");
     }    
 }

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

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