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
0
Question by dittytwo · May 05, 2015 at 02:25 PM · c#textureimage

load all png files as resource but NOT from resource folder

I have an issue. I need to be able to load all the *.png files at run time from a folder under steamingassets, these are dynamic images and can changed after build. here is a sample of my code.

 using UnityEngine;
 using System.IO;
 using System.Collections;
 
 public class loadallimages : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
         GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube);
         string folderpath = Application.streamingAssetsPath + @"/Tiles/";
         // string[] FileList = Directory.GetFiles(folderpath, "*.png");//loads only png files
         Object[] textures = Resources.LoadAll(folderpath);
         int fred = Random.Range(0, textures.Length);
         Debug.Log(string.Format("fred is crurrently:{0}", fred));
         Debug.Log(textures.Length);
         Debug.Log(textures[fred].name);
         
         Texture2D texture = (textures[fred]) as Texture2D;
         go.GetComponent<Renderer>().material.mainTexture = texture;
     }
 }

Resources.LoadAll wont load them at all (fred=0).

Resources.load wont load, I get a list of null objects :(.

code to load each file (note: you have to remove the ".png" from the file name to get the Resource.load to load them)(but it doesn't)

 string[] FileList = Directory.GetFiles(folderpath, "*.png");//loads only png files
         Object[] textures = new Object[FileList.Length];
         for (int ten =0; ten> FileList.Length;ten++)
         {
             textures[ten] = Resources.Load(FileList[ten].Substring(0,FileList.Length-4)); // removes the ".png" from the end of the file name
         }
         

I would like the ability to load all the tile images at start up form a streamingassets/tiles folder location to use them then discard them to free up memory.

I understand that placing them in the resources folder and then calling Resources.loadAll("Tiles") would load them however the files will be changed after release hence the need to use the steamingassets folder. if there is an option them please help cheers D2

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

Answer by dittytwo · May 06, 2015 at 12:48 PM

I actually fixed my own issue with the code above

Change -->Texture2D texture = new Texture2D(32,32); to -->Texture2D tex = new Texture2D(x, y, TextureFormat.DXT5, false);

The TextureFormat is the key here as this sets the texture to a compressed texture; see link text for details. this then freed up some more memory for the processing

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 Bunny83 · May 06, 2015 at 01:09 PM 0
Share

@dittytwo: About how many images are we talking about? If you want to read them all in you should process one at a time. It makes not much sense to store the raw bytes for every image in a dictionary as the raw data isn't needed anymore once you load it into an Texture.

So it's much more memory friendly to read in file by file and directly create your Texture. Also note that Texture2D is a "tracked object" in Unity. That means it doesn't get garbage collected once all references to it are gone. You can still use FindObjectsOfType to retrieve any texture loaded. If you don't need a dynamically created texture anymore you should use Destroy() on it.

avatar image dittytwo · Aug 18, 2015 at 01:15 PM 0
Share

thanks @bunny83 that is useful info, cheers

avatar image
2

Answer by Baste · May 05, 2015 at 02:32 PM

Resources.Load is specifically for Resources, which is defined as "stuff in the resources folder when you build"

As far as I can tell, you'll have to handle stuff in the streamingassets in a much more low-level manner. If you have -.pngs, you should read the bytes of the files (System.IO.ReadAllBytes), and use the Texture2D method LoadImage which converts a byte array into an image.

In general, if you're going to be supplying arbitrary data to your application at runtime, you'll have to write the code that handles parsing that data yourself.

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 dittytwo · May 06, 2015 at 09:27 AM 0
Share

I have tried this.

 using UnityEngine;
 using System.IO;
 using System.Collections;
 using System.Collections.Generic;
 
 public class loadallimages : $$anonymous$$onoBehaviour {
 
     // Use this for initialization
     void Start () {
         GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube);
         string folderpath = Application.strea$$anonymous$$gAssetsPath + @"/Tiles/";
         string[] FileList = Directory.GetFiles(folderpath, "*.png");//loads only png files
         Debug.Log("readingin the files into byte");
         Dictionary<string, byte[]> readinPictures = returnreadinpictures(FileList);
         Debug.Log("read all the files in");
         Texture2D texture = new Texture2D(32,32);
         Debug.Log("adding the bytes as a texture");
         texture.LoadImage(readinPictures[FileList[5]]);
         Debug.Log("texture added");
         Debug.Log(string.Format("this is the file name {0} and this is the bytes{1}", FileList[5], readinPictures[FileList[5]].ToString()));
         go.GetComponent<Renderer>().material.mainTexture = texture;
     }
 
     /// <summary>
     /// returns the list of files pasted as filename , bytes read in
     /// </summary>
     /// <param name="filelist">pass a list of strings of the file location</param>
     /// <returns>Dictionary<string,byte> ("filelocation", bytes of read file)</returns>
     private Dictionary<string, byte[]> returnreadinpictures(string[] filelist)
     {
         Dictionary<string, byte[]> fred = new Dictionary<string, byte[]>();
         foreach (string claire in filelist)
         {
             fred.Add(claire, (File.ReadAllBytes(claire)));
         }
 
         return fred;
     }
 }

Basically it caused the ide to crash as it went of 2Gb (unity32.exe) there has to be a better way

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

Loading user custom image. 2 Answers

Multiple Cars not working 1 Answer

How do I iterate over a large Texture2d quickly? 1 Answer

Change texture of RawImage 2 Answers

Distribute terrain in zones 3 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