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 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 thing 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 which 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

Two mistakes here:

  1. Loading order depends on internal GID. Not the name of the texture.

  2. You're loading again at each iteration; This is wrong, you should load the textures outside the loop.

avatar image Corandy · Oct 19, 2012 at 11:57 AM 0
Share

Thanks for the comment, about number 2 you were more than right. But can you explain more about internal GID? I don't know what it means and how can I change it the way I would like?

avatar image Kryptos · Oct 19, 2012 at 12:14 PM 0
Share

It's an internal ID used by Unity. You cannot change it. Basically it is a global counter that is incremented each time an object is created so that each object is guaranteed to have a unique ID.

When you use GetComponents or GetComponentsInChildren, object are sorted by ID. $$anonymous$$y guess is, it is the same with resources.

avatar image Corandy · Oct 19, 2012 at 12:32 PM 0
Share

Thanks for the advice, I have fixed the order by na$$anonymous$$g them _001, _002, _003 etc ins$$anonymous$$d of 01, 02, 03. It works fine now, so if you ever use this type of method (which isn't a good one either) then it is more stable with those names.

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 third 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

LoadAll does not load assets as they are seen in the project tab. Example: project tab lists objects as so: 1,2,3....,9,10,11,12. LoadAll returns as so: 1, 10, 11, 12...2, 20, 21, 22

avatar image
0

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

 public static int CompareToUnity(this 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 this 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 think of a use case for .LoadAll at all really.

This 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.

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

How would I set up an order for turns using multiple scripts? 3 Answers

Resource.LoadAll Loading of new text file type 0 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Manually changing between sprites in a spritesheet 0 Answers

How i can a variable clasification in javascript? 3 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