- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正尝试在我的 unity 游戏中使用 parse.com 服务。我的问题是根据从查询中收到的结果实例化对象。
例如,当我运行下面的代码时;
var queryCurrent = ParseObject.GetQuery("Levels")
.WhereEqualTo("ItemId", "Character")
.WhereEqualTo("Level", "3");
queryCurrent.FirstAsync().ContinueWith(t =>
{
Character character = ScriptableObject.CreateInstance<Character>();
});
我收到以下错误;
CreateInstanceFromType can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
这似乎是一个普遍的问题,每个人都试图通过使用协程来找到解决方法。优化的解决方案将不胜感激。
提前致谢。
最佳答案
使用协程在主线程上运行。它记录在 Parse 站点上:https://parse.com/docs/unity_guide#tasks-coroutines
编辑:
Unity 提供了协同多任务处理的概念,允许代码在主线程上运行时交错。任务和延续模型大多独立于这种多任务机制,但很容易适应在协程中工作。您的协程只需检查任务的 IsCompleted 属性即可了解异步工作是否已完成,从而允许协程从中断处继续。例如,以下协程代码保存一个对象,然后等待查询返回:
public IEnumerator GameOver()
{
var gameHistory = new ParseObject("GameHistory");
gameHistory["score"] = score;
gameHistory["player"] = ParseUser.CurrentUser;
var saveTask = gameHistory.SaveAsync();
while (!saveTask.IsCompleted) yield return null;
// When the coroutine reaches this point, the save will be complete
var historyQuery = new ParseQuery<ParseObject>("GameHistory")
.WhereEqualTo("player", ParseUser.CurrentUser)
.OrderByDescending("createdAt");
var queryTask = historyQuery.FindAsync();
while (!queryTask.IsCompleted) yield return null;
// The task is complete, so we can simply check for its result to get
// the current player's game history
var history = queryTask.Result;
}
关于c# - Parse.com 查询的主线程问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24441501/
有人可以向我澄清主线 DHT 规范中的声明吗? Upon inserting the first node into its routing table and when starting up th
我正在尝试使用 USB 小工具驱动程序使嵌入式设备作为 MTP 设备工作。 我知道 Android 从大容量存储设备切换到 MTP 设备已经有一段时间了,并且找到了 source code for M
我是一名优秀的程序员,十分优秀!