AR/VR Buttons Question

Hi :wave::blush:

Is it possible to have a 3D object within a scene act as a AR or VR button.

I’ve had a good look at the ‘Custom XR Buttons’ info and sample but that doesn’t seem to cover what I’m hoping to achieve. Unless I’ve missed some obvious… which is entirely possible :sweat_smile:

It’s not a deal breaker if it can’t be done… but it would be cool if it can :sunglasses:

Cheeeeeeeeers, in advance, for any advice :slightly_smiling_face:

Original Post on Discord

by user 908977119781060648

Yep, you can, this should do the trick:

import { Behaviour, GameObject, IPointerDownHandler, WebXR } from "@needle-tools/engine";

// Documentation → https://docs.needle.tools/scripting

export class ClickToStartXR extends Behaviour  implements IPointerDownHandler{

    private btn: HTMLElement;

    start() {
        const xr = GameObject.findObjectOfType(WebXR);
        if (!xr) return;
        this.btn = WebXR.createARButton(xr);
    }

    onPointerDown() {
        if (!this.btn) return;
        this.btn.click();
    }
}

Make sure to turn off the regular button creation, otherwise they’re double

Fantastic! :star_struck:

Huge thanks for that!!! :partying_face: :rocket:

by user 908977119781060648

So, just curious… on a similar matter…

If I wanted a 3D object to act as clickable to launch a Quicklook experience, would that also be possible with Needle?

In the past when I’ve done so (in 2D) on web pages just using html I’ve achieved it with…

  <a href="https://www.cartoonfun.co.uk/name_of.reality" rel="ar">
    <img />
    <img style="max-width: 20px;" src="assets/ios_ARBtn_icon.png">View in your space (iOS) </a>
</div>```

Just wondering if there's a way to do something equivalent with Needle?

*by user 908977119781060648*

Yep almost the same,

private exporter: USDZExporter;
...
this.exporter = GameObject.findObjectOfType(USDZExporter);
...
this.exporter.exportAsync();

We’re handling that <a rel="ar"><img/></a> dance internally in the USDZExporter

Aha! :eyes: Ingenius! :star_struck:

Thank you!!!

by user 908977119781060648

So, can I launch an external, pre-existing usdz (or .reality file) with that?

by user 908977119781060648

By default it will export the part of your scene where USDZExporter is on (or the entire scene if nothing is assigned)

But yes, you can also use a pre-existing file by dragging that into the USDZExporter in Unity into the “custom USDZ” field

Aaand I’d like to know the reason for using a pre-existing file so that we can improve our conversion :slightly_smiling_face:

Oooooooo, that sounds cool. Cheers for the info!

For ref… I doubt I’ll ever have an occasion where I’d need to use an external USDZ (I’m hoping not anyway). My curiosity was primarily spurred by wanting to be prepared to cover any eventualities, such as (for example) a compiled/exported USDZ not being able to match the functionality of an equivalent one that’s been specifically created in Reality Composer.

It doesn’t seem that what I’ve described would ever actually be an issue though, as (a) the kind of scenes which I’m creating are quite simple and (b) the Needle USDZExporter appears to output a very sophisticated version of things :grinning: :+1:

by user 908977119781060648

a compiled/exported USDZ not being able to match the functionality of an equivalent one that’s been specifically created in Reality Composer.

If you ever run into this please let me know - our goal is that we’re at least matching what Reality Composer can output for common cases. There are some areas we didn’t touch yet (such as scene switching in RC) but in terms of interactive behaviours I think we have pretty good coverage

It certainly looks like you’ve nailed it from what I’ve seen thus far :star_struck: :raised_hands:

by user 908977119781060648

Okay, sorry but I’m not having any luck with creating a ‘launch USDZ/Quicklook via a clickable 3D object’.

Based on the previous guidance, I’ve cobbled together the following script (ClickToStartARiOS.ts - the latest of several tries :hot_face: ) but I’m obviously getting various things wrong as none of it seems to work :grimacing:



export class ClickToStartARiOS extends Behaviour  implements IPointerDownHandler{

    private btn: HTMLElement;
    private exporter: USDZExporter;

    start() {
        this.exporter = GameObject.findObjectOfType(USDZExporter);
    }

    onPointerDown() {
        if (!this.btn || !this.exporter) return;
        this.btn.click();

        this.exporter.exportAsync();
    }
}```

Sadly, I'm unable to spot where the problem lies.

If there's a fix for the shambles that I've made I'd be eternally grateful!!! 🙏

*by user 908977119781060648*

Remove the btn lines

The “private…” and especially the part in the if and the click

Sorry, should have made that more explicit

No, seriously the failing is mine not yours, I kid you not!

Cheeeeeeers… and I’ll try again :slightly_smiling_face:

by user 908977119781060648