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
1
Question by onevcat · Jun 14, 2011 at 02:04 PM · loadimage

iOS NSData to Unity, and LoadImage from the data

I have already get a NSData instance of an UIImage like this:

     UIImage *myImage = [UIImage imageNamed:@"pic.png"];
     NSData *imageData = UIImagePNGRepresentation(myImage);

Now I want to send the data to Unity and get the image.

Is there a possible solution to send "imageData" to Unity and use the function: Texture2D.LoadImage to get this image? I wonder whether the method "UnitySendMessage" in XCode can send this imageData to Unity?

Or is there any way to send an image from XCode's UIImage to Unity?

Thanks for any idea.

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

Answer by Peter G · Jun 14, 2011 at 04:18 PM

I did something similar in an app I made where I let the user choose there own image for the players texture. So let me direct you to some helpful sources.

  1. take the NSData object that you have and save it. Your app has permission to write data to your game's "Documents" folder. (According to the developer docs) The proper way of getting the documents path from XCode is to do the following:

       NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
          NSString *documentsDirectory = [paths objectAtIndex:0];
     then append on your specific file name such "/MyImage.png".  So you have the file directory and the data so save it (theres a method in `NSData` for writing to files), and you're ready for the next step.
    
    
  2. In Unity, then you can read that file and load the image. For reading files, see @SpinalJack's explanation here. Load the byte array using System.IO into Unity.

  3. Then the final step is the easiest. Use Texture2D.LoadImage() to load the image data into a texture.

EDIT: Just remembered, you can use the WWW to load the bytes as well. Probably isn't any faster than System.IO, but its easier. Just add file: to the beginning of the asset path in Unity.

Comment
Add comment · Show 2 · 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 onevcat · Jun 14, 2011 at 04:31 PM 0
Share

Thanks a lot for the answer. A good way to solve my issue. Saving the image to "Document" direction and then loading it from disk is a way. But I think it may be a little expensive to perform IO operations to/from disk, especially when I want load a lot of images.

If there is a way to just passing the bytes of image from XCode to Unity, it will be perfect.

Thank you again, and if there is no better solution, I will just follow you to perform IO.

avatar image subhaJit_Dev · Jun 08, 2015 at 01:45 PM 0
Share

In one of my projects, I used the WWW object to load textures, but, the problem is,every time you use the WWW.LoadTexture(), a new texture is created, and you run into memory issues. So, I guess, System.IO.ReadAllBytes(pathname) is more efficient.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Is it possible to split Texture2D.LoadImage into multiple parts? 1 Answer

Texture2D.LoadImage black pixel border 0 Answers

Fatal Exception when loading multiple image files on Android from persistent data path 0 Answers

Detecting Texture2D.LoadImage failure 2 Answers

loadImage texture crash on android 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