- Home /
Save sprite in custom folder
I'm creating an android app with Unity and I have to create a sprite dynamically, save it to a custom folder ( maybe Application.PersistantData ?) because Resources folder is not available. So here are the different steps of the process :
Take a photo with the camera
Save it to Persistant data folder
Load it and create a sprite with this texture
Save the sprite somewhere (not resources folder, this is my problem. I already did it with AssetDatabase.CreateAsset() but its not available on build).
Set the sprite to a scriptable Object.
Steps 1, 2, 3 and 5 work on build. My problem is step 4.
This is my code (works on editor but not on build because of AssetDatabase.CreateAsset()) :
public class takePhoto : MonoBehaviour {
// Variables
WebCamTexture webCamTexture;
public Character UserObject;
private Sprite ImageUser;
private RawImage myTexture;
private byte[] bytesImage;
Texture2D thisTexture;
private RawImage thisImage;
private Sprite mySprite;
private SpriteRenderer sr;
private RawImage test;
private Sprite last_screenshot_save;
private Image m_SpriteRenderer;
private float widthSprite;
private float heightSprite;
private float ratio;
void Start () {
// Get the front camera on start
WebCamDevice[] devices = WebCamTexture.devices;
foreach (WebCamDevice cam in devices) {
if (!cam.isFrontFacing) continue;
webCamTexture = new WebCamTexture {deviceName = cam.name};
webCamTexture.Play ();
}
}
public void snapshot()
{
// Set file path
var fileNameResources = "Assets/Resources/userPic.asset";
var fileNamePersistantData = Application.persistentDataPath + "/userPic.png";
// Take photo and save it to persistent data folder
Texture2D photo = new Texture2D (webCamTexture.width, webCamTexture.height);
photo.SetPixels (webCamTexture.GetPixels ());
photo.Apply ();
byte[] bytes = photo.EncodeToPNG ();
File.WriteAllBytes (fileNamePersistantData, bytes);
if (System.IO.File.Exists(fileNamePersistantData))
{
// Load photo from persistent data folder
byte[] bytesPNG = System.IO.File.ReadAllBytes(fileNamePersistantData);
Texture2D texture = new Texture2D(1, 1);
texture.LoadImage(bytesPNG);
// var to crop sprite to square
var floatWidthCam = (float) webCamTexture.width;
var floatHeightCam = (float) webCamTexture.height;
widthSprite = (floatWidthCam - floatHeightCam) / 2.0f;
ratio = ((float)webCamTexture.width) / ((float)webCamTexture.height);
// Create square sprite from photo
mySprite = Sprite.Create(texture, new Rect(widthSprite, 0.0f, (texture.width) / ratio, texture.height), new Vector2(0.5f, 0.5f), 300.0f);
// Save the sprite (doesn't work on build)
AssetDatabase.CreateAsset(mySprite, fileNameResources);
Sprite t = (Sprite)AssetDatabase.LoadAssetAtPath(fileNameResources, typeof(Sprite));
// Set the sprite to scriptable object
UserObject.infos.states[UserObject.stateToUse].image = t;
}
}
}
Can someone help ?
Answer by Serellyn · Jun 03, 2018 at 09:09 AM
Take a look at this one. https://answers.unity.com/questions/245600/saving-a-png-image-to-hdd-in-standalone-build.html
As i said, my problem is not to save my png but my sprite.
var fileNameResources = "Assets/Resources/userPic.asset";
var fileNamePersistantData = Application.persistentDataPath + "/userPic.png";
// Take photo and save it to persistent data folder
Texture2D photo = new Texture2D (webCamTexture.width, webCamTexture.height);
photo.SetPixels (webCamTexture.GetPixels ());
photo.Apply ();
byte[] bytes = photo.EncodeToPNG ();
File.WriteAllBytes (fileNamePersistantData, bytes);
$$anonymous$$y problem is to save a sprite, created programmatically.
// Load photo from persistent data folder
byte[] bytesPNG = System.IO.File.ReadAllBytes(fileNamePersistantData);
Texture2D texture = new Texture2D(1, 1);
texture.LoadImage(bytesPNG);
// var to crop sprite to square
var floatWidthCam = (float) webCamTexture.width;
var floatHeightCam = (float) webCamTexture.height;
widthSprite = (floatWidthCam - floatHeightCam) / 2.0f;
ratio = ((float)webCamTexture.width) / ((float)webCamTexture.height);
// Create square sprite from photo
mySprite = Sprite.Create(texture, new Rect(widthSprite, 0.0f, (texture.width) / ratio, texture.height), new Vector2(0.5f, 0.5f), 300.0f);
// Save the sprite (doesn't work on build)
AssetDatabase.CreateAsset(mySprite, fileNameResources);
Sprite t = (Sprite)AssetDatabase.LoadAssetAtPath(fileNameResources, typeof(Sprite));
Here, my sprite is created programmatically and saved but AssetDatabase is not available when I build on Android. So, I guess i can't use System.IO.File.WriteAllBytes() method to save sprite, isn't it ?
AssetDatabase / Resources folder is never available after building, it's Editor only. So, however I am no expert on this at all, I think the only thing you need to change are the last 2 lines. Do'nt try to save it to the AssetDatabase, use a folder you can access like the persistantDataPath and save it there.
I'm sorry I can't help more than that.
Yes, this is what I want to do but do you know how to create and load an asset on a custom folder ?
Your answer
![](https://koobas.hobune.stream/wayback/20220612163931im_/https://answers.unity.com/themes/thub/images/avi.jpg)