Hello unity community,
I was wondering if anyone can help me figure out how to get a texture in unity to save to memory on the iPhone.
I know that if I was on a PC I would write something like this:
System.IO.File.WriteAllBytes(Application.dataPath + @“/…/PhotoLibrary” + @“/newPic.png”, texToSave.EncodeToPNG());
Basically, I want to do that same thing for the iPhone, but have no idea where to even get started in doing this.
From what I’ve read so far, it seems I will have to write a native Objective C plugin to deal with the texture once I get a reference to that texture in memory. However, I’m not exactly sure how to even access that Unity texture object on the iPhone through objective C.
Can anyone help me out with this issue, or at least point me in the right direction to get started on this problem?
you need to use either the applications documents path or its cache path if you want to write (independent on if you use .NET system.io or objc directly)
Alright, so first off, am I right in assuming that I can’t permanently save photos and create folders in my application cache path?
Basically, what I need to be able to do is to save some photos created in my app somewhere in memory so I can continue to use those photos between sessions in my application. It sounds like the applications documents directory is where I would want/need to save those photos, but if I can’t write to that directory, then how would I go about keeping my photos in memory (without having to save them to the iPhones photo library)?
Alright, so that leaves finding the correct path to that cache folder. So far I’ve found reference to accessing that path using objective C, but is there a way I can access that folder doing something like:
Application.dataPath + “/Cache”
function CreateImagesDirectory() {
_ImagePath = _FileLocation +"images/";
if (Directory.Exists(_FileLocation +"images")) {return;}
var t: DirectoryInfo = new DirectoryInfo(_FileLocation);
t.CreateSubdirectory("images");
}
In my app i’m creating an images folder, to save my screenshots and thumbnails in, the code above does this.
Thanks a lot guys! I’m getting closer to my goal.
So far I’ve been able to create the new directory using the code Gamma posted.
After getting the reference to that new path,
I’ve been trying to save my image texture using:
WWW imageToLoadPath = new WWW(@"file://" + PhotoPath);
tempTex = new Texture2D(320, 480);
imageToLoadPath.LoadImageIntoTexture(tempTex);
PhotoObject.renderer.material.mainTexture = (Texture)tempTex;
When I do this on the PC or mac, this works perfectly. However, on the iPod touch I am testing it on, it isn’t working.
Instead of loading the image I saved, it just loads a blank (invisible) texture. However, it does seem to be loading something, because when I try loading the image, I am outputting the dimensions of that texture on the screen, and the correct dimensions are showing up.
So far I’ve tried getting my ipod jailbroken to see if I can look at the saved image on there, but so far I haven’t had much success doing this…
Does anyone else have some suggestions on why my file might not be loading correctly?