Page 1 of 2 12 LastLast
Results 1 to 10 of 11
  1. #1
    Join Date
    Aug 2004
    Location
    Sydney Australia
    Posts
    737

    find the folder structure of textures in unity

    Hey guys and girls, firstly i havent been scripting for terribly long, but I have started on a project and gotten a fair way but i've just hit a wall that i've not been able to find my way around I wonder if you can help me.

    I've got it where when i click on an object i can get the texture to change on the object based upon an array of textures i've set in a variable. it cycles through the textures in the array with each subsiquent click.

    Anyway, The problem I am running into is that I want when you click on an object to find out the directory the texture its currently using is, then populate the array with all the textures from that folder. so that whatever object you click on it will only cycle through its textures. by having this dynamic it means i dont need to set up tons and tons of arrays for each and every object. just have a logical folder structure within unity and it should all work

    If you guys could point me in the right direction that would be awesome

    Thanks

    eagle4
    The race for quality has no finish line -
    So technically its more like a death march

  2. #2
    Join Date
    Aug 2010
    Location
    Vector3.Zero
    Posts
    146
    u can get the file path using the AssetDatabase or c# file class. First of all all of your textures have to be in the Resources folder, make seperate folder if you want to load them seperately. cuz the method I'm going show is going to load everything inside the child folders if you provide a parent folder to load the textures.

    AssetDatabase is a UnityEditor class. You can use AssetDatabase.GetAssetPath to get a path of a certain asset; it IS relative to the "Assets" folder of your project. To get the path simply provide the asset resource, and it will return the path(which is, again, relatice to the "Assets" folder). Syntax is below:

    Code:
    AssetDatabase.GetAssetPath(Object obj) : String
    To get/load all the textures, you must use the Resources class; Syntax is below:

    Code:
    Resources.LoadAll(string resourceRelativePath, type fileType) : Object[]
    This certain function returns an ARRAY of objects of type that you'd sepcify in the fileType field(in this case you'd want "Texture2D"). Remember, Resources.LoadAll function IS relative to the "Resources" folder(this is why I told you to put the textures inside a folder called "Resources" in your "Assets" folder.

    You can use these both techniques to figure out what folder that particular asset is located and concatenate the folder name to get the "Resources-Folder-Specific-Path" then use that path to load the textures from the folder

    Hope this helped. Let me know if anything goes wrong
    Last edited by peter8989; 10-24-2011 at 08:53 PM.
    Noob stuff I've been playing with

    Multiplayer RPG - http://www.youtube.com/user/Stridervan?feature=mhee
    Advanced Minimap Tutorial - http://www.youtube.com/playlist?list...ature=view_all
    Website - http://projectzoso.weebly.com/

  3. #3
    Join Date
    Aug 2004
    Location
    Sydney Australia
    Posts
    737
    Thanks heaps for that mate, after trying to wrap my head around what you wrote this is what i've got in my code. it seems to be working up to a point, but doesn't seem to be loading the textures into the array

    var FullPathName = AssetDatabase.GetAssetPath(myObjectsTexture); //this works
    var DirName = System.IO.Path.GetDirectoryName(FullPathName); //so does this. debugging shows its fine

    var mytextures : Object[] = Resources.LoadAll(DirName, Texture2D) ; //this is where i reckon its going wrong
    Debug.Log(mytextures[0]);


    However it errors when i try to debug.Log it. it says "IndexOutOfRangeException: Array index is out of range."

    I wonder if you could cast your eyes over this and see what could be improved. Thanks again for your help mate. really appreciate it.

    eagle4
    The race for quality has no finish line -
    So technically its more like a death march

  4. #4
    Join Date
    Aug 2010
    Location
    Vector3.Zero
    Posts
    146
    100% its NOT going to load the textures from the code lol. heres the code I generated on the fly, I'll recheck when I get home if I made any errors. there's most likely going to be some spelling errors ;p

    your getting there close, but as i mentioned "Resources.LoadAll"(any Resources.Load) function will ONLY work for a folder called "Resources". ex if your asset path is something like "D:\Unity Projects\Project 23\Assets" then you have a folder called "Resources"(you have to create one in order for this to work) inside the "Assets" Folder. Then you have bunch of folders inside the "Resources" folders called "R001", "R002", and "R003". Your call to the "Resources.LoadAll" would be like,

    Code:
    Texture2D tex;
    Object[] texArray;
    
    string path;
    string[] pathSeperator;
    
    void Awake()
    {
    	tex = Resources.Load("SampleTex") as Texture2D;
    	pathSeperator = new string[] { "Resources/" };
    }
    
    void OnGUI()
    {
    	path = AssetDatabase.GetAssetPath(tex); // getting the actual path
    	path = path.Split(pathSeperator, System.StringSplitOptions.None)[1]; // getting the path w/ filename relative to the resources folder
    
    	if (path.LastIndexOf('/') == -1) // means it only returned a filename
    		path = ""; // so set the path to an empty string
    	else
    		path = path.Remove(path.LastIndexOf('/')); // otherwise eleminate the filename and store the relative path
    
    	texArray = Resources.LoadAll(path, typeof(Texture2D)); // loads all the textures from that folder
    	GUI.DrawTexture(new Rect(0, 0, 50, 50), texArray[0] as Texture2D);
    }
    hope it helped, lemme know if u run into any problems
    Last edited by peter8989; 10-26-2011 at 12:17 AM.
    Noob stuff I've been playing with

    Multiplayer RPG - http://www.youtube.com/user/Stridervan?feature=mhee
    Advanced Minimap Tutorial - http://www.youtube.com/playlist?list...ature=view_all
    Website - http://projectzoso.weebly.com/

  5. #5
    Join Date
    Aug 2004
    Location
    Sydney Australia
    Posts
    737
    still trying to wrap my head around this, sorry if i am seeming thick.

    I tried coping everything you had there and made a new javascript file and it fires back a ton of errors, usually about a ton of missing semicolons but it appears its wanting it mid line. like at (1,10). strange

    When I run my above code that I gave before, the path that it provides for the variable DirName is Assets/Resources/textures/texturepack1

    now when i try to load the textures are you sayng that it default points to the Assets/Resources folder? so i dont need to have them in my path string? so my path string that i am trying to load from should be just textures/texturepack1

    also not sure what you're doing when you have void Awake() and void GUI() are these specific unity functions or just functions that you've made with your own names?

    Thanks again for ya help mate. you're a champ
    Last edited by eagle4; 10-25-2011 at 04:26 PM.
    The race for quality has no finish line -
    So technically its more like a death march

  6. #6
    Join Date
    Sep 2008
    Location
    Salisbury, UK
    Posts
    2,812
    Quote Originally Posted by eagle4 View Post
    I tried coping everything you had there and made a new javascript file and it fires back a ton of errors, usually about a ton of missing semicolons but it appears its wanting it mid line. like at (1,10). strange
    Unless I'm much mistaken, Peter's code in post #4 is C#, not Javascript... it'll need to go in a *.cs file.

    I found
    my current avatar on google, so props to THIS GUY who created the original...

  7. #7
    Join Date
    Aug 2010
    Location
    Vector3.Zero
    Posts
    146
    Quote Originally Posted by fatgav View Post
    Unless I'm much mistaken, Peter's code in post #4 is C#, not Javascript... it'll need to go in a *.cs file.
    Thanks Gav Yes, It is C# eagle4, Unless you don't want to use C# here's what you can do.

    Quote Originally Posted by eagle4 View Post
    now when i try to load the textures are you sayng that it default points to the Assets/Resources folder? so i dont need to have them in my path string? so my path string that i am trying to load from should be just textures/texturepack1
    Exactly. It's ONLY true when you are using Unity's Resources class(any of its methods)

    Quote Originally Posted by eagle4 View Post
    also not sure what you're doing when you have void Awake() and void GUI() are these specific unity functions or just functions that you've made with your own names?
    Both, "Awake()" and "OnGUI()" are Unity functions, which are called when Initializing Variables(Awake) and when drawing GUI stuffs(OnGUI).

    http://unity3d.com/support/documenta...Behaviour.html

    Any problems, lemme know
    Noob stuff I've been playing with

    Multiplayer RPG - http://www.youtube.com/user/Stridervan?feature=mhee
    Advanced Minimap Tutorial - http://www.youtube.com/playlist?list...ature=view_all
    Website - http://projectzoso.weebly.com/

  8. #8
    Join Date
    Aug 2004
    Location
    Sydney Australia
    Posts
    737
    thanks guys, I didnt realise it was c#. (clearly still a programming noob lol)

    Yeah I'd like to stay with javascript as it seems very familiar to me as its rather similar to MEL script.

    Quote Originally Posted by peter8989 View Post
    Thanks Gav Yes, It is C# eagle4, Unless you don't want to use C# here's what you can do.
    What can I do?


    I'll try removing the Assets/Resources from the path and see how that goes, unless you have any other sugestions
    The race for quality has no finish line -
    So technically its more like a death march

  9. #9
    Join Date
    Aug 2004
    Location
    Sydney Australia
    Posts
    737
    Hey guys, after a bit of dicking around I finally got it to work.

    I used the split function to break the string apart and just used the extensions i want, removing the Assets/Resources from the front and it worked like a charm.

    Thanks a ton for your help fellas
    really appreciate it
    The race for quality has no finish line -
    So technically its more like a death march

  10. #10
    Join Date
    Aug 2010
    Location
    Vector3.Zero
    Posts
    146
    thats what I did in the c# version of the program :P was going to write JS version of that but, glad you figured that out
    Noob stuff I've been playing with

    Multiplayer RPG - http://www.youtube.com/user/Stridervan?feature=mhee
    Advanced Minimap Tutorial - http://www.youtube.com/playlist?list...ature=view_all
    Website - http://projectzoso.weebly.com/

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •