Actually, you may not need to call DestroyDataSet if you are swapping scenes, the datasets should be destroyed automatically.
I just tested a setup that I think is similar to yours. Here is what I did:
Scene 1 - Camera with the following script attached:
using UnityEngine;
using System.Collections;
public class DownloadDataset : MonoBehaviour
{
void Start()
{
string myurl = ...
string mydataset = "myDataSet";
StartCoroutine(DownloadAndSave(myurl, mydataset + ".xml"));
StartCoroutine(DownloadAndSave(myurl, mydataset + ".dat"));
}
IEnumerator DownloadAndSave(string url, string filename)
{
string remoteFile = url + "/" + filename;
string localFile = Application.persistentDataPath + "/" + filename;
Debug.Log("remoteFile: " + remoteFile);
Debug.Log("localFile: " + localFile);
if (System.IO.File.Exists(localFile))
{
Debug.Log("Exists: " + localFile);
System.IO.File.Delete(localFile);
Debug.Log("Deleted: " + localFile);
}
WWW wwwdat = new WWW(remoteFile);
yield return wwwdat;
System.IO.File.WriteAllBytes(localFile, wwwdat.bytes);
Debug.Log("Cache saved: " + localFile);
Debug.Log("file download is done");
if (System.IO.File.Exists(localFile))
{
Debug.Log(" file does exist");
}
else
{
Debug.Log(" file does not exist");
}
}
public void OnGUI ()
{
if (GUI.Button(new Rect(20,40,100,100), "AR"))
{
Application.LoadLevel(1);
}
}
}
Scene 2 - ARCamera with the following script attached:
using UnityEngine;
using System.Collections;
public class LoadDataset : MonoBehaviour
{
void Start()
{
string myDataPath = Application.persistentDataPath + "/myDataSet.xml";
LoadAndActivateDataSet(myDataPath);
}
void LoadAndActivateDataSet(string dataSetPath)
{
if (TrackerManager.Instance.GetTracker(Tracker.Type.IMAGE_TRACKER) == null)
{
TrackerManager.Instance.InitTracker(Tracker.Type.IMAGE_TRACKER);
}
if (!DataSet.Exists(dataSetPath, DataSet.StorageType.STORAGE_ABSOLUTE))
{
Debug.LogError("Data set " + dataSetPath + " does not exist.");
return;
}
ImageTracker imageTracker = (ImageTracker)TrackerManager.Instance.GetTracker(Tracker.Type.IMAGE_TRACKER);
DataSet dataSet = imageTracker.CreateDataSet();
if (!dataSet.Load(dataSetPath,DataSet.StorageType.STORAGE_ABSOLUTE))
{
Debug.LogError("Failed to load data set " + dataSetPath + ".");
return;
}
for (int i = 0; i < dataSet.GetNumTrackables(); ++i)
{
DataSetTrackableBehaviour dstb = dataSet.GetTrackable(i);
GameObject go = dstb.gameObject;
go.AddComponent<DefaultTrackableEventHandler>();
}
imageTracker.ActivateDataSet(dataSet);
}
public void OnGUI ()
{
if (GUI.Button(new Rect(20,40,100,100), "Main"))
{
Application.LoadLevel(0);
}
}
}
After loading the AR scene I switched the dataset files on my server and went back to the first scene. When I loaded the AR scene again the log file showed that the correct trackables were loaded.
Is this what you are trying to do?
- Kim
Okay, I can confirm that this doesn't work consistently on an iPad running iOS 5.0.1.
It works every time on an iPhone 4 running 4.3.5.
I'm guessing there's an issue with overwriting the file on the iPad. Perhaps the file is locked from the previous read. I'll investigate more once I have a chance.
- Kim