Start a new topic

Getting content of online wtc file with Unity

Hi!


I generated a wtc file, placed it on my server and tried to read it with the wikitude sample scene "Runtime Tracker" in Unity 2020.3. It works - that's great.


There are several target pictures in the wtc file, but how can I instantiate different image trackables for it? At the moment, every marker has the same augmentation. I would like to seperate these markers into different kinds of what should happen: play a sound, play a video, show a 3D model …


- How can I get the number of targets in the wtc file?

- How do I get all the names of the targets?

- Can I create a loop over all the targets to choose what Prefab the target should load?


Have a nice weekend!

Björn



Hi Björn,


glad that it works now! I am not a 100% sure if during an apps lifetime there is some caching going on, actually... But that's something that you can validate very easily I guess, if needed.


There is also the API documentation located here. This is a great source to learn more about the public API of the Unity plugin.



Kind regards,

Gökhan

Hi Gökhan,

I just found a solution for storing the wtc file on a device locally.

You can access it like this:

file:///Users/myuser/Library/Application Support/company/App-ID/example.wtc


Otherwise with the wtc file on a server it will be donwloaded every time the ImageTracker gets active, right?


Kind regards

Björn



By the way: I tried to learn about "use custom URL" in the Wikitude documentation, but I can't find entries for this topic! In the FAQ section: no results. Only far back in the forum section there was the helping post.

Hi Björn,

The AddListener method requires a method/delegate of type Wikitude.ImageTrackable.OnImageRecognizedEvent and the VideoMarkerGefundenFuerPrefabs is convertable to that, because it has the required signature.


The thing with the event callbacks not visible in the Editor is something known in Unity. There is this method to do that but I never tried that.


I don't know by heart which path type works but it should be the first one you sent... assuming you are on MacOS... But the file has to be saved at this position before you pass it to the Wikitude SDK.


Kind regards,

Gökhan

Yes, that works, thank you.


A bit confusing, that I write


imageTrackable.OnImageRecognized.AddListener(myMethod);

 

without "(aTarget)", and the method knows the target nevertheless

 

public void VideoMarkerGefundenFuerPrefabs(ImageTarget target)
{
    Debug.Log("Statische Fkt. aufgerufen für target " + target.Name);
}

 

Is it right, that the new listener does not appear in the event list in Unity's inspector?

I took a screenshot, there was no event added, but it was executed!




By the way: can I store the wtc file at Unity's "Application.persistentDataPath" and how should be the ustom URL in this case?


Following URL's didin't work:

/Users/myuser/Library/Application Support/company/App-ID/example.wtc

//Users/myuser/Library/Application Support/company/App-ID/example.wtc

file://Users/myuser/Library/Application Support/company/App-ID/example.wtc

example.wtc


Kind regards

Björn




pdf

I think you are on the right track already... you can't add a ObjectTrackable's event to another event though... You need to pass a valid method to the AddListener method.

So in that case you have to link a method that has this kind of signature:


public void SomeMethodFromSomeComponent(ImageTarget target) { }


Then, if your imageTrackable object finds something it will call this method... it can't call another ImageTrackable's event though with that.


If you want to add something to the persistant ImageTrackable's event you can do that similarly with:


masterTrackable.OnImageRecognized.AddListener(SomeMethodFromSomeComponent);


Kind regards,
Gökhan

Hi!


I just tried a few attempts with your proposal, but I didn't succeed.

Your example was for ObjectTracker, I changed it for ImageTracking like the auto completion suggested.


In my Scene there is a MASTER trackable active from which I'd like to take the recognition and lost events. So this trackable should be my "FindObjectOfType<YourCustomComponentYouWantToLinkTo>()"?


If I try the following line 

imageTrackable.OnImageRecognized.AddListener(masterTrackable.OnImageRecognized); // masterTrackable is an ImageTrackable

 

then this error occurs:

Argument 1: cannot convert from 'Wikitude.ImageTrackable.OnImageRecognizedEvent' to 'UnityEngine.Events.UnityAction<Wikitude.ImageTarget>'


Maybe you can explain your code lines

var componentToLinkTo = FindObjectOfType<YourCustomComponentYouWantToLinkTo>();

trackable.OnObjectRecognized.AddListener(componentToLinkTo.OnObjectRecognized);


and especially what

YourCustomComponentYouWantToLinkTo

means respectively what it stands for!?


Thank you!

Björn


Hi Björn,

it could be that the trackable prefab didn't save references from the scene. You can link the events also on your own. Could do it with a custom component on the ObjectTrackable like this:


    public class TrackableEventLinker : MonoBehaviour 
    {
        void Start()
        {
            var trackable = GetComponent<ObjectTrackable>();
            var componentToLinkTo = FindObjectOfType<YourCustomComponentYouWantToLinkTo>();
            trackable.OnObjectRecognized.AddListener(componentToLinkTo.OnObjectRecognized);
            trackable.OnObjectLost.AddListener(componentToLinkTo.OnObjectLost);
        }
    }


Hi Gökhan,


thank you for your reply. I'am looking for a new solution now.

Therefore I instantiate all my ImageTrackables at scene opening. Unfortunately the trackable prefab doesn't remember my OnImageRecognized and OnImageLost events. How can I add my script functions from the main script (added to the ImageTracker) to a new Clone in C# and Unity? Do you have an example for this, too?


Kind regards

Björn


Hi Björn,

unfortunately, there is no public API to retrieve the number of targets in a target collection or the names of the targets.


I am not sure if you are referring to the Expert Edition or the Professional Edition "Runtime Tracker" sample, but in the Expert Edition one, we have a sample, that showcases how to use config files instead of direct links to a target collection. In that specific sample scene, for instance, a config json file will be loaded (the default URL in the scene is pointing to this one). That config file holds the augmentations as AssetBundles (for each supported platform), as well as the target collection for tracking and the thumbnail for the target(s)... You can create something similar, holding information about targets and their names and which prefabs should be used. You could also use AssetBundles for each of the targets or hold a key to reference a GameObject available in the scene...


Kind regards,

Gökhan

Login or Signup to post a comment