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);
}
}