Skip to content

Commit

Permalink
add FileReference example
Browse files Browse the repository at this point in the history
  • Loading branch information
marwie committed Feb 16, 2024
1 parent 4fdade9 commit 36fbae0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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 <img> 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());
}
}
6 changes: 6 additions & 0 deletions documentation/scripting-examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

<!-- SAMPLE receive click from HTML button
## Receiving html element click in component
-->
Expand Down

0 comments on commit 36fbae0

Please sign in to comment.