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 Vortexboy · Nov 06, 2018 at 12:17 PM · iostexture2dimagefile

How to save textures to iOS devices, ask for help.

Now I need to implement saving images to my iOS device. I have seen many online examples using "ScreenCapture.CaptureScreenshot(filepach);" for screenshot capture, but what I need is to save the texture image prepared in Unity. Please ask me What do you need to do.

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
1
Best Answer

Answer by Grish_tad · Nov 06, 2018 at 05:07 PM

Hi @Vortexboy there is a simple way to save textures to your device.

   IEnumerator SaveTexture(Texture2D texture)
     {
         string fileName = "Photo";
         string screenshotFilename;
         string path;
 
         string date = System.DateTime.Now.ToString("ddMMyyHHmmss");

         screenshotFilename = fileName + "_" + date + ".png";
 
         if (Application.platform == RuntimePlatform.Android)
         {
             string androidPath = "/../../../../DCIM/YourAppName/" + screenshotFilename;
             
             path = Application.persistentDataPath + androidPath;
             string pathonly = Path.GetDirectoryName(path);
 
             if (!Directory.Exists(pathonly))
             {
                 Directory.CreateDirectory(pathonly);
             }
         
         }
         else if (Application.platform == RuntimePlatform.IPhonePlayer)
         {
             string iosPath = Application.persistentDataPath + "/" + screenshotFilename;
             path = iosPath;
         }
         else
         {
             string otherPath = screenshotFilename;
             path = otherPath;
         }
         byte[] bytes = texture.EncodeToJPG();
         File.WriteAllBytes(path, bytes);
         yield return new WaitForEndOfFrame();

     }
Comment
Add comment · Show 5 · 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 Vortexboy · Nov 07, 2018 at 02:25 AM 0
Share

thank you very much!

avatar image Vortexboy · Nov 07, 2018 at 06:52 AM 0
Share
 using System.IO;
 using System.Collections;
 using System.Runtime.InteropServices;
 using UnityEngine;
 
 
 public class SaveImage : $$anonymous$$onoBehaviour
 {
 #if UNITY_IOS
     [DllImport("__Internal")]
     private static extern void _SavePhoto(string readAddr);
 #endif
     public Texture2D[] textures;
     private Texture2D texture;
     private string m_ImageFileName;
 
     public void OnCilck()
     {
         StartCoroutine(SaveImages());
     }
 
     IEnumerator SaveImages()
     {
         texture = textures[Random.Range(0, textures.Length)];
         m_ImageFileName = texture.name +".png";
 
         string path = Application.persistentDataPath;
 #if UNITY_ANDROID
         path = "/sdcard/DCI$$anonymous$$/Save Image";
 #elif UNITY_IOS
         path = Application.persistentDataPath;
 #endif
 
         if (!Directory.Exists(path))
             Directory.CreateDirectory(path);
 
         string savePath = path + "/" + m_ImageFileName;
         File.WriteAllBytes(savePath, texture.EncodeToPNG());
         savePng(savePath);
         yield return new WaitForEndOfFrame();
     }
 
     public void savePng(string fileName)
     {
 #if UNITY_ANDROID
         GetAndroidJavaObject().Call("scanFile", fileName);
 #elif UNITY_IOS
         _SavePhoto(m_ImageFileName);
 #endif
     }
 
 #if UNITY_ANDROID
     private AndroidJavaObject GetAndroidJavaObject()
     {
         return new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
     }
 #endif
 }
 

Hello, I used your method, but still did not save successfully, this is my code.

avatar image Vortexboy · Nov 07, 2018 at 07:06 AM 0
Share

Only iOS does not save the image.

avatar image Grish_tad Vortexboy · Nov 07, 2018 at 12:04 PM 0
Share

iOS saved it to, but you need some libraries to show saved images in galery.

avatar image Vortexboy Grish_tad · Nov 07, 2018 at 12:20 PM 1
Share

Thank you very much, it turned out that my "path" was wrong. Now I can successfully save the image and view the saved image in the iPad album.

      public void savePng(string fileName)
      {
  #if UNITY_ANDROID
          GetAndroidJavaObject().Call("scanFile", fileName);
  #elif UNITY_IOS
          //_SavePhoto(m_ImageFileName);
          _SavePhoto(fileName);
  #endif
      }

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

133 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 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

How do I get file size of texture? 1 Answer

How to detect file type loaded from device local storage 1 Answer

Create, export, and then load a huge image. 2 Answers

Continuously scanning and recognising text with Unity3D on Android 0 Answers

iOS crashing on Texture2D LoadImage 1 Answer


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