Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | newold |
I have created a plugin in which I can drag and drop a scene over a control that should display a texture previewing it but I haven’t managed to do it yet.
In my plugin.gd I have this:
var resource_previewer = get_editor_interface().get_resource_previewer()
and assign it to my main scene.
my_scene.set_resource_previewer(resource_previewer)
Then, when I drag and drop a scene into the control, I use this command:
var res = ResourceLoader.load(scene_that_should_be_previewed)
resource_previewer.queue_resource_preview(res, my_scene, "my_function", null)
That command is supposed to cause the resource texture to be created and the “my_function” function in my scene to be called, but this function is never called.
Do you have any idea how to get the scene previewed as a texture?
This would be my code:
Plugin.gd
tool
extends EditorPlugin
var _interface
func _ready() -> void:
make_visible(false)
func _enter_tree() -> void:
# Set interface
_interface = preload("res://addons/MyAddon/MyScene.tscn").instance()
# Add interface to editor viewport
get_editor_interface().get_editor_viewport().add_child(_interface)
# Add resource_previewer to interface
var e = get_editor_interface().get_resource_previewer()
_interface.set_editor_resource_preview_generator(e)
func _exit_tree() -> void:
if _interface: _interface.queue_free()
func has_main_screen() -> bool:
return true
func get_plugin_name() -> String:
return("My Plugin")
func get_plugin_icon() -> Texture:
return load("my_icon") as Texture
func make_visible(visible: bool) -> void:
if _interface:
_interface.visible = visible
My Scene:
tool
extends TextureRect
var editor_resource_preview_generator
func set_editor_resource_preview_generator(editor) -> void:
editor_resource_preview_generator = editor
func can_drop_data(position, data):
return true
func drop_data(position: Vector2, data : Dictionary) -> void:
var obj = ResourceLoader.load(data.files[0])
if obj is PackedScene:
editor_resource_preview_generator.queue_resource_preview(obj, self, "_set_resource_preview", null)
func _set_resource_preview(path : String, preview : Texture, thumbnail : Texture, nothing) -> void:
texture = thumbnail
print("Works!!")