Editing a script

I’ve got the following script which extracts files in a string from the streaming assets folder, and saves them to a persistent data path:

using UnityEngine;
using System.Collections;
using System.IO;

public class ObbExtractor : MonoBehaviour {
	
	void Start () {
		StartCoroutine(ExtractObbDatasets());
	}
	private IEnumerator ExtractObbDatasets () {
		string[] filesInOBB = {"Tracker_Name.dat", "Tracker_Name.xml"};
		foreach (var filename in filesInOBB) {
			string uri = "file://" + Application.streamingAssetsPath + "/QCAR/" + filename;
			
			string outputFilePath = Application.persistentDataPath + "/QCAR/" + filename;
			if(!Directory.Exists(Path.GetDirectoryName(outputFilePath)))
				Directory.CreateDirectory(Path.GetDirectoryName(outputFilePath));
			
			var www = new WWW(uri);
			yield return www;
			
			Save(www, outputFilePath);
			yield return new WaitForEndOfFrame();
		}
		// When done extracting the datasets, Start Vuforia AR scene
	}
	private void Save(WWW www, string outputPath) {
		File.WriteAllBytes(outputPath, www.bytes);
		
		// Verify that the File has been actually stored
		if(File.Exists(outputPath))
			Debug.LogError("File successfully saved at: " + outputPath);
		else
			Debug.LogError("Failure!! - File does not exist at: " + outputPath);
		
		Application.LoadLevel("SceneMenu1");
	}
}

However, it only saves the .dat file to the path; the first file in the string. So my plan was to identify, extract and save the files individually, as opposed to a string, to see if that worked. I tried this script:

using UnityEngine;
using System.Collections;
using System.IO;

public class ObbExtractor2 : MonoBehaviour {
	
	void Start () {
		StartCoroutine(ExtractObbDatasets());
	}
	private IEnumerator ExtractObbDatasets () {
			string XMLuri = "file://" + Application.streamingAssetsPath + "/QCAR/" + "Tracker_Name.xml";
			string XMLoutputFilePath = Application.persistentDataPath + "/QCAR/" + "Tracker_Name.xml";
			if(!Directory.Exists(Path.GetDirectoryName(XMLoutputFilePath)))
				Directory.CreateDirectory(Path.GetDirectoryName(XMLoutputFilePath));
			
		string DATuri = "file://" + Application.streamingAssetsPath + "/QCAR/" + "Tracker_Name.dat";
		string DAToutputFilePath = Application.persistentDataPath + "/QCAR/" + "Tracker_Name.dat";
		if(!Directory.Exists(Path.GetDirectoryName(DAToutputFilePath)))
			Directory.CreateDirectory(Path.GetDirectoryName(DAToutputFilePath));

		var XMLwww = new WWW(XMLuri);
		yield return XMLwww;
		Save(XMLwww, XMLoutputFilePath);
		yield return new WaitForEndOfFrame();
		
		var DATwww = new WWW(DATuri);
		yield return DATwww;
		Save(DATwww, DAToutputFilePath);
		yield return new WaitForEndOfFrame();	
		}
		// When done extracting the datasets, Start Vuforia AR scene

	private void Save(WWW www, string outputPath) {
		File.WriteAllBytes(outputPath, www.bytes);
		
		// Verify that the File has been actually stored
		if(File.Exists(outputPath))
			Debug.LogError("File successfully saved at: " + outputPath);
		else
			Debug.LogError("Failure!! - File does not exist at: " + outputPath);
		
		Application.LoadLevel("SceneMenu1");
	}
}

When I test it in the Editor, in the Console it only says the .xml file has been saved (the first one to be identified) Why is it only saved the one file?

Could anyone please help?

Hi. In the second code segment, your Save coroutine never breaks. Also it’s not a good idea to load levels from a coroutine. The reason you only save the first file is because the Application.LoadLevel destroys the script that’s running (or stops it or something). Put your Application.LoadLevel at the end of ExtractObbDatasets instead of Save.