Start a new topic
Solved

Creating trackable at runtime (SDK 8.4 in Unity 2019)

 I have created a small image tracker demo based on the Wikitude Unity Runtime tracker sample.


It firstly reads a QR code from a  Wikitude camera texture passed to the ZXing barcode library and saves a OBJ and WTC to the application persistent data path at the click of a "get QR" button.


It then loads the OBJ at runtime with the OBJ Import script from Unity user "aaro4130" and also loads the WTC as an external URL from the file.


After this, the image tracker is created and the OBJ is attached as a child GameObject


If I include instantiating and attaching a Wikitude trackable prefab to the image tracker along with the OBJ then both trackables load, otherwise the OBJ is not visible on its own.


In the docs, it specifies to add the trackable before "start()" is called on the tracker but I'm not sure if I'm obeying this condition in the code below:


OnQRButtonPressed() is called by a UI button

OnContentLoaded() is called by a second UI button

 

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using ZXing;
using Wikitude;

public class RuntimeTrackerController : SampleController
{

    public WikitudeCamera _wikitudeCamera1;

    public void OnQRButtonPressed()
    {
        Debug.Log("QR Button pressed");
        string pressTime1 = System.DateTime.Now.Ticks.ToString();

        _wikitudeCamera1 = FindObjectOfType<WikitudeCamera>();

        StartCoroutine(QRDecode(_wikitudeCamera1, pressTime1));

    }

    private IEnumerator QRDecode(WikitudeCamera wikiCam, string timeID)
    {

         Debug.Log("QR0");

        yield return new WaitForEndOfFrame();

        // string photoPath = Application.persistentDataPath + "/Photo" + timeID + ".png";

        RenderTexture _targetRenderTexture = new RenderTexture(Screen.width, Screen.height, 24);
        Texture2D _cameraFeed = new Texture2D(Screen.width, Screen.height);

        Debug.Log(wikiCam);

        Graphics.Blit(wikiCam.CameraTexture, _targetRenderTexture);
        RenderTexture.active = _targetRenderTexture;

        //   Debug.Log("QR2");

        _cameraFeed.ReadPixels(new Rect(0, 0, _cameraFeed.width, _cameraFeed.height), 0, 0);
        _cameraFeed.Apply();

        //   Debug.Log("QR3");

        IBarcodeReader QRReader = new BarcodeReader();

        var barcodeBitmap = _cameraFeed.GetPixels32();

        //   Debug.Log("QR4");

        var QRResult = QRReader.Decode(barcodeBitmap, _cameraFeed.width, _cameraFeed.height);
        // do something with the result
           Debug.Log("QR5");

        if (QRResult != null)
        {
            Debug.Log("not null");
            Debug.Log(QRResult.BarcodeFormat.ToString());
            Debug.Log(QRResult.Text);
            StartCoroutine(dlBIM(QRResult.Text));
    ///        OnLoadTracker();
        }
        else
        {
           Debug.Log("QRResult null");
        }

    }

    IEnumerator dlBIM(string file_name)
    {
        Debug.Log("file_name");
        Debug.Log(file_name);

        file_name = file_name.Remove(file_name.LastIndexOf('/') + 1);

        string url = file_name  + "knight.obj"; //file_name;
        using (UnityWebRequest www = UnityWebRequest.Get(url))
        {

            string savePathOBJ = string.Format("{0}/{1}", Application.persistentDataPath, "myfile.obj");

            string savePathWTC = string.Format("{0}/{1}", Application.persistentDataPath, "CAD_tracker.wtc");

            var dlHandler = new DownloadHandlerFile(savePathOBJ);
            dlHandler.removeFileOnAbort = true;
            www.downloadHandler = dlHandler;


            yield return www.SendWebRequest();
            if (www.isNetworkError || www.isHttpError)
            {
                Debug.Log(www.error);
            }
            else
            {
                //Debug.Log(www.downloadHandler.text);

                //string savePath = string.Format("{0}/{1}", Application.persistentDataPath, file_name_split);
                //System.IO.File.WriteAllText(savePath, www.downloadHandler.text);

                Debug.Log(Application.persistentDataPath);

                //dlHandler.isDone();

                /*

                GameObject trackableObject = OBJLoader.LoadOBJFile(savePathOBJ);

                GameObject trackerObject = new GameObject("ImageTracker1");
                _currentTracker = trackerObject.AddComponent<ImageTracker>();

                trackableObject.transform.SetParent(_currentTracker.transform, false);

                _currentTracker.TargetSourceType = TargetSourceType.TargetCollectionResource;
                _currentTracker.TargetCollectionResource = new TargetCollectionResource();
                _currentTracker.TargetCollectionResource.UseCustomURL = true;
                //savePathWTC = savePathWTC.Substring(1);
                _currentTracker.TargetCollectionResource.TargetPath = "file://" + savePathWTC;

                */
            }
        }

        string url1 = file_name + "CAD_tracker.wtc"; //file_name;
        using (UnityWebRequest www1 = UnityWebRequest.Get(url1))
        {
            string savePathWTC = string.Format("{0}/{1}", Application.persistentDataPath, "CAD_tracker.wtc");

            var dlHandler1 = new DownloadHandlerFile(savePathWTC);
            dlHandler1.removeFileOnAbort = true;
            www1.downloadHandler = dlHandler1;


            yield return www1.SendWebRequest();
            if (www1.isNetworkError || www1.isHttpError)
            {
                Debug.Log(www1.error);
            }
            else
            {
                Debug.Log("WTC downloaded");
            }
        }
    }

    /* UI control to specify the URL from which the TargetCollectionResource should be loaded. */
    //public InputField Url;
    public GameObject TrackablePrefab;
    public GameObject CarInstructions;

    private ImageTracker _currentTracker;
    /* Flag to keep track if a tracker is currently loading. */
    private bool _isLoadingTracker = false;

    public void onLoadContent() {

        string savePathOBJ = string.Format("{0}/{1}", Application.persistentDataPath, "myfile.obj");
        GameObject trackableObject1 = OBJLoader.LoadOBJFile(savePathOBJ);
        trackableObject1.transform.position = new Vector3(-31.8f, -7.5f, 24f);

        // trackableObject1.transform.SetParent(_currentTracker.transform, false);

        // GameObject trackableObject = GameObject.Instantiate(TrackablePrefab);

        // trackableObject.transform.SetParent(_currentTracker.transform, false);

        OnLoadTracker(trackableObject1);//, trackableObject);

    }

    public void OnLoadTracker(GameObject trackableObject01) { //, GameObject trackableObject0) {
        if (_isLoadingTracker) {
            /* Wait until previous request was completed. */
            return;
        }
        /* Destroy any previously loaded tracker. */
        if (_currentTracker != null) {
            Destroy(_currentTracker.gameObject);
        }

        _isLoadingTracker = true;

       // string savePathOBJ = string.Format("{0}/{1}", Application.persistentDataPath, "myfile.obj"); 
       // GameObject trackableObject1 = OBJLoader.LoadOBJFile(savePathOBJ);
       // trackableObject1.transform.position = new Vector3(-31.8f, -7.5f, 24f);

        /* Create and configure the tracker. */
        GameObject trackerObject = new GameObject("ImageTracker");
        _currentTracker = trackerObject.AddComponent<ImageTracker>();


        trackableObject01.transform.SetParent(_currentTracker.transform, false);
        //trackableObject0.transform.SetParent(_currentTracker.transform, false);


        _currentTracker.TargetSourceType = TargetSourceType.TargetCollectionResource;
        _currentTracker.TargetCollectionResource = new TargetCollectionResource();
        _currentTracker.TargetCollectionResource.UseCustomURL = true;
        _currentTracker.TargetCollectionResource.TargetPath = "file://" + string.Format("{0}/{1}", Application.persistentDataPath, "CAD_tracker.wtc"); // Url.text;

        _currentTracker.TargetCollectionResource.OnFinishLoading.AddListener(OnFinishLoading);
        _currentTracker.TargetCollectionResource.OnErrorLoading.AddListener(OnErrorLoading);

        _currentTracker.OnTargetsLoaded.AddListener(OnTargetsLoaded);

        //GameObject trackableObject = GameObject.Instantiate(TrackablePrefab);

        //trackableObject.transform.SetParent(_currentTracker.transform, false);

        _currentTracker.OnTargetsLoaded.AddListener(() => { OnTargetsLoaded1(trackableObject01); }); //, trackableObject0); });
        _currentTracker.OnErrorLoadingTargets.AddListener(OnErrorLoadingTargets);
        _currentTracker.OnInitializationError.AddListener(OnInitializationError);

        /* Create and configure the trackable. */
       // trackableObject = GameObject.Instantiate(TrackablePrefab);

       // trackableObject.transform.SetParent(_currentTracker.transform, false);
       // trackableObject1.transform.SetParent(_currentTracker.transform, false);
        //trackableObject.SetActive(false);
    }

    public override void OnTargetsLoaded() {
        base.OnTargetsLoaded();
        // CarInstructions.SetActive(true);
        //_trackableObject1.transform.SetParent(_currentTracker.transform, false);
        _isLoadingTracker = false;
    }

    public override void OnErrorLoadingTargets(Error error) {
        base.OnErrorLoadingTargets(error);
        _isLoadingTracker = false;
    }

    public void OnTargetsLoaded1(GameObject _trackableObject1) //, GameObject _trackableObject)
    {
       // base.OnTargetsLoaded();
        // CarInstructions.SetActive(true);
       // _trackableObject1.transform.SetParent(_currentTracker.transform, false);

      //  _trackableObject.SetActive(false);
        Debug.Log("eye active");
      //  Debug.Log(_trackableObject.activeSelf ? "Active" : "Inactive");
      //  _isLoadingTracker = false;
    }

}

 



 

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using ZXing;
using Wikitude;

public class RuntimeTrackerController : SampleController
{

    public WikitudeCamera _wikitudeCamera1;

    public void OnQRButtonPressed()
    {
        Debug.Log("QR Button pressed");
        string pressTime1 = System.DateTime.Now.Ticks.ToString();

        _wikitudeCamera1 = FindObjectOfType<WikitudeCamera>();

        StartCoroutine(QRDecode(_wikitudeCamera1, pressTime1));

    }

    private IEnumerator QRDecode(WikitudeCamera wikiCam, string timeID)
    {

         Debug.Log("QR0");

        yield return new WaitForEndOfFrame();

        // string photoPath = Application.persistentDataPath + "/Photo" + timeID + ".png";

        RenderTexture _targetRenderTexture = new RenderTexture(Screen.width, Screen.height, 24);
        Texture2D _cameraFeed = new Texture2D(Screen.width, Screen.height);

        Debug.Log(wikiCam);

        Graphics.Blit(wikiCam.CameraTexture, _targetRenderTexture);
        RenderTexture.active = _targetRenderTexture;

        //   Debug.Log("QR2");

        _cameraFeed.ReadPixels(new Rect(0, 0, _cameraFeed.width, _cameraFeed.height), 0, 0);
        _cameraFeed.Apply();

        //   Debug.Log("QR3");

        IBarcodeReader QRReader = new BarcodeReader();

        var barcodeBitmap = _cameraFeed.GetPixels32();

        //   Debug.Log("QR4");

        var QRResult = QRReader.Decode(barcodeBitmap, _cameraFeed.width, _cameraFeed.height);
        // do something with the result
           Debug.Log("QR5");

        if (QRResult != null)
        {
            Debug.Log("not null");
            Debug.Log(QRResult.BarcodeFormat.ToString());
            Debug.Log(QRResult.Text);
            StartCoroutine(dlBIM(QRResult.Text));
    ///        OnLoadTracker();
        }
        else
        {
           Debug.Log("QRResult null");
        }

    }

    IEnumerator dlBIM(string file_name)
    {
        Debug.Log("file_name");
        Debug.Log(file_name);

        file_name = file_name.Remove(file_name.LastIndexOf('/') + 1);

        string url = file_name  + "knight.obj"; //file_name;
        using (UnityWebRequest www = UnityWebRequest.Get(url))
        {

            string savePathOBJ = string.Format("{0}/{1}", Application.persistentDataPath, "myfile.obj");

            string savePathWTC = string.Format("{0}/{1}", Application.persistentDataPath, "CAD_tracker.wtc");

            var dlHandler = new DownloadHandlerFile(savePathOBJ);
            dlHandler.removeFileOnAbort = true;
            www.downloadHandler = dlHandler;


            yield return www.SendWebRequest();
            if (www.isNetworkError || www.isHttpError)
            {
                Debug.Log(www.error);
            }
            else
            {
                //Debug.Log(www.downloadHandler.text);

                //string savePath = string.Format("{0}/{1}", Application.persistentDataPath, file_name_split);
                //System.IO.File.WriteAllText(savePath, www.downloadHandler.text);

                Debug.Log(Application.persistentDataPath);

                //dlHandler.isDone();

                /*

                GameObject trackableObject = OBJLoader.LoadOBJFile(savePathOBJ);

                GameObject trackerObject = new GameObject("ImageTracker1");
                _currentTracker = trackerObject.AddComponent<ImageTracker>();

                trackableObject.transform.SetParent(_currentTracker.transform, false);

                _currentTracker.TargetSourceType = TargetSourceType.TargetCollectionResource;
                _currentTracker.TargetCollectionResource = new TargetCollectionResource();
                _currentTracker.TargetCollectionResource.UseCustomURL = true;
                //savePathWTC = savePathWTC.Substring(1);
                _currentTracker.TargetCollectionResource.TargetPath = "file://" + savePathWTC;

                */
            }
        }

        string url1 = file_name + "CAD_tracker.wtc"; //file_name;
        using (UnityWebRequest www1 = UnityWebRequest.Get(url1))
        {
            string savePathWTC = string.Format("{0}/{1}", Application.persistentDataPath, "CAD_tracker.wtc");

            var dlHandler1 = new DownloadHandlerFile(savePathWTC);
            dlHandler1.removeFileOnAbort = true;
            www1.downloadHandler = dlHandler1;


            yield return www1.SendWebRequest();
            if (www1.isNetworkError || www1.isHttpError)
            {
                Debug.Log(www1.error);
            }
            else
            {
                Debug.Log("WTC downloaded");
            }
        }
    }

    /* UI control to specify the URL from which the TargetCollectionResource should be loaded. */
    //public InputField Url;
    public GameObject TrackablePrefab;
    public GameObject CarInstructions;

    private ImageTracker _currentTracker;
    /* Flag to keep track if a tracker is currently loading. */
    private bool _isLoadingTracker = false;

    public void onLoadContent() {

        string savePathOBJ = string.Format("{0}/{1}", Application.persistentDataPath, "myfile.obj");
        GameObject trackableObject1 = OBJLoader.LoadOBJFile(savePathOBJ);
        trackableObject1.transform.position = new Vector3(-31.8f, -7.5f, 24f);

        // trackableObject1.transform.SetParent(_currentTracker.transform, false);

        // GameObject trackableObject = GameObject.Instantiate(TrackablePrefab);

        // trackableObject.transform.SetParent(_currentTracker.transform, false);

        OnLoadTracker(trackableObject1);//, trackableObject);

    }

    public void OnLoadTracker(GameObject trackableObject01) { //, GameObject trackableObject0) {
        if (_isLoadingTracker) {
            /* Wait until previous request was completed. */
            return;
        }
        /* Destroy any previously loaded tracker. */
        if (_currentTracker != null) {
            Destroy(_currentTracker.gameObject);
        }

        _isLoadingTracker = true;

       // string savePathOBJ = string.Format("{0}/{1}", Application.persistentDataPath, "myfile.obj"); 
       // GameObject trackableObject1 = OBJLoader.LoadOBJFile(savePathOBJ);
       // trackableObject1.transform.position = new Vector3(-31.8f, -7.5f, 24f);

        /* Create and configure the tracker. */
        GameObject trackerObject = new GameObject("ImageTracker");
        _currentTracker = trackerObject.AddComponent<ImageTracker>();


        trackableObject01.transform.SetParent(_currentTracker.transform, false);
        //trackableObject0.transform.SetParent(_currentTracker.transform, false);


        _currentTracker.TargetSourceType = TargetSourceType.TargetCollectionResource;
        _currentTracker.TargetCollectionResource = new TargetCollectionResource();
        _currentTracker.TargetCollectionResource.UseCustomURL = true;
        _currentTracker.TargetCollectionResource.TargetPath = "file://" + string.Format("{0}/{1}", Application.persistentDataPath, "CAD_tracker.wtc"); // Url.text;

        _currentTracker.TargetCollectionResource.OnFinishLoading.AddListener(OnFinishLoading);
        _currentTracker.TargetCollectionResource.OnErrorLoading.AddListener(OnErrorLoading);

        _currentTracker.OnTargetsLoaded.AddListener(OnTargetsLoaded);

        //GameObject trackableObject = GameObject.Instantiate(TrackablePrefab);

        //trackableObject.transform.SetParent(_currentTracker.transform, false);

        _currentTracker.OnTargetsLoaded.AddListener(() => { OnTargetsLoaded1(trackableObject01); }); //, trackableObject0); });
        _currentTracker.OnErrorLoadingTargets.AddListener(OnErrorLoadingTargets);
        _currentTracker.OnInitializationError.AddListener(OnInitializationError);

        /* Create and configure the trackable. */
       // trackableObject = GameObject.Instantiate(TrackablePrefab);

       // trackableObject.transform.SetParent(_currentTracker.transform, false);
       // trackableObject1.transform.SetParent(_currentTracker.transform, false);
        //trackableObject.SetActive(false);
    }

    public override void OnTargetsLoaded() {
        base.OnTargetsLoaded();
        // CarInstructions.SetActive(true);
        //_trackableObject1.transform.SetParent(_currentTracker.transform, false);
        _isLoadingTracker = false;
    }

    public override void OnErrorLoadingTargets(Error error) {
        base.OnErrorLoadingTargets(error);
        _isLoadingTracker = false;
    }

    public void OnTargetsLoaded1(GameObject _trackableObject1) //, GameObject _trackableObject)
    {
       // base.OnTargetsLoaded();
        // CarInstructions.SetActive(true);
       // _trackableObject1.transform.SetParent(_currentTracker.transform, false);

      //  _trackableObject.SetActive(false);
        Debug.Log("eye active");
      //  Debug.Log(_trackableObject.activeSelf ? "Active" : "Inactive");
      //  _isLoadingTracker = false;
    }

}

 


Hi, 


I apologize for the late reply.

Looking through the code, I'm not sure where the ImageTrackable is created. Is it part of the trackableObject01?


The hierarchy should be:

    ImageTracker

        ImageTrackable

            Augmentation (OBJ)


The ImageTrackable can be created at the same point as the ImageTracker.


Best regards,

Alexandru


1 person likes this

Hi Alexandru and thank you, I find the reply times to be perfectly fair for the associated product licence cost.


I had indeed forgot to create the "ImageTrackable" component which holds the actual object to be tracked!


As an aside, it's curious but generally not concerning that the OBJ model as GameObject seems to be added to the prefab trackable automatically without being declared in the code.


For anyone else who comes across this, the working (but badly written) code is below, note that two buttons trigger the separate QR code reading and runtime model loader and tracking functions:



 

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using ZXing;
using Wikitude;

public class RuntimeTrackerController : SampleController
{

    public WikitudeCamera _wikitudeCamera1;

    public void OnQRButtonPressed()
    {
        Debug.Log("QR Button pressed");
        string pressTime1 = System.DateTime.Now.Ticks.ToString();

        _wikitudeCamera1 = FindObjectOfType<WikitudeCamera>();

        StartCoroutine(QRDecode(_wikitudeCamera1, pressTime1));

    }

    private IEnumerator QRDecode(WikitudeCamera wikiCam, string timeID)
    {

         Debug.Log("QR0");

        yield return new WaitForEndOfFrame();

        // string photoPath = Application.persistentDataPath + "/Photo" + timeID + ".png";

        RenderTexture _targetRenderTexture = new RenderTexture(Screen.width, Screen.height, 24);
        Texture2D _cameraFeed = new Texture2D(Screen.width, Screen.height);

        Debug.Log(wikiCam);

        Graphics.Blit(wikiCam.CameraTexture, _targetRenderTexture);
        RenderTexture.active = _targetRenderTexture;

        //   Debug.Log("QR2");

        _cameraFeed.ReadPixels(new Rect(0, 0, _cameraFeed.width, _cameraFeed.height), 0, 0);
        _cameraFeed.Apply();

        //   Debug.Log("QR3");

        IBarcodeReader QRReader = new BarcodeReader();

        var barcodeBitmap = _cameraFeed.GetPixels32();

        //   Debug.Log("QR4");

        var QRResult = QRReader.Decode(barcodeBitmap, _cameraFeed.width, _cameraFeed.height);
        // do something with the result
           Debug.Log("QR5");

        if (QRResult != null)
        {
            Debug.Log("not null");
            Debug.Log(QRResult.BarcodeFormat.ToString());
            Debug.Log(QRResult.Text);
            StartCoroutine(dlBIM(QRResult.Text));
    ///        OnLoadTracker();
        }
        else
        {
           Debug.Log("QRResult null");
        }

    }

    IEnumerator dlBIM(string file_name)
    {
        Debug.Log("file_name");
        Debug.Log(file_name);

        file_name = file_name.Remove(file_name.LastIndexOf('/') + 1);

        string url = file_name  + "knight.obj"; //file_name;
        using (UnityWebRequest www = UnityWebRequest.Get(url))
        {

            string savePathOBJ = string.Format("{0}/{1}", Application.persistentDataPath, "myfile.obj");

            string savePathWTC = string.Format("{0}/{1}", Application.persistentDataPath, "CAD_tracker.wtc");

            var dlHandler = new DownloadHandlerFile(savePathOBJ);
            dlHandler.removeFileOnAbort = true;
            www.downloadHandler = dlHandler;


            yield return www.SendWebRequest();
            if (www.isNetworkError || www.isHttpError)
            {
                Debug.Log(www.error);
            }
            else
            {
                //Debug.Log(www.downloadHandler.text);

                //string savePath = string.Format("{0}/{1}", Application.persistentDataPath, file_name_split);
                //System.IO.File.WriteAllText(savePath, www.downloadHandler.text);

                Debug.Log(Application.persistentDataPath);

                //dlHandler.isDone();

                /*

                GameObject trackableObject = OBJLoader.LoadOBJFile(savePathOBJ);

                GameObject trackerObject = new GameObject("ImageTracker1");
                _currentTracker = trackerObject.AddComponent<ImageTracker>();

                trackableObject.transform.SetParent(_currentTracker.transform, false);

                _currentTracker.TargetSourceType = TargetSourceType.TargetCollectionResource;
                _currentTracker.TargetCollectionResource = new TargetCollectionResource();
                _currentTracker.TargetCollectionResource.UseCustomURL = true;
                //savePathWTC = savePathWTC.Substring(1);
                _currentTracker.TargetCollectionResource.TargetPath = "file://" + savePathWTC;

                */
            }
        }

        string url1 = file_name + "CAD_tracker.wtc"; //file_name;
        using (UnityWebRequest www1 = UnityWebRequest.Get(url1))
        {
            string savePathWTC = string.Format("{0}/{1}", Application.persistentDataPath, "CAD_tracker.wtc");

            var dlHandler1 = new DownloadHandlerFile(savePathWTC);
            dlHandler1.removeFileOnAbort = true;
            www1.downloadHandler = dlHandler1;


            yield return www1.SendWebRequest();
            if (www1.isNetworkError || www1.isHttpError)
            {
                Debug.Log(www1.error);
            }
            else
            {
                Debug.Log("WTC downloaded");
            }
        }
    }

    /* UI control to specify the URL from which the TargetCollectionResource should be loaded. */
    //public InputField Url;
    public GameObject TrackablePrefab;
    public GameObject CarInstructions;

    private ImageTracker _currentTracker;

    private ImageTrackable _objHolder;
    /* Flag to keep track if a tracker is currently loading. */
    private bool _isLoadingTracker = false;

    public void onLoadContent() {

        string savePathOBJ = string.Format("{0}/{1}", Application.persistentDataPath, "myfile.obj");
        GameObject trackableObject1x = OBJLoader.LoadOBJFile(savePathOBJ);
        trackableObject1x.transform.position = new Vector3(-31.8f, -7.5f, 24f);

        // trackableObject1.transform.SetParent(_currentTracker.transform, false);

        // GameObject trackableObject = GameObject.Instantiate(TrackablePrefab);

        // trackableObject.transform.SetParent(_currentTracker.transform, false);

        GameObject trackableHolder = new GameObject("OBJ_trackable");
        _objHolder = trackableHolder.AddComponent<ImageTrackable>();

        trackableObject1x.transform.SetParent(_objHolder.transform);

        OnLoadTracker(_objHolder); // trackableObject1);//, trackableObject);

    }

    public void OnLoadTracker(ImageTrackable trackableObject01) { //, GameObject trackableObject0) {
        if (_isLoadingTracker) {
            /* Wait until previous request was completed. */
            return;
        }
        /* Destroy any previously loaded tracker. */
        if (_currentTracker != null) {
            Destroy(_currentTracker.gameObject);
        }

        _isLoadingTracker = true;

       // string savePathOBJ = string.Format("{0}/{1}", Application.persistentDataPath, "myfile.obj"); 
       // GameObject trackableObject1 = OBJLoader.LoadOBJFile(savePathOBJ);
       // trackableObject1.transform.position = new Vector3(-31.8f, -7.5f, 24f);

        /* Create and configure the tracker. */
        GameObject trackerObject = new GameObject("ImageTracker");
        _currentTracker = trackerObject.AddComponent<ImageTracker>();


        trackableObject01.transform.SetParent(_currentTracker.transform, false);
        //trackableObject0.transform.SetParent(_currentTracker.transform, false);


        _currentTracker.TargetSourceType = TargetSourceType.TargetCollectionResource;
        _currentTracker.TargetCollectionResource = new TargetCollectionResource();
        _currentTracker.TargetCollectionResource.UseCustomURL = true;
        _currentTracker.TargetCollectionResource.TargetPath = "file://" + string.Format("{0}/{1}", Application.persistentDataPath, "CAD_tracker.wtc"); // Url.text;

        _currentTracker.TargetCollectionResource.OnFinishLoading.AddListener(OnFinishLoading);
        _currentTracker.TargetCollectionResource.OnErrorLoading.AddListener(OnErrorLoading);

        _currentTracker.OnTargetsLoaded.AddListener(OnTargetsLoaded);

        //GameObject trackableObject = GameObject.Instantiate(TrackablePrefab);

        //trackableObject.transform.SetParent(_currentTracker.transform, false);

        _currentTracker.OnTargetsLoaded.AddListener(() => { OnTargetsLoaded1(trackableObject01); }); //, trackableObject0); });
        _currentTracker.OnErrorLoadingTargets.AddListener(OnErrorLoadingTargets);
        _currentTracker.OnInitializationError.AddListener(OnInitializationError);

        /* Create and configure the trackable. */
       // trackableObject = GameObject.Instantiate(TrackablePrefab);

       // trackableObject.transform.SetParent(_currentTracker.transform, false);
       // trackableObject1.transform.SetParent(_currentTracker.transform, false);
        //trackableObject.SetActive(false);
    }

    public override void OnTargetsLoaded() {
        base.OnTargetsLoaded();
        // CarInstructions.SetActive(true);
        //_trackableObject1.transform.SetParent(_currentTracker.transform, false);
        _isLoadingTracker = false;
    }

    public override void OnErrorLoadingTargets(Error error) {
        base.OnErrorLoadingTargets(error);
        _isLoadingTracker = false;
    }

    public void OnTargetsLoaded1(ImageTrackable _trackableObject1) //, GameObject _trackableObject)
    {
       // base.OnTargetsLoaded();
        // CarInstructions.SetActive(true);
       // _trackableObject1.transform.SetParent(_currentTracker.transform, false);

      //  _trackableObject.SetActive(false);
        Debug.Log("eye active");
      //  Debug.Log(_trackableObject.activeSelf ? "Active" : "Inactive");
      //  _isLoadingTracker = false;
    }

}

 

Login or Signup to post a comment