How can I load a json file and create the image targets on runtime?

0 votes
asked Aug 22, 2017 by hernanipawlick (220 points)
Hello,

I'm trying to create a multitarget experience on runtime so first I made a batch script to create a json file with all images on a folder. Next I wanted to load a zip with the files and the json . Somehow I got this working but I was wondering if there is a better implementation inside the SDK, as you can see had to create a struct to be able to iterate the json, and that takes 6 seconds on my phone.

Is there anyway to do the same thing without the need of creating a struct? In another words, can I load a json with multitargets and automatically load all targets?

using System.Collections;
using UnityEngine;
using EasyAR;
using System.IO;
using UnityEngine.UI;

public class CreateTargets : MonoBehaviour
{
    #region PUBLIC FIELDS

    public string jsonPath = string.Empty;
    public StorageType storageType = StorageType.Assets;

    [Space]

    public bool autoStart = false;

    #endregion

    private EasyARMarker[] easyARMarkers;
    private string filePath = string.Empty;
    private string result = string.Empty;
    private bool isDataLoaded = false;

    ImageTrackerBehaviour imageTrackerBehaviour;

    private void Awake()
    {
        if (autoStart)
        {
            StartTracker();
        }
    }

    public void StartTracker()
    {
        CreateImageTracker();

        filePath =Path.Combine(Application.streamingAssetsPath, jsonPath);
        StartCoroutine(LoadFile());
    }

    private void CreateImageTracker()
    {
        GameObject imageTracker = new GameObject("ImageTracker");
        imageTracker.transform.SetParent(Camera.main.transform.parent);
        imageTrackerBehaviour = imageTracker.AddComponent<ImageTrackerBehaviour>();
        imageTrackerBehaviour.StartTrack();
    }

    IEnumerator LoadFile()
    {

        if (filePath.Contains("://"))
        {
            WWW www = new WWW(filePath);        
            yield return www;
            result = www.text;
        }
        else
        {

            result = System.IO.File.ReadAllText(filePath);
        }

        isDataLoaded = true;
    }

    private void Start()
    {
        StartCoroutine(LoadJsonData());
    }

    IEnumerator LoadJsonData()
    {
        while (!isDataLoaded)
        {
            yield return null;
        }

        string jsonData = result;

        Debug.Log(jsonData);

        EasyARMarker easyARMarker = EasyARMarker.CreateFromJSON(jsonData);
        foreach (var image in easyARMarker.images)
        {
            Debug.Log(image.image);
            Debug.Log(image.name);

            CreateImageTargets(jsonData, image.image, image.name);
        }
    }

    private void CreateImageTargets(string json, string image, string name)
    {   

        ImageTargetBehaviour imageTargetBehaviour;

        GameObject imageTarget = new GameObject(name);

        imageTargetBehaviour = imageTarget.AddComponent<ImageTargetBehaviour>();

        imageTargetBehaviour.SetupWithJsonString(json, storageType, name);
        imageTargetBehaviour.Bind(imageTrackerBehaviour);

        //Test
        GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
        cube.transform.SetParent(imageTargetBehaviour.transform);
       
    }
}

[System.Serializable]
public struct EasyARMarker
{
    [System.Serializable]
    public struct Images
    {
        public string image;
        public string name;
    }

    public Images[] images;

    public static EasyARMarker CreateFromJSON(string jsonString)
    {
        return JsonUtility.FromJson<EasyARMarker>(jsonString);
    }
}

1 Answer

0 votes
answered Sep 7, 2017 by kenn (18,750 points)
Do you mean load a json and all targets inside json will be loaded? There is such a interface. You can reference HelloARTarget sample. For short, you need to use ImageTargetBaseBehaviour.LoadListFromJsonFile, https://www.easyar.com/doc/EasyAR%20SDK/Unity%20Plugin%20Reference/2.0/ImageTargetBaseBehaviour.html#static-list-imagetarget-loadlistfromjsonfile-string-path-storagetype-storagetype

And more, you can find more explanations for target json file from https://www.easyar.com/doc/EasyAR%20SDK/Guides/EasyAR-Target-Configure.html
Welcome to EasyAR SDK Q&A, where you can ask questions and receive answers from other members of the community.
...