[godot] Add support for load from disk for Godot 3.x and example

This commit is contained in:
Mario Zechner 2024-10-07 08:20:38 +02:00
parent e4999922b6
commit d4a3444328
3 changed files with 46 additions and 12 deletions

View File

@ -0,0 +1,24 @@
extends Node2D
func _ready():
# Load the skeleton file
var skeleton_file_res = SpineSkeletonFileResource.new();
skeleton_file_res.load_from_file("/Users/badlogic/workspaces/spine-runtimes/examples/coin/export/coin-pro.skel");
# Load the atlas file
var atlas_res = SpineAtlasResource.new();
atlas_res.load_from_atlas_file("/Users/badlogic/workspaces/spine-runtimes/examples/coin/export/coin.atlas");
# Create a skeleton data resource, you can share this across multiple sprites
var skeleton_data_res = SpineSkeletonDataResource.new();
skeleton_data_res.skeleton_file_res = skeleton_file_res;
skeleton_data_res.atlas_res = atlas_res
# Create a sprite from the skeleton data and add it as a child
var sprite = SpineSprite.new();
sprite.skeleton_data_res = skeleton_data_res;
sprite.position.x = 200;
sprite.position.y = 200;
sprite.get_animation_state().set_animation("animation", true, 0);
self.add_child(sprite)
pass

View File

@ -0,0 +1,6 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://examples/13-load-from-disk/load_from_disk.gd" type="Script" id=1]
[node name="load_from_disk" type="Node2D"]
script = ExtResource( 1 )

View File

@ -30,11 +30,16 @@
#include "SpineAtlasResource.h" #include "SpineAtlasResource.h"
#include "SpineRendererObject.h" #include "SpineRendererObject.h"
#include "core/io/json.h" #include "core/io/json.h"
#include "core/io/image.h"
#include "scene/resources/image_texture.h"
#include "scene/resources/texture.h" #include "scene/resources/texture.h"
#include <spine/TextureLoader.h> #include <spine/TextureLoader.h>
#if VERSION_MAJOR > 3
#include "core/io/image.h"
#include "scene/resources/image_texture.h"
#else
#include "core/image.h"
#endif
#ifdef TOOLS_ENABLED #ifdef TOOLS_ENABLED
#include "editor/editor_file_system.h" #include "editor/editor_file_system.h"
#endif #endif
@ -90,22 +95,21 @@ public:
Vector<uint8_t> buf = FileAccess::get_file_as_array(path, &error); Vector<uint8_t> buf = FileAccess::get_file_as_array(path, &error);
if (error == OK) { if (error == OK) {
Ref<Image> img; Ref<Image> img;
img.instantiate(); INSTANTIATE(img);
String filename = path.get_filename().to_lower(); img->load(path);
if (filename.ends_with(".png")) {
img->load_png_from_buffer(buf); Ref<ImageTexture> texture;
} else if (filename_lower.ends_with(".jpg")) { INSTANTIATE(texture);
img->load_jpg_from_buffer(buf); texture->create_from_image(img);
} return texture;
return ImageTexture::create_from_image(img);
} }
return Ref<Texture>();
} }
return Ref<Texture>();
} }
#endif #endif
void import_image_resource(const String &path) { void import_image_resource(const String &path) {
#ifdef VERSION_MAJOR> 4 #if VERSION_MAJOR > 4
#ifdef TOOLS_ENABLED #ifdef TOOLS_ENABLED
// Required when importing into editor by e.g. drag & drop. The .png files // Required when importing into editor by e.g. drag & drop. The .png files
// of the atlas might not have been imported yet. // of the atlas might not have been imported yet.