Godot UI线程,Task异步和消息弹窗通知
前言
最近我在研究Godot的全局消息,然后发现Godot 也是有UI线程限制的,只能在主线程的子线程里面修改UI。
线程安全


全局消息IOC注入
我之前写过Godot 的IOC注入,这些都是CSDN时期的博客。但是后面CSDN的广告实在是太多了,我就转到博客园里面来了。
Godot.NET C# 工程化开发(1):通用Nuget 导入+ 模板文件导出,包含随机数生成,日志管理,数据库连接等功能
注意,我后面的都是基于我那个IOC框架来写的。

消息窗口搭建
如何修改Label样式可以看我上一篇文章
最简单的消息提示

using Godot;
using Godot_UI_Test.Utils;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
namespace Godot_UI_Test.SceneModels
{
public class MessageSceneModel : ISceneModel
{
private PrintHelper printHelper;
private Godot.Label title;
private Godot.Label content;
private VBoxContainer container;
private ColorRect colorRect;
public MessageSceneModel(PrintHelper printHelper) {
this.printHelper = printHelper;
printHelper.SetTitle(nameof(MessageSceneModel));
}
public override void Process(double delta)
{
//throw new NotImplementedException();
}
public override void Ready()
{
printHelper.Debug("加载完成");
colorRect = Scene.GetNode<ColorRect>("ColorRect");
container = colorRect.GetNode<VBoxContainer>("VBoxContainer");
title = container.GetNode<Godot.Label>("Title");
content = container.GetNode<Godot.Label>("Content");
//同步容器大小
container.Size = colorRect.Size;
printHelper.Debug(JsonConvert.SerializeObject(title.Size));
//默认设置为不可见
Scene.Visible = false;
//throw new NotImplementedException();
}
/// <summary>
/// 弹窗延迟退出
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public async Task ShowInfo(string message)
{
Scene.Visible = true;
printHelper.Debug("Info打印信息");
printHelper.Debug(message);
await Task.Delay(3000);
Scene.Visible = false;
}
}
}
简单使用

虽然有点丑,但是能用

仿Element UI
ElementUI 效果

简单的Label样式

简单的画一下,我就不给具体的参数了,大家点一下就知道了

如何快速加载多个相同节点
如果我们把这个作为场景,又没有那么的复杂。如果用代码生成,写起来很麻烦,也不直观。最好的方法就是复制节点添加。

using Godot;
using Godot_UI_Test.Utils;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
namespace Godot_UI_Test.SceneModels
{
public class MessageSceneModel : ISceneModel
{
private PrintHelper printHelper;
private VBoxContainer vBoxContainer;
private AssetsHelper assetsHelper;
private Godot.Label label;
public MessageSceneModel(PrintHelper printHelper, AssetsHelper assetsHelper)
{
this.printHelper = printHelper;
printHelper.SetTitle(nameof(MessageSceneModel));
this.assetsHelper = assetsHelper;
}
public override void Process(double delta)
{
//throw new NotImplementedException();
}
public override void Ready()
{
printHelper.Debug("加载完成");
vBoxContainer = Scene.GetNode<VBoxContainer>("VBoxContainer");
label = Scene.GetNode<Godot.Label>("Label");
//将vBoxContainer居中,GodotProjectSetting是自己设置的
vBoxContainer.Position = new Vector2(GodotProjectSetting.Width/4, 10);
//添加label的靠别,不能直接添加label,因为label已经拥有了父节点
var newLabel = label.Duplicate() as Godot.Label;
//显示Label
newLabel.Visible =true;
vBoxContainer.AddChild(newLabel.Duplicate());
vBoxContainer.AddChild(newLabel.Duplicate());
vBoxContainer.AddChild(newLabel.Duplicate());
//CreateText("te321");
//CreateText("te321 3213 321 3434 4 2 41321 st1 te321 3213 321 3434 4 2 41321 st1 te321 3213 321 3434 4 2 41321 st1");
printHelper.Debug(JsonConvert.SerializeObject(vBoxContainer.Position));
printHelper.Debug(JsonConvert.SerializeObject(vBoxContainer.GetWindow().Position));
//Scene.Visible = false;
//throw new NotImplementedException();
}
private void CreateText(string text)
{
var res = new Godot.Label();
res.AddThemeStyleboxOverride("normal", assetsHelper.MessageItemStyle);
res.AutowrapMode = TextServer.AutowrapMode.WordSmart;
res.HorizontalAlignment = HorizontalAlignment.Center;
res.Text = text;
res.CustomMinimumSize = new Vector2(200, 0);
label = res;
vBoxContainer.AddChild(res);
}
/// <summary>
/// 延迟打印
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public async Task ShowInfo(string message)
{
printHelper.Debug("Info打印信息");
}
}
}

修改一下,IOC按钮事件注册
using Godot;
using Godot_UI_Test.Utils;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
namespace Godot_UI_Test.SceneModels
{
public class MessageSceneModel : ISceneModel
{
private PrintHelper printHelper;
private VBoxContainer vBoxContainer;
private AssetsHelper assetsHelper;
private Godot.Label label;
public MessageSceneModel(PrintHelper printHelper, AssetsHelper assetsHelper)
{
this.printHelper = printHelper;
printHelper.SetTitle(nameof(MessageSceneModel));
this.assetsHelper = assetsHelper;
}
public override void Process(double delta)
{
//throw new NotImplementedException();
}
public override void Ready()
{
printHelper.Debug("加载完成");
vBoxContainer = Scene.GetNode<VBoxContainer>("VBoxContainer");
label = Scene.GetNode<Godot.Label>("Label");
//将vBoxContainer居中,GodotProjectSetting是自己设置的
vBoxContainer.Position = new Vector2(GodotProjectSetting.Width/4, 10);
//CreateText("te321 3213 321 3434 4 2 41321 st1 te321 3213 321 3434 4 2 41321 st1 te321 3213 321 3434 4 2 41321 st1");
printHelper.Debug(JsonConvert.SerializeObject(vBoxContainer.Position));
printHelper.Debug(JsonConvert.SerializeObject(vBoxContainer.GetWindow().Position));
//Scene.Visible = false;
//throw new NotImplementedException();
}
/// <summary>
/// 挂载Label
/// </summary>
/// <param name="text"></param>
private Godot.Label CreateText(string text)
{
var newLabel = label.Duplicate() as Godot.Label;
newLabel.Text = text;
newLabel.Visible=true;
vBoxContainer.AddChild(newLabel);
return newLabel;
}
/// <summary>
/// 延迟打印
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public async Task ShowInfo(string message)
{
printHelper.Debug("Info打印信息");
var newLabel = CreateText(message);
await Task.Delay(3 * 1000);
newLabel.Free();
}
}
}


总结
我只是很潦草的实现了消息弹窗这个功能,还没加动画效果。不过这个确实让我学到了很多,尤其是节点挂载这个事情。
Godot UI线程,Task异步和消息弹窗通知的更多相关文章
- Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面
Android应用的开发过程中需要把繁重的任务(IO,网络连接等)放到其他线程中异步执行,达到不阻塞UI的效果. 下面将由浅入深介绍Android进行异步处理的实现方法和系统底层的实现原理. 本文介绍 ...
- 细说UI线程和Windows消息队列(经典)
在Windows应用程序中,窗体是由一种称为“UI线程(User Interface Thread)”的特殊类型的线程创建的. 首先,UI线程是一种“线程”,所以它具有一个线程应该具有的所有特征,比如 ...
- 细说UI线程和Windows消息队列
在 Windows应用程序中,窗体是由一种称为“ UI线程( User Interface Thread)”的特殊类型的线程创建的. 首先, UI线程是一种“线程”,所以它具有一个线程应该具有的所有特 ...
- 【转】细说UI线程和Windows消息队列
在Windows应用程序中,窗体是由一种称为“UI线程(User Interface Thread)”的特殊类型的线程创建的. 首先,UI线程是一种“线程”,所以它具有一个线程应该具有的所有特征,比如 ...
- c#多线程(UI线程,控件显示更新) Invoke和BeginInvoke 区别
如果只是直接使用子线程访问UI控件,直接看内容三,如果想深入了解从内容一看起. 一.Control.Invoke和BeginInvoke方法的区别 先上总结: Control.Invoke 方法 (D ...
- [转] c#多线程(UI线程,控件显示更新) Invoke和BeginInvoke 区别
如果只是直接使用子线程访问UI控件,直接看内容三,如果想深入了解从内容一看起. 一.Control.Invoke和BeginInvoke方法的区别 先上总结: Control.Invoke 方法 (D ...
- 千万别在UI线程上调用Control.Invoke和Control.BeginInvoke,因为这些是依然阻塞UI线程的,造成界面的假死
原文地址:https://www.cnblogs.com/wangchuang/archive/2013/02/20/2918858.html .c# Invoke和BeginInvoke 区别 Co ...
- Android UI线程和非UI线程
Android UI线程和非UI线程 UI线程及Android的单线程模型原则 当应用启动,系统会创建一个主线程(main thread). 这个主线程负责向UI组件分发事件(包括绘制事件),也是在这 ...
- android脚步---如何看log之程序停止运行,和UI线程和非UI线程之间切换
经常运行eclipse时,烧到手机出现,“停止运行”,这时候得通过logcat查log了.一般这种情况属于FATAL EXCEPTION,所以检索FATAL 或者 EXCEPTION,然后往下看几行 ...
- 关于“UI线程”
http://www.cppblog.com/Streamlet/archive/2013/05/05/199999.html 缘起 这是一篇找喷的文章. 由于一些历史原因和人际渊源,周围同事谈论一些 ...
随机推荐
- 钉钉机器人自动关联 GitHub 发送 approval prs
摘要:用技术来解决 PM 枯燥的 approval pr 工作,本文将阐述如何自动化获取 GitHub Organization 下各个 repo 待 merge 的 pull requests 并通 ...
- STL-stack模拟实现
#pragma once #include<assert.h> #include<list> #include<vector> #include<deque& ...
- [C++] epoll编写的echo服务端
直接贴代码,代码是运行在Linux上面的,通过 g++ epoll.cpp编译 #include <sys/socket.h> #include <sys/epoll.h> # ...
- foundation部分学习记录(更正更新中……)
foundation部分学习记录(更新中--) 从FDB的角度看,它对上层只提供有序+事务+KV存储的抽象. 设计原则 模块化分割,尽量细分且模块之间相互解耦 例如事务系统内,其提交(write pa ...
- rst文件查看(Sphinx)
reStructuredText ( RST . ReST 或 reST )是一种用于文本数据的文件格式,主要用于 Python 编程语言社区的技术文档. 在下载了别人的Python源文件里面有rst ...
- Oracle中表字段有使用Oracle关键字的一定要趁早改!!!
一.问题由来 现在进行项目改造,数据库需要迁移,由原来的使用GBase数据库改为使用Oracle数据库,今天测试人员在测试时后台报了一个异常. 把SQL语句单独复制出来进行查询,还是报错,仔细分析原因 ...
- vite ts 安装 js-cookie 库,vscode找不到类型说明(有波浪线),解决方案
vite ts 安装 js-cookie 库,vscode找不到类型说明(有波浪线),解决方案 先安装库 https://www.npmjs.com/package/js-cookie 再安装类型 h ...
- DiagnosticSource DiagnosticListener 无侵入式分布式跟踪
ASP.NET Core 中的框架中发出大量诊断事件,包括当前请求进入请求完成事件,HttpClient发出收到与响应,EFCore查询等等. 我们可以利用DiagnosticListener来选择性 ...
- Ubuntu 22.04 源码安装ST-Link V2过程详解
一 首先安装依赖工具: A 安装预编译库: sudo apt-get install git make cmake libusb-1.0-0-dev B 安装gcc库: sudo apt-get in ...
- SpringBoot 支付宝付款接口类、支付异步回调函数模板
1.付款接口类 1.1.引入Maven依赖 <dependency> <groupId>com.alipay.sdk</groupId> <artifactI ...