Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Mineraux · Jun 02, 2018 at 03:54 PM · spritepersistentdatapathsave file

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 :

  1. Take a photo with the camera

  2. Save it to Persistant data folder

  3. Load it and create a sprite with this texture

  4. Save the sprite somewhere (not resources folder, this is my problem. I already did it with AssetDatabase.CreateAsset() but its not available on build).

  5. 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 ?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mineraux · Jun 03, 2018 at 10:30 AM 0
Share

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 ?

avatar image Serellyn Mineraux · Jun 03, 2018 at 12:35 PM 0
Share

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.

avatar image Mineraux Serellyn · Jun 03, 2018 at 01:21 PM 0
Share

Yes, this is what I want to do but do you know how to create and load an asset on a custom folder ?

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

114 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

No Gamefolder created for mobile game (Nexus 4) 0 Answers

Save file is not detected, persistentdatapath changed? 2 Answers

Saved files deleted after creating a new build 0 Answers

Rotate the weapon sprite with a joystick 0 Answers

Sprite Filling not Working as Desired 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges