- Home /
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?
Im sorry, forgot to post my code. Let me post my code here. Hold on please :) Edit: Dont have the files here. after work ill try
And yet again I forgot about it, im re-program$$anonymous$$g an app so I keep forgetting it, my appologies! I've just putted it in my agenda so I wont forget about it. Ill be at home at around 6 P$$anonymous$$ CET
Note: If the scripts only works for the first file. Its not a good solution but you can make an empty gameObject for each .X$$anonymous$$L and .DAT file, put in the script on each of them with the correct information(make it a public variable so you can change it in the hierachery).
Sorry, you've been able to solve this problem? because now I also encountered the same eh! In my case I created the files on the computer, but in the dispositovo (tablet) I created the files but empty bone 0 $$anonymous$$B
Answer by smoggach · Oct 24, 2014 at 01:12 PM
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.
Exactly, Application.LoadLevel will wipe out all gameobjects in the scene including the scripts attached and the coroutines running on them. The first script would work just find without that LoadLevel.
Your answer
![](https://koobas.hobune.stream/wayback/20220613164232im_/https://answers.unity.com/themes/thub/images/avi.jpg)