Hello! Say, I set up a scene with 10 buttons in Unity.
Is it possible to hook onto the different buttons and execute javascript onClick code for them?
I exported the sample for React but it seems to be all wrapped in NeedleEngine- As a simple example, how would I go about prompting a javascript alert when I pick up the trash can object?
Hello there – we have an experimental AI bot that might be able to help you with your question. Would you like to try this out?
How it works:
Reply to the messages of the bot that end with a thread_id to interact with it and ask follow-up questions. You can also mention the bot directly in this channel to get help.
Yes, it is possible to execute JavaScript code when interacting with buttons or objects in Needle Engine. When working with React, you can still use the Needle Engine API to interact with your scene’s elements and attach event listeners.
For prompting a JavaScript alert when you pick up a trash can object, you would typically use Needle Engine’s event system, specifically by implementing the IPointerEventHandler interface on a component attached to the trash can object.
The process would be something like this:
Create a custom component that extends Behaviour and implements the IPointerEventHandler interface.
In the start method of that component, ensure that an ObjectRaycaster is present in the parent hierarchy or add one if it’s missing.
Implement the onPointerDown method to execute the JavaScript alert function.
Attach this component to the trash can object in Unity before exporting.
Once the scene is exported and loaded in the browser, picking up the trash can object should trigger the alert.
Here is a simple example component in TypeScript:
import { Behaviour, ObjectRaycaster, IPointerEventHandler } from '@needle-tools/engine';
import { serializable } from '@needle-tools/engine/engine.serialization-decorators';
export class TrashCanPickup extends Behaviour implements IPointerEventHandler {
awake() {
this.gameObject.getOrAddComponent(ObjectRaycaster);
}
onPointerDown(event) {
alert('Picked up the trash can!');
}
}
Yes it will - we have samples for those cases and remember: Unity is only used to setup the scene but once you’re in the browser you’re working with a web engine just like you would with react three fiber, playcanvas or others - so you can import and export everything from packages the same way.