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 缘起 这是一篇找喷的文章. 由于一些历史原因和人际渊源,周围同事谈论一些 ...
随机推荐
- Java 常用类 于 StringBuffer 和 StringBuilder的使用 + String三者的异同
1 package com.bytezero.stringclass; 2 3 import org.junit.Test; 4 5 /** 6 * 关于 StringBuffer 和 StringB ...
- 1、mysql-索引简介
1.1 MySQL官方对索引的定义为: 索引(index)是帮助MySQL高效获取数据的数据结构(有序).在数据之外,数据库系统还维护者满足特定查找算法的数据结构,这些数据结构以某种方式引用(指向)数 ...
- base64 转文件上传
// 将base64转换为blob dataURLtoBlob (dataurl) { let arr = dataurl.split(',') let mime = arr[0].match(/:( ...
- [【stars-one】Android图标生成器 PC工具
原文: [stars-one]Android图标生成器 - Stars-One的杂货小窝 一款Android开发者的PC工具软件,可以快速生成android开发需要使用的xml矢量图标文件 起因 个人 ...
- Hibernate之list和iterator
在Hibernate3中将Session.find(),Session.iterator()换成:session.createQuery().list(),session.createQuery(). ...
- 三维模型3DTile格式轻量化的纹理压缩和质量关系分析
三维模型3DTile格式轻量化的纹理压缩和质量关系分析 在三维模型的3DTile格式轻量化处理中,纹理压缩是一个重要环节.但是,纹理压缩和模型质量之间存在明显的关系需要权衡.以下是纹理压缩和模型质量关 ...
- 开源一个教学型分库分表示例项目 shardingsphere-jdbc-demo
在笔者心中,消息队列,缓存,分库分表是高并发解决方案三剑客. 分库分表之所以被广泛使用,因为工程相对简单,但分库分表并不仅仅是分片,还是需要考虑如何扩缩容(全量同步.增量同步.数据校验等). 因此笔者 ...
- elasticsearch使用painless的一些简单例子
目录 1.背景 2.准备数据 2.1 mapping 2.2 插入数据 3.例子 3.1 (update)更新文档 id=1 的文档,将 age 加 2岁 3.2 (update_by_query)如 ...
- SpringBoot集成LDAP同步数据
1.pom引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId& ...
- oracle_fdw扩展使用中异常问题
Oracle_fdw 的使用介绍已经在之前的文章中介绍过了,具体见:https://www.cnblogs.com/kingbase/p/14846479.html. 本篇文章介绍一下可能碰到的问题处 ...