I actually got it working compressing the files at build into a tgz file. Then, I downloaded the file using WWW if the game runs on android and extracted it to the persistentdatapath folder. If anyone would like some examples I can post them. It required an external dll to do.
EDIT: Okay, who am I kidding I’m going to post the code here in case anyone ever has this problem.
1) The first thing I needed was a library that works on android and windows universally for zipping up and unzipping tar.gz files (or tgz files, both of those formats the same thing).
The best library I found for this is here:
https://github.com/icsharpcode/SharpZipLib
You need the dll entitled:
ICSharpCode.SharpZipLib
You need to place that in your Plug-ins folder in your Assets directory.
2) So say, like me, you have your main game database files and save files in a directory like “Assets/Data”. In this case what’s needed is to zip that folder up into a tgz file and place that tgz file within the StreamingAssets folder upon building the game.
One can do that using code like this…I named this file Editor_BuildPreprocess.cs and placed it within my Editor folder:
#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;
using UnityEditor.Build;
using System.IO;
using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Zip;
class CustomBuildPreProcess : IPreprocessBuild
{
public int callbackOrder { get { return 0; } }
public void OnPreprocessBuild(BuildTarget target, string path) {
string dataDirectory = Application.dataPath + "/Data/";
string fileToCreate = Application.streamingAssetsPath + "/Data.tgz";
Utility_SharpZipCommands.CreateTarGZ_FromDirectory (fileToCreate, dataDirectory);
}
}
#endif
Now we need to create a new cs file with our sharp zip library methods (we have to create these ourselves if we want to use the dll).
I named this file Utility_SharpZipCommands.cs (I prefix things with something like that typically):
using System;
using System.IO;
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Tar;
public class Utility_SharpZipCommands {
//// Calling example
//CreateTarGZ(@"c:\temp\gzip-test.tar.gz", @"c:\data");
//USE THIS:
public static void CreateTarGZ_FromDirectory(string tgzFilename, string sourceDirectory) {
Stream outStream = File.Create(tgzFilename);
Stream gzoStream = new GZipOutputStream(outStream);
TarArchive tarArchive = TarArchive.CreateOutputTarArchive(gzoStream);
// Note that the RootPath is currently case sensitive and must be forward slashes e.g. "c:/temp"
// and must not end with a slash, otherwise cuts off first char of filename
// This is scheduled for fix in next release
tarArchive.RootPath = sourceDirectory.Replace('\\', '/');
if (tarArchive.RootPath.EndsWith("/"))
tarArchive.RootPath = tarArchive.RootPath.Remove(tarArchive.RootPath.Length - 1);
AddDirectoryFilesToTar(tarArchive, sourceDirectory, true);
tarArchive.Close();
}
public static void AddDirectoryFilesToTar(TarArchive tarArchive, string sourceDirectory, bool recurse) {
// Optionally, write an entry for the directory itself.
// Specify false for recursion here if we will add the directory's files individually.
//
TarEntry tarEntry = TarEntry.CreateEntryFromFile(sourceDirectory);
tarArchive.WriteEntry(tarEntry, false);
// Write each file to the tar.
//
string[] filenames = Directory.GetFiles(sourceDirectory);
foreach (string filename in filenames) {
tarEntry = TarEntry.CreateEntryFromFile(filename);
tarArchive.WriteEntry(tarEntry, true);
}
if (recurse) {
string[] directories = Directory.GetDirectories(sourceDirectory);
foreach (string directory in directories)
AddDirectoryFilesToTar(tarArchive, directory, recurse);
}
}
public static void ExtractTGZ(string gzArchiveName, string destFolder) {
Stream inStream = File.OpenRead (gzArchiveName);
Stream gzipStream = new GZipInputStream (inStream);
TarArchive tarArchive = TarArchive.CreateInputTarArchive (gzipStream);
tarArchive.ExtractContents (destFolder);
tarArchive.Close ();
gzipStream.Close ();
inStream.Close ();
}
} // Calling example
Now what will happen when you build your game is the tgz file will be created and placed within your StreamingAssets folder. This is accessible then from Android like so when the game starts:
#if UNITY_STANDALONE_WIN
Debug.Log("unzipping to persistent data path (windows)");
Utility_SharpZipCommands.ExtractTGZ (Application.streamingAssetsPath + "/" + "Data.tgz",Application.persistentDataPath);
#endif
#if UNITY_ANDROID
//if mg_data doesn't exist, extract default data...
if (File.Exists(Application.persistentDataPath + "/" + "MG_Data.data") == false) {
ShowMessageOnScreen("\n\nMG_Data.data doesn't exist, creating it for the first time.");
//copy tgz to directory where we can extract it
WWW www = new WWW(Application.streamingAssetsPath + "/Data.tgz");
while ( ! www.isDone) {}
System.IO.File.WriteAllBytes(Application.persistentDataPath + "/" + "Data.tgz", www.bytes);
//extract it
Utility_SharpZipCommands.ExtractTGZ (Application.persistentDataPath + "/" + "Data.tgz",Application.persistentDataPath);
//delete tgz
File.Delete(Application.persistentDataPath + "/" + "Data.tgz");
} else {
ShowMessageOnScreen("\n\nMG_Data.data does exist, will not extract default data.");
}
#endif
With that done, now you have a file system you can work with on Android that is cross compatible with all other file systems using Application.persistentDataPath…just add in an #if UNITY_IOS or whatever for ios etc etc… above where it says STANDALONE_WIN.
With this it’s finally allowing me to start virtual reality games (like heavy data rpg games with xml save files) using samsung vr and also have it compatible with the occulus, for example.