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 thornekey · Jan 15, 2014 at 10:53 PM · c#resourcesexternal

External Resource Folder -- Please Help

Hi, so I have a Resource folder within my project that i can load textures from. but id like the player to be able to load their own textures from an external resource folder (or any folder they choose)

this is my code so far:

     void OnGUI ()
     {
         
         filelocationinput = GUI.TextField (new Rect (10, 10, 128, 20), filelocationinput);
         
         
         if (GUI.Button (new Rect (10, 35, 128, 32), "Load!")) {
             LoadTexture ();
             
         }
     }
     
     void LoadTexture ()
     {
     
         Texture2D boxtexture = Resources.Load (filelocationinput) as Texture2D;
         Debug.Log ("loaded texture: " + filelocationinput);
         renderer.material.mainTexture = boxtexture;
         
     }

as you can see i have a GUI text field which i want the player to type the file location into (for example: "/Desktop/Game/GameDATA/ExtResource/texture.png")

but of course the code i have atm only loads from the compiled resource folder. any help would be much apreciated.

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

Answer by rutter · Jan 15, 2014 at 11:18 PM

You're probably already aware, but the Resources class won't be able to do that for you: it only really supports compiled resources that are shipped with your project.

Instead, you can use the WWW class to request a local file (ie: file:///path/to/file/my_texture.jpg), wait for the "download" to finish, then call LoadImageIntoTexture to convert that file into a texture. The function linked has a decent example.

This opens up a new can of worms, of course. How do you know which folder to search? Can the user select files in some sort of visual browser? Or will you just check their user docs folder for some specific path/name? The System.IO namespace provides Path and Directory classes that may be very helpful, there.

Comment
Add comment · Show 6 · 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 robertbu · Jan 15, 2014 at 11:23 PM 2
Share

I second @rutter's points. Here is a quick example using your code.

 using UnityEngine;
 using System.Collections;
 
 public class Bug25c : $$anonymous$$onoBehaviour 
 {
     public Texture texNotFound;
     private string filelocationinput = "";
 
     void OnGUI ()
     {
         
         filelocationinput = GUI.TextField (new Rect (10, 10, 128, 20), filelocationinput);
 
         if (GUI.Button (new Rect (10, 35, 128, 32), "Load!")) {
             StartCoroutine(LoadTexture ());
             
         }
     }
     
     IEnumerator LoadTexture ()
     {
         WWW www = new WWW ("file://"+filelocationinput);;
 
         yield return www;
 
         Texture boxtexture = null;
 
         if (www.error == null && www.texture != null) {
             boxtexture = www.texture;
         }
         else {
             boxtexture = texNotFound;
             Debug.Log ("Texture not found");
         }
 
         renderer.material.mainTexture = boxtexture;
     }
 }
avatar image thornekey · Jan 15, 2014 at 11:31 PM 0
Share

thanks guys, this has helped a lot :) i didnt realise that using the www would work.. (but i guess thinking back to when i was young doing html shananigans.. with local html pages.. heheh)

thanks again Also if the file cant be found can i just use return null? or idk

avatar image thornekey · Jan 15, 2014 at 11:33 PM 0
Share

oh i see there is a texnotfound variable thanks :))

avatar image rutter · Jan 15, 2014 at 11:35 PM 1
Share

@robertbu Nice example!

avatar image robertbu · Jan 16, 2014 at 12:11 AM 1
Share

@rutter - thanks. I edited the example to a more traditional use of WWW. He did not need the isDone hack.

Show more comments

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

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

Loading a resource file from external plugin dll 0 Answers

Best way to handle huge number of audio assets 2 Answers

Why does resources.load not work or what am I doing wrong? 1 Answer

How to load objects from a file outside of unity3d 3 Answers

Resources.LoadAll on Current Folder Only? 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