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 /
This question was closed Feb 28, 2015 at 07:58 AM by thornekey for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by thornekey · Feb 27, 2015 at 09:43 AM · texturedynamicsystem.io

Dynamically store images from file path folder

Ive set up my game to be able to change a texture by inputing a file path. How can I make it so that if i put a folder as a file path all the images in that folder are stored in an array of textures?

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

  • Sort: 
avatar image
1

Answer by SkaredCreations · Feb 27, 2015 at 10:47 AM

Here is:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.IO;
 
 public class MyClass : InGameScriptCS
 {
     Texture[] myTexs = new Texture[0];
 
     void Start ()
     {
         myTexs = GetTexturesInFolder("/Users/myuser/Pictures");
     }
 
     Texture[] GetTexturesInFolder (string path)
     {
         List<Texture> textures = new List<Texture>();
         List<string> texExtensions = new List<string>() { ".png", ".jpg", ".jpeg", ".psd", ".tiff", ".dds" };
         string[] files = Directory.GetFiles(path);
         foreach (string file in files)
         {
             // Skip non-texture files
             if (!texExtensions.Contains(Path.GetExtension(file)))
                 continue;
             try
             {
                 Texture2D tex = new Texture2D(1,1);
                 tex.LoadImage(File.ReadAllBytes(file));
                 textures.Add(tex);
             }
             catch
             {
             }
         }
         return textures.ToArray();
     }
 
     void OnGUI()
     {
         if (myTexs.Length > 0)
             GUI.DrawTexture(new Rect(0, 0, myTexs[0].width, myTexs[0].height), myTexs[0]);
     }
 }
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 thornekey · Feb 27, 2015 at 11:24 AM 0
Share

i had this: it didnt work. i dont know why...

 IEnumerator AllImages() {
         if (Directory.Exists (pathDir)) {
             dirImages.Clear();
 
             string[] dirPaths = Directory.GetFiles(pathDir, "*.jpg", SearchOption.AllDirectories);
         
 
             if(dirPaths.Length > 0) {
 
                 foreach (string i in dirPaths) {
 
                     WWW www = new WWW ("file:///" + pathDir);
                     yield return www;
                     
                     Texture prevText = null;
                     
                     if (www.error == null && www.texture != null) {
                         prevText = www.texture;
                     }
                     else {
                         prevText = textNotFound;
                         Debug.Log ("Texture not found");
                     }
                     
                     prev_2.renderer.material.mainTexture = prevText;
                     Texture2D newTex = new Texture2D (1,1 );
 
                     www.LoadImageIntoTexture(newTex);
 
                     dirImages.Add(newTex);
 
 
                     Debug.Log(i.ToString());
                 }
             }
         }
     }



it says "You are trying to load data from a www stream which had the following error when downloading."

avatar image SkaredCreations · Feb 27, 2015 at 11:44 AM 0
Share

Why "file:///"? I think it should be "file://" if pathDir is an absolute path (don't know if you need also to replace "\" with "/" for Windows platforms, you'll need to try). Also I think you should pass "i" to WWW ins$$anonymous$$d of "pathDir".

Follow this Question

Answers Answers and Comments

19 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

Related Questions

Assigning UV Map to model at runtime 0 Answers

Need help with creating dynamic textures. 1 Answer

Load image into texture not working on Galaxy SIII 1 Answer

Dynamically loading textures to avoid memory problems 1 Answer

Dynamicaly Creating A Plane With A JPG Material ... 2 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