Basically, I’m making an app which takes a picture of the user(which allows them to switch their camera), and saves it in their storage.
What I researched so far:
I looked around for a bit, and found some solutions, some of them using particularly c# specific like system.IO .
Other, using the AssetDatabase, I couldn’t much understand if it was usable outside the unity editor, in real devices(there was someone telling the namespace was UnityEditor, and it only worked in the unity editor, does that mean it didn’t work on real device path, like on Android?)
And, I most certainly don’t understand, if UnityEngine.Windows is a windows-only namespace? It’s almost undocumented, but, I could gather from one of it’s classes, it mentioned something called, Universal Windows Platform, so, it probably is. (And it would mean, I can’t use it as a device Agnostic code).
What I’m looking to do:
My app is targeted to android and iOS platform, so, what would be the most platform agnostic way to write a file in unity?
Using my little knowledge, I plan to do this:
string dataPath = UnityEngine.Application.persistentDataPath;
string dirName = “myDir”;
string fullPath = Path.Join(dataPath, dirName);
if(!System.IO.Directory.Exists(fullPath)){
Directory.CreateDirectory(fullPath);
}
System.IO.SomeMethodToWriteFileInThePath(fullPath, filename, data);
Is what I am doing right? If not, what would be the most platform agnostic way to write the data as described above in unity?
Yes, System.IO.File is almost certainly what you want to use. AssetDatabase is, as you figured, specific to the Unity Editor and your project’s assets and not usable at runtime. However Application.persistentDataPath isn’t going to be the user’s Camera Roll or Photo Gallery or whatever it’s called across all the platforms – now that may be a specific directory if you’re lucky, but may also require some native API calls specific to each platform to add a photo to.
I mean that, each system (Android, iOS, Windows Phone (do those still exist?), etc.) will probably have their own way of deciding what pictures show up in the native Camera Roll/Photo Gallery apps that may not be as simple as writing a file to a specific directory. You’ll need to look up the specific functionalities for each of those platforms and what you need to do to register files/photos.