Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Rustam-Ganeyev · Nov 01, 2011 at 12:17 PM · texturesresources

[Solved]copy data from resources to persistent data path

I want to copy files(images) from Resources to Application.persistentDataPath. Here's the code:

 private void SaveFile (string fileName)
 {
     Texture2D texture = Resources.Load (System.IO.Path.GetFileNameWithoutExtension (fileName)) as Texture2D;
     byte[] bytes = ?//i need to get byte array from texture
     System.IO.File.WriteAllBytes (Application.persistentDataPath + folder, bytes);
 }

The question is: can we get byte array of files in Resources folder? Or can we get byte information from textures?

UPD: if you want to copy file to persistent data path, you may use TextAssets(notice, your file has to have "bytes" extension on yr file to copy binary data).

Comment
Add comment · Show 3
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 VamshiKrishnaP · Jan 27, 2015 at 08:15 AM 0
Share

Hi, How can i get byte array from the texture ? can you suggest ?

avatar image Rustam-Ganeyev · Jan 27, 2015 at 12:36 PM 1
Share

@Vamshi$$anonymous$$rishnaP, use texture.EncodeToPNG

avatar image meat5000 ♦ · Jan 27, 2015 at 01:19 PM 0
Share

@Rustam Could you insert an answer for completeness?

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Rustam-Ganeyev · Jan 27, 2015 at 01:30 PM

You can use texture.EncodeToPNG

Comment
Add comment · 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
0

Answer by VamshiKrishnaP · Feb 03, 2015 at 07:23 AM

Hi,

Here i followed like this and working perfect.

First i placed a ImageToUpload.png file in resources folder and renamed its extension .png to .bytes

Then called the below lines in void Start() to copy the image in persistentDataPath.

   void Start(){
     TextAsset imageTextAsset;
     imageTextAsset = Resources.Load (System.IO.Path.GetFileNameWithoutExtension ("ImageToUpload")) as TextAsset;
     File.WriteAllBytes(Application.persistentDataPath + "/ImageToUpload.png", imageTextAsset.bytes);
     }

Comment
Add comment · Show 1 · 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 Rustam-Ganeyev · Feb 03, 2015 at 08:53 AM 0
Share

Well, a lot of things were changed since this time. Now it's much much easier:

Firstly, put your imageToUpload.png to resources (do not rename to .bytes extension)

And then write this code:

 void Start () {
     var texture = Resources.Load<Texture2D>("imageToUpload");//omit extension
     File.WriteAllBytes(Application.persistentDataPath + "/imageToUpload.png", texture.encodeToPng());

That's it! You have copy of your image in persistent data path and you can change it.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

C# Error when trying to load Texture at runtime 1 Answer

Load 2D Textures with Resource.Load doesn't work 4 Answers

Loading Custom Texture, Audio, Sprite at runtime (Like Resource Packs in minecraft ye) 0 Answers

Import Texture with good quality... 2 Answers

Does loading a Resource load that resource's dependencies? 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