• 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 Corandy · Oct 18, 2012 at 12:33 PM · javascriptresourcesorderforlooploadall

LoadAll order issue

Hi,

I used a for-loop to display ascending textures in the gui. But the strange t$$anonymous$$ng is that the order line is completely different than the name order in my resource map.

 for(var n = 41; n > -1; n--)
     {
         var textures : Object[] = Resources.LoadAll("GUI", Texture2D);
         guiTexture.texture = textures[n];
         yield(WaitForSeconds(1));
     }

If I use Debug.log(n) then I get nicely the countdown from 41 to 0, but when i do Debug.log(textures[n]) I get 9,8,7,6,5,41,40,4,39,38 on so forth. The names of my images are from the same as the n values (0 to 41) and in the GUI map w$$anonymous$$ch is in the Resources map.

So the for loop works well, but the loadall does strange, does someone knows what's happening and knows how to fix it?

Thanks!

(And sorry for my bad english)

Comment
Add comment · Show 4
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 Kryptos · Oct 18, 2012 at 01:16 PM 0
Share
avatar image Corandy · Oct 19, 2012 at 11:57 AM 0
Share
avatar image Kryptos · Oct 19, 2012 at 12:14 PM 0
Share
avatar image Corandy · Oct 19, 2012 at 12:32 PM 0
Share

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by hvilela · Oct 19, 2012 at 11:14 PM

I see that you had solved your problem but sounds like you don't understand why. Resources.Load returns the results alphabetically (it's the same order that will see in the project tab), so it compares the first char for each file name and it will only compares the second one if the first one are different.

The second problem is that you are loading all textures 41 times. The t$$anonymous$$rd problems is that you hardcoded that you have 41 textures, so if you delete or add one texture you have to come back and fix you code. So, what I recommend is:

 var textures : Object[] = Resources.LoadAll("GUI", Texture2D);
 for (var n = textures.Lenght - 1; n >= 0; n--) {
        guiTexture.texture = textures[n];
        yield(WaitForSeconds(1));
 }
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 ProbePLayer · Jul 08, 2017 at 12:31 AM 3
Share
avatar image
0

Answer by RealWar555 · Apr 11, 2021 at 03:24 PM

 public static int CompareToUnity(t$$anonymous$$s string strA, string strB)
 {
     if (strA.IsNullOrEmpty() || strB.IsNullOrEmpty())
     {
         return strA.CompareTo(strB);
     }

     int minLength = strA.Length < strB.Length ? strA.Length : strB.Length;

     for (int i = 0; i < minLength; ++i)
     {
         if (char.IsDigit(strA[i]) && char.IsDigit(strB[i]))
         {
             string numberA = "";
             string numberB = "";

             for (int j = i; j < strA.Length; ++j)
             {
                 if (char.IsDigit(strA[j]))
                 {
                     numberA += strA[j];
                 }
                 else
                     break;
             }

             for (int j = i; j < strB.Length; ++j)
             {
                 if (char.IsDigit(strB[j]))
                 {
                     numberB += strB[j];
                 }
                 else
                     break;
             }

             int compare = int.Parse(numberA).CompareTo(int.Parse(numberB));

             if (compare != 0)
                 return compare;
             else
                 i += numberA.Length - 1;
         }
         else
         {
             int compare = ((int)strA[i]).CompareTo(strB[i]);

             if (compare != 0)
                 return compare;
         }
     }

     if (strA.Length < strB.Length) return -1;
     else if (strA.Length > strB.Length) return 1;
     else return 0;
 }
Comment
Add comment · 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
0

Answer by louiselessel · Apr 20, 2021 at 08:23 PM

I know t$$anonymous$$s is an old thread, but in case others get annoyed by the order issue, the fastest would be to just number your textures flowerOpacityMap_0, flowerOpacityMap_1, flowerOpacityMap_2... etc. and load in via a for loop using the "Resources.Load".

Make sure they are all placed in a folder you name whatever, inside a folder called "Resources". Eg. Assets/Resources/PurpleFlower_opacity_map/flowerOpacityMap_1.

I can't t$$anonymous$$nk of a use case for .LoadAll at all really.

T$$anonymous$$s code preps an array with opacity map textures, that can be switched in your shader. Using .Load you'd just do:

 public string pathOfResources = "PurpleFlower_opacity_map/flowerOpacityMap_";
 
 void Start() { 
         m_AlphaArray = new Texture[arraySize];
         m_Renderer = GetComponent<Renderer>();    
         
         for (int i = 0; i <= arraySize; i++)
         {
             string tempPath = pathOfResources + i.ToString();
             var texture = Resources.Load<Texture2D>(tempPath);
             m_AlphaArray[i] = texture;
         }
     }
Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

14 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

Related Questions

Loading textures runtime and assign to multiple button 1 Answer

How to identify/get the file name of resources loaded with Resources.LoadAll 2 Answers

Setting Scroll View Width GUILayout 1 Answer

What is the best way to learn C# and JavaScript? Which classes do I need to take? 1 Answer

Resource.LoadAll Loading of new text file type 0 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