Pick random texture from a looooong folder hierarchy

Hello Everyone,

I’m on the development of a game who want to use library of anger-neutral-happy faces expressions and I need to make them randomly appear on an HUD plane above a space ship.

The issue I have, is that I can’t figure how create the access without a List (who will make me spend 3 years on it)…

I would like my hierarchy work this way:

The material must randomly pick into 2 folders (Men or Women), then in a random face folder (Men1, Men2…) and finally a random texture of facial expression in it (but I need the Random.Range to define the Length of the texture I want to use…)

So if anybody can help a beginner in game development, It will be very welcome :slight_smile:



Place your textures in the /resources folder of your project, and then populate a Texture2D array using Resources.LoadAll(). There’s a full example on the docs page.

If you have many large assets in that folder and you fear the memory pressure when you do Resources.LoadAll you can try my ResourceDB over here. The point of this class is to create an “inventory list” of all assets inside Resources folders at edit time and store that list in a scriptable object. That information can then be used at runtime to “find” assets / folders within Resources folders without actually loading them. The ResourceItem of an asset can be used to actually load that asset.

ps: in case you didn’t know, you can mass assign assets to a List or Array in the inspector by following those steps:

  • First select the object with the script that holds the array / List.
  • Now lock the inspector by clicking the tiny padlock at the top right of the inspector.
  • Now select all assets you want to add to the list using the usual Shift / Ctrl + mouse techniques.
  • Finally you just drag one of the selected items onto the List / array itself. Do not try to assign it to a single element of the list.
  • When done, don’t forget to unlock the inspector

Note: this does not take duplicates into account. So if “batch-assign” several asset which you already had in your list you will get duplicates. It’s usually easier to just set the size back to “0” and reassign all assets when you’ve added some more during development.

In case you didn’t know you can use “Shift+Delete” to remove an array / List element in between. Just click on the element and press Shift+Delete. Likewise you can “insert” a single element in a list / array be pressing Ctrl+D which will duplicate the element below the current selected.

Oh Very Easy!

Attach this script to an object and It will assign RandomFace variable to a random texture

Note: In the Inspector panel of the Object You have to drop EVERY Texture to choose from in the texture Array space

//This Script Chooses random Texture every time you press space
#pragma strict
public var RandomFace = Texture;
public var texureArray = Texture[];
function Start(){
function Update(){
function PickRandomFace(){
   RandomFace = textureArray[Random.Range(0,textureArray.lenght)];