https://github.com/atlinx/godot-pluginassetsregistry
A GDScript that scales assets used by plugins (like images and fonts) automatically based on the editor's current UI scale.
https://github.com/atlinx/godot-pluginassetsregistry
Last synced: 3 months ago
JSON representation
A GDScript that scales assets used by plugins (like images and fonts) automatically based on the editor's current UI scale.
- Host: GitHub
- URL: https://github.com/atlinx/godot-pluginassetsregistry
- Owner: Atlinx
- Created: 2021-04-24T00:03:02.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2021-08-11T15:14:04.000Z (almost 5 years ago)
- Last Synced: 2025-02-01T04:28:03.140Z (over 1 year ago)
- Language: GDScript
- Homepage:
- Size: 188 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Godot Plugin Assets Registry
A GDScript that scales assets used by plugins (like images and fonts) automatically based on the editor's current UI scale.
This script keeps a record of all loaded assets and does not duplicate the same asset more than once, which saves on memory.
## Installation
1. Copy the `plugin_assets_registry.gd` into your plugin's folder
```
res://
└─ addons
└─ my_plugin
└─ plugin_assets_registry.gd <- COPY HERE
```
2. Change `PLUGIN_ABSOLUTE_PATH_PREFIX` to be the path to your plugin's folder
```GDScript
# Inside "plugin_assets_registry.gd"
class_name PluginAssetsRegistry
extends Reference
# Replace 'demo_plugin' with your plugin's name
const PLUGIN_ABSOLUTE_PATH_PREFIX = "res://addons/demo_plugin/"
...
```
3. Create a new instance of `PluginAssetsRegistry` inside of your main plugin script that extends `EditorPlugin`, and pass in your plugin inside of the `new()` constructor
```GDScript
# Inside "plugin.gd"
extends EditorPlugin
var assets_registry = PluginAssetsRegistry.new(self)
...
```
## Use
`assets_registry.load_asset(asset_path: String)`
- Loads a scaled asset using a path that is relative to the plugin folder. (ie `assets/fonts/font.tres` has a real path of `res://addons/my_plugin/assets/fonts/font.tres`, where `res://addons/my_plugin` is the plugin folder.)
`assets_registry.load_asset(asset)`
- Loads a scaled asset using an already loaded asset
## Recommended Use
You should create a `_setup_editor_assets(asset_registry)` function in all the node you wish to use scaled assets and then have the plugin.gd script call `_setup_editor_assets` on those nodes, with the `assets_registry` as the argument. This lets each script load the assets they need since they have access to the `PluginAssetsRegistry` instance.
ie.
```GDScript
# Inside "plugin.gd"
extends EditorPlugin
var assets_registry = PluginAssetsRegistry.new(self)
var plugin_ui : PluginUI
...
func _ready():
plugin_ui._setup_editor_assets(assets_registry)
...
```
```GDScript
# Inside "plugin_ui.gd"
class_name PluginUI
extends Control
var label: Label
var custom_button: CustomUISection
...
func _setup_editor_assets(assets_registry):
label.add_font_override("font", assets_registry.load_asset("assets/fonts/my_font.tres"))
# Chaining the _setup_editor_assets to child nodes
CustomUISection._setup_editor_assets(assets_registry)
...
```