Godot 制作根据文本量自适应高度的按钮,并使用垂直布局容器将多个按钮排列

(版本:Godot 4.11)

一、准备一个PanelContainer名字为Control,在里面放入一个button,以及一个label。

PanelContainer:使其子节点可以利用其容器属性进行定位,以自适应其大小。

label:不适用button自带的text,功能不够用。

button:提供点击功能,没什么好说的。

二、修改label的AutoWrap Mode属性为"Word (Smart)",这样就可以实现自动换行功能。

三、改变Control的大小,可以看到按钮和Lable的大小随之改变,这是Container sizing(容器大小)的功劳。

四、构建选项框

依次增加节点:Control,ScrollContainer,VBoxContainer,名字自取,然后往里面加入制作好的选项。需要注意的是,调整好Control,ScrollContainer​​​​​​​的宽度,这样

最后效果类似这样:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个使用多线程加载Godot资源的示例代码: ```python import threading import queue import os import time import glob from functools import partial from typing import List import godot class ResourceLoader: def __init__(self, resources_dir: str): self.resources_dir = resources_dir self.resource_paths = [] self.load_queue = queue.Queue() self.loaded_resources = [] self.num_threads = 4 def load_resources(self) -> List[godot.Resource]: self.resource_paths = self._get_resource_paths() self._fill_load_queue() self._start_loading_threads() return self.loaded_resources def _get_resource_paths(self) -> List[str]: resource_paths = [] for extension in ["*.png", "*.jpg", "*.wav", "*.mp3"]: resource_paths += glob.glob(os.path.join(self.resources_dir, extension)) return resource_paths def _fill_load_queue(self): for path in self.resource_paths: self.load_queue.put(path) def _start_loading_threads(self): for i in range(self.num_threads): t = threading.Thread(target=self._worker_thread) t.start() def _worker_thread(self): while True: path = self.load_queue.get() resource = godot.load(path) self.loaded_resources.append(resource) self.load_queue.task_done() if self.load_queue.empty(): break if __name__ == "__main__": resources_dir = "/path/to/resources" loader = ResourceLoader(resources_dir) start_time = time.monotonic() loaded_resources = loader.load_resources() end_time = time.monotonic() print(f"Loaded {len(loaded_resources)} resources in {end_time - start_time:.2f} seconds.") ``` 在这个示例中,我们首先定义了一个 `ResourceLoader` 类,它负责加载指定目录下的所有资源。该类使用了 Python 的 `threading` 模块来实现多线程加载资源,以加快加载速度。在 `load_resources` 方法中,我们首先调用 `_get_resource_paths` 方法获取所有资源的路径,然后通过 `_fill_load_queue` 方法将这些路径添加到一个 `queue.Queue` 对象中,以便我们可以轮流加载它们。然后,我们通过 `_start_loading_threads` 方法启动多个工作线程,这些工作线程将从队列中获取路径并加载资源。一旦资源被加载,它将被添加到 `loaded_resources` 列表中。当工作队列为空时,工作线程将退出。 在主程序中,我们首先创建一个 `ResourceLoader` 实例,并指定要加载的资源目录。然后,我们调用 `load_resources` 方法来加载所有资源。最后,我们打印出加载的资源数和加载所花费的时间。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值