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 xxluky · Aug 18, 2016 at 04:19 PM · android buildpathfindingresources

How to get Resources filepath on Android device?

Hello guys,

I went through some questions in the forum and none of the answers was relative to my question or it did not work.

In my project I have a Resources folder. I am not loading files from this folder, I only need to get the path of the file in this folder. In the Editor I have this and it is working perfectly:

filePath = "Assets/Resources/" + value.Enemy + ".dat"; Output is: filePath = "Assets/Resources/"CliffSide".dat";

But now I need to release this game on Android device and it is not working. How should I get the path of the asset from the android build?

Please 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

2 Replies

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

Answer by LK84 · Aug 18, 2016 at 04:44 PM

The Resoures folder simply doens't exist any more afer your build. The assets in the Resources folder get packed into the game's archive for assets.

Comment
Add comment · Show 7 · 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 xxluky · Aug 18, 2016 at 04:47 PM 0
Share

Ok, thank you very much for your answer. So what is the file path on Android after the build?

avatar image meat5000 ♦ xxluky · Aug 18, 2016 at 04:53 PM 0
Share

This help?

https://docs.unity3d.com/$$anonymous$$anual/Strea$$anonymous$$gAssets.html

avatar image xxluky meat5000 ♦ · Aug 18, 2016 at 05:10 PM 0
Share

No, I tried that. Strea$$anonymous$$g Assets not working. Documentation is not explained well, too. Do I have to rename the resources folder or anything like that?

I also looked at this, bud it does not say much: http://docs.unity3d.com/ScriptReference/Application-dataPath.html

Show more comments
avatar image xxluky · Aug 19, 2016 at 09:14 AM 0
Share

Ok, according to Snip2Code site, Android only use WWW to read file. So in my case this worked just fine :-D

$$anonymous$$y final code:

 string tempPath = System.IO.Path.Combine(Application.strea$$anonymous$$gAssetsPath, "CliffSide.dat");
 
 // Android only use WWW to read file
 WWW reader = new WWW(tempPath);
 while ( ! reader.isDone) {}
 
 filePath = Application.persistentDataPath + "/db";
 System.IO.File.WriteAllBytes(filePath, reader.bytes);


avatar image saschandroid xxluky · Aug 19, 2016 at 09:43 AM 1
Share

Ah ok. In your question you said you don't want to load from the resource folder ... so I didn't answer your question. You could have done the same thing with the Resources folder. You just have to rename your files to "*.bytes" and then the following should work:

 // This is for multiple files.
 TexAssets[] l_assets = Resources.LoadAll("nameOfResourcesSubFolder", typeof(TextAsset)).Cast<TextAsset>().ToArray();
 for (int i = 0; i < l_assets .Length; i++)
 {
     File.WriteAllBytes(Application.persistenDataPath + "/db/" + l_assets[i].name + ".dat", l_assets[i].bytes);
 }



avatar image nmbileg xxluky · Jan 25, 2018 at 11:11 AM 0
Share

YOU ARE $$anonymous$$Y HERO!!! THAN$$anonymous$$S A LOT!!!

avatar image
2

Answer by shadyshrif · Jul 04, 2017 at 07:59 PM

To solve this problem I created pre-compilation script which will run before the compilation; this script will list the names of the files you want to access later in text file

  #if UNITY_EDITOR
  using UnityEditor.Build;
  using UnityEditor;
  using System.IO;
  
  public class BM_AndroidBuildPrepartion : IPreprocessBuild
  {
      public int callbackOrder { get { return 0; } }
      public void OnPreprocessBuild(BuildTarget target, string path)
      {
          // Do the preprocessing here
          string[] fileEntries = Directory.GetFiles("Assets/Resources/Prefabs/alphabet", "*.prefab");
          System.IO.Directory.CreateDirectory("Assets/StreamingAssets/");
          using (StreamWriter sw = new StreamWriter("Assets/StreamingAssets/alphabet.txt", false))
          {
            
          foreach (string filename in fileEntries) {
                  sw.WriteLine(Path.GetFileNameWithoutExtension(filename));
          }
            
          }
      }
  }
  #endif

then I read the text file and you can access the files in same way but you have to put them inside your project in Assets/StreamingAssets/

    #if UNITY_ANDROID
         string  path = "jar:file://" + Application.dataPath + "!/assets/alphabet.txt";
           WWW wwwfile = new WWW(path);
           while (!wwwfile.isDone) { }
           var filepath = string.Format("{0}/{1}", Application.persistentDataPath, "alphabet.t");
           File.WriteAllBytes(filepath, wwwfile.bytes);
   
           StreamReader wr = new StreamReader(filepath);
               string line;
               while ((line = wr.ReadLine()) != null)
               {
               //your code
               }
    #endif
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

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

58 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

Related Questions

Android build error. Failed to re-package resources. 3 Answers

CommandInvokationFailure: Failed to re-package resources. 0 Answers

How to read file on android built from Unity? 1 Answer

What's wrong with my script? 1 Answer

iPhone build doesn't copy existing files in Application.dataPath or .persistantDataPath?! 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