From 36fbae00e2f49ade36fa05af7392792f5cc3fb72 Mon Sep 17 00:00:00 2001 From: Marcel Wiessler Date: Fri, 16 Feb 2024 23:03:28 +0800 Subject: [PATCH] add FileReference example --- .../code-samples/component-filereference.ts | 20 +++++++++++++++++++ documentation/scripting-examples.md | 6 ++++++ 2 files changed, 26 insertions(+) create mode 100644 documentation/.vuepress/public/code-samples/component-filereference.ts diff --git a/documentation/.vuepress/public/code-samples/component-filereference.ts b/documentation/.vuepress/public/code-samples/component-filereference.ts new file mode 100644 index 000000000..387ba54eb --- /dev/null +++ b/documentation/.vuepress/public/code-samples/component-filereference.ts @@ -0,0 +1,20 @@ +import { Behaviour, FileReference, ImageReference, serializable } from "@needle-tools/engine"; + +export class FileReferenceExample extends Behaviour { + + // A FileReference can be used to load and assign arbitrary data in the editor. You can use it to load images, audio, text files... FileReference types will not be saved inside as part of the GLB (the GLB will only contain a relative URL to the file) + @serializable(FileReference) + myFile?: FileReference; + // Tip: if you want to export and load an image (that is not part of your GLB) if you intent to add it to your HTML content for example you can use the ImageReference type instead of FileReference. It will be loaded as an image and you can use it as a source for an tag. + + async start() { + console.log("This is my file: ", this.myFile); + // load the file + const data = await this.myFile?.loadRaw(); + if (!data) { + console.error("Failed loading my file..."); + return; + } + console.log("Loaded my file. These are the bytes:", await data.arrayBuffer()); + } +} \ No newline at end of file diff --git a/documentation/scripting-examples.md b/documentation/scripting-examples.md index 007a2af24..3d47f3f99 100644 --- a/documentation/scripting-examples.md +++ b/documentation/scripting-examples.md @@ -131,6 +131,12 @@ For most usecases however you can use the core AudioSource component and don't h @[code](@code/component-2d-audio.ts) + +## Arbitrary external files + +Use the FileReference type to load external files (e.g. a json file) +@[code](@code/component-filereference.ts) +