码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 【解决】Unity3D中无法在MQTT事件中执行Animator


    问题原因:

    解决方法:

    解决过程

    1、在 Unity 中创建一个名为 MainThreadDispatcher 的脚本,用于处理主线程操作。

    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. public class MainThreadDispatcher : MonoBehaviour
    4. {
    5. private static MainThreadDispatcher instance;
    6. private static readonly Queue actionQueue = new Queue();
    7. private void Awake()
    8. {
    9. instance = this;
    10. }
    11. private void Update()
    12. {
    13. lock (actionQueue)
    14. {
    15. while (actionQueue.Count > 0)
    16. {
    17. System.Action action = actionQueue.Dequeue();
    18. action.Invoke();
    19. }
    20. }
    21. }
    22. public static void ExecuteOnMainThread(System.Action action)
    23. {
    24. lock (actionQueue)
    25. {
    26. actionQueue.Enqueue(action);
    27. }
    28. }
    29. }

    2、在 Unity 中创建一个名为 MainThreadDispatcherInitializer 的脚本,用于初始化 MainThreadDispatcher。

    1. using UnityEngine;
    2. public class MainThreadDispatcherInitializer : MonoBehaviour
    3. {
    4. private void Awake()
    5. {
    6. DontDestroyOnLoad(this.gameObject);
    7. gameObject.AddComponent();
    8. }
    9. }

    3、在 Unity 中创建一个名为 MainThreadDispatcherInitializer 的空 GameObject,并将 MainThreadDispatcherInitializer 脚本添加到该 GameObject 上。

    4、在其他脚本中,可以通过 MainThreadDispatcher.ExecuteOnMainThread 方法将操作推送到主线程执行。

    1. public class ExampleScript : MonoBehaviour
    2. {
    3. private void Start()
    4. {
    5. // 在其他线程中执行操作
    6. System.Threading.Thread thread = new System.Threading.Thread(() =>
    7. {
    8. // 模拟耗时操作
    9. System.Threading.Thread.Sleep(2000);
    10. // 在主线程中执行操作
    11. MainThreadDispatcher.ExecuteOnMainThread(() =>
    12. {
    13. // 执行动画操作
    14. Debug.Log("操作在主线程中执行");
    15. });
    16. });
    17. thread.Start();
    18. }
    19. }

    在这也感谢siki学院的Sk-Sky老师耐心的帮我远程解决问题

  • 相关阅读:
    关于2023中国(济南)国际换热传热技术与应用展览会通知
    C++:AVL树
    Vue的使用
    面向对象及工程环境构建,220729,
    【23】线程相关扩展习题
    【docker】windows版本安装学习使用
    力扣(LeetCode)272. 最接近的二叉搜索树值 II(2022.09.29)
    BAT031:按列表名单将路径a下的文件夹批量剪切到路径b
    ios开发App,在App Store上架的app更新提交过程
    【备忘录】kafka常用命令维护
  • 原文地址:https://blog.csdn.net/wwwwerewrew/article/details/133135600
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号