当前位置:   article > 正文

godot4自定义按钮,适配boxcontainer中放置_godot 棋盘按钮

godot 棋盘按钮

godot4自定义按钮,适配boxcontainer中放置

实现代码如下:

extends Button
class_name CustomButton

@export var texture:Texture
@export var texture_image_count:int
@export var reload:bool = true :
	set(value):
		reload = value
		_setTexture()
	get:
		return reload
	
	
func _ready():
	_setTexture()


func _process(_delta):
	if(reload):
		_setTexture()

func _setTexture():
	if texture == null:
		return
	add_theme_stylebox_override("normal", _getOneImageInTexture(0))
	if texture_image_count >= 3:
		add_theme_stylebox_override("pressed", _getOneImageInTexture(1))
		add_theme_stylebox_override("hover", _getOneImageInTexture(2))
	else:
		add_theme_stylebox_override("pressed", StyleBoxEmpty.new())
		add_theme_stylebox_override("hover", StyleBoxEmpty.new())	
	
	if texture_image_count == 4:
		add_theme_stylebox_override("disabled", _getOneImageInTexture(3))
	else:
		add_theme_stylebox_override("disabled", StyleBoxEmpty.new())

	add_theme_stylebox_override("focus", StyleBoxEmpty.new())
	
	var width = texture.get_width() / texture_image_count	
	var height = texture.get_height()	
	size = Vector2(width, height)
	custom_minimum_size = Vector2(width, height)
		
func _getOneImageInTexture(number):
	var width = texture.get_width() / texture_image_count
	var height = texture.get_height()
	var style_box_texture = StyleBoxTexture.new()
	style_box_texture.texture = texture;
	var x = number * width
	style_box_texture.region_rect = Rect2(x, 0, width, height)
	return style_box_texture

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

界面添加自定义按钮:
在这里插入图片描述
运行效果:

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/111030
推荐阅读
相关标签
  

闽ICP备14008679号