GDScript:UIFramework V1.0

Relatively simple and crude way, no cache, which uses that is loaded. The first UI tscn are placed in a folder, or you can specify subfolders

extends Node

signal ui_opened(ui_name)

export(String, DIR) var ui_dir:String

func open(ui_name:String,msg:Dictionary = {},sub_dir:String= "/",suffix:String = ".tscn")->void:
	var ui:Node = null
	if has_node(ui_name) : 
		ui = get_node(ui_name)
	else:
		var scn  := load(ui_dir + sub_dir + ui_name + suffix) as PackedScene
		if scn != null and scn.can_instance():
			ui = scn.instance() as Node
			add_child(ui)
	if ui != null:
		move_child(ui,get_child_count() - 1)
		ui.name = ui_name
		if ui.has_method("open"):
			ui.open(msg)
		emit_signal("ui_opened",ui_name)
	else:
		Logger.error("Failed to open UI:" + ui_name)
	
func close(ui_name:String)->void:
	if has_node(ui_name):
		var ui := get_node(ui_name)
		remove_child(ui)
		ui.queue_free()

func close_all()->void: 
	for ui in get_children():
		remove_child(ui)
		ui.queue_free()


Published 272 original articles · won praise 134 · views 80000 +

Guess you like

Origin blog.csdn.net/hello_tute/article/details/104036298