How can I load a prefab scene or object that includes its original scripts and colliders in needle-engine? Also, what’s the process for exporting an asset and importing it into another needle-engine project?
Hi oot4chi!
To load a prefab or scene with its original scripts and colliders in Needle Engine, you will need to use AssetReference
in your scripts. Here’s a step-by-step outline followed by a code example:
-
Export the Asset: Ensure you have your prefab or scene created in Unity and export it to a glTF format as an asset reference.
-
Declare AssetReference: In your Needle Engine component, create a property of type
AssetReference
for the prefab. -
Instantiate the Prefab: Use the methods provided by Needle Engine to instantiate or load your prefab in the
start()
method of your script.
Example Code
Here’s how you can implement this in your TypeScript code:
import { Behaviour, serializable, AssetReference } from "@needle-tools/engine";
export class MyPrefabLoader extends Behaviour {
@serializable(AssetReference)
myPrefab?: AssetReference;
async start() {
// Load and instantiate the prefab
const instance = await this.myPrefab?.instantiate();
if (instance) {
console.log("Prefab instantiated successfully!", instance);
this.gameObject.add(instance);
} else {
console.error("Failed to instantiate prefab.");
}
}
}
Exporting and Importing Assets
To export an asset and import it into another Needle Engine project:
-
Add
ExportInfo
Component: In Unity, add theExportInfo
component to your scene. This will set up the project for exporting. -
Export as GLTF: When saving the scene, ensure the prefab is saved as a glTF file. You can modify the
GltfObject
component to manage this. -
Re-import in Another Project: Use the same
AssetReference
setup in your new project, referencing the previously exported glTF file.
Refer to the documentation for further details about exporting assets and managing prefabs.
Feel free to ask if you need more information on any of these steps!
1 replies left - Get a PRO license for more AI replies.
thread_pmgQiE5bEaoOO6pIs8vAnvNv
Reply to this message to continue the conversation
The bot’s reply is correct
OR if you already have an URL this is a quick way to create a AssetReference from code (using an URL to a glTF or GLB file)
import { AssetReference } from "@needle-tools/engine"
const assetref = AssetReference.createFromURL("https://cloud.needle.tools/-/assets/Z23hmXBZ1Y5yHp-Z1Y5yHp/file");
// and instantiate (returns a promise, so make sure to await it)
const instance = await assetref.instantiate(<parent>);
Note that script data is included in the glTF but not actual code (same as with Unity’s AssetBundles for example)
How process exporting prefab to asset file for load with “AssetReference.createFromURL()”?
(collider/materials/shader/light/script reference/etc include ?)
How do script work?
Need Register types/script in javascript like register_types.ts?
All exported prefab assets and scene assets are exported to glTF with components and the data you mentioned already when using Needle Engine (like Needle Asset Bundles if that makes more sense).
You can use our normal web dev workflow with the local server and get the files from there
or context click any part of your scene hierarchy OR scene or prefab asset to export those files.
Checkout Creating and using Components | Needle Engine Documentation
Can I export individual prefabs separately, or do I have to export the entire scene/all prefabs at once?