Author Topic: Atlas Switching - only loading needed atlases  (Read 5825 times)

db82

  • Guest
Atlas Switching - only loading needed atlases
« on: August 20, 2012, 12:41:13 PM »
I'd just like to share my Atlas swapping method in case it was useful for anyone or someone could offer further suggestions!

My goal is: At runtime, only load the Atlas textures that are needed for the device resolution. So if the game is run on iPhone 3gs, only SD Atlases are loaded into memory, not HD or IPAD3-HD.

1. Set up all UISprites to use Reference type UIAtlas.

2. Create SD, HD, IPAD3-HD UIAtlases for the different resolutions. Place these in a Resources folder so they can be loaded at runtime.

3. Run a custom pre-build script that strips all UIAtlas references when building for iOS.

4. At runtime, loop through all loaded UIAtlas components and set the correct reference to either SD, HD or IPAD-HD depending on the device resolution. (Note only reference UIAtlases are loaded at this point)

Here is the code for looping through and sets the correct reference for all UIAtlas components in the game -

  1.  
  2.                 // find all LOADED UIAtlas components
  3.                 UIAtlas[] uiAtlases = Resources.FindObjectsOfTypeAll(typeof(UIAtlas)) as UIAtlas[];
  4.                                
  5.                 foreach(UIAtlas atlasRef in uiAtlases)
  6.                 {                      
  7.                         // If this is a reference atlas and the atlas dosen't have a replacement...
  8.                         if (atlasRef.name.Contains("-Ref") && atlasRef.replacement == null)
  9.                         {                                                              
  10.                                 // Gets the name of the atlas that needs to be used (SD, HD or IPAD-HD)
  11.                                 string resAppropriatePath = GetCorrectReplacementAtlasName(atlasRef);
  12.                                
  13.                                 // Loads the new atlas
  14.                                 UIAtlas replacementAtlas = Resources.Load(resAppropriatePath, typeof(UIAtlas)) as UIAtlas;
  15.                                                                
  16.                                 // Sets the new atlas as the replacment
  17.                                 atlasRef.replacement = replacementAtlas;
  18.                                
  19.                                 Debug.Log("UIHandlerClass setting replacementAtlas: "+replacementAtlas);
  20.                         }
  21.                 }
  22.  

I look forward to any sugestions on this!
« Last Edit: August 20, 2012, 01:00:20 PM by db82 »

ashishverma88

  • Guest
Re: Atlas Switching - only loading needed atlases
« Reply #1 on: August 26, 2012, 08:20:45 AM »
Hi dB82,

I am new to Unity javascripting. And I read your code lines. But was unable to use it. Sorry.
I can switch between SD and HD altas in the editor mode, as shown in the video demo at http://www.youtube.com/watch?v=ARfmGCMbJr8.

But, I am having trouble doing it automatically at runtime depending on device resolution (or more specifically device width in landscape mode).

Well, Can you please make an empty project and make a demo of it and post it on the forum or send to me via email.
Any help would be appreciated.

Thank you.
-ashish

dlewis

  • Guest
Re: Atlas Switching - only loading needed atlases
« Reply #2 on: August 26, 2012, 09:06:28 PM »
I am new to Unity javascripting

That code wasn't javascript, it was c#. NGUI is written in C# and only limited support can be given to javascript users (as the creators only use C# and from what I've seen, a lot of users on the forums only use C#).

ashishverma88

  • Guest
Re: Atlas Switching - only loading needed atlases
« Reply #3 on: August 27, 2012, 04:06:23 AM »
Hi dlewis and dB82,

@ dlewis:
Thankyou for your reply. When i used the code a c# script it didn't worked.
What i did is:
1. Created an empty gameObject, and added the UIAltas.cs script on it and marked it as 'Reference'
2. Created a prefab of this gameObject named "Main Atlas".
3. Created a c# script and named it "DynamicAltasSwitcher.cs" and attached this script to the "Main Atlas" prefab that i just created.
4. Then I added the code in the top of this post into the Start function of this script and saved it.
it gave me an error: "GetCorrectReplacementAtlasName do not exist in current context"

Please help me on how to use it.
Or can you please include this part also in the end of your this video demo  (SD to HD) http://www.youtube.com/watch?v=ARfmGCMbJr8


Many Thanks,
-Ashish

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Atlas Switching - only loading needed atlases
« Reply #4 on: August 27, 2012, 08:20:37 AM »
Looking at that code, "GetCorrectReplacementAtlasName" is simply the function that would figure out what atlas you should be using based on the target device's resolution.

theprojectabot

  • Guest
Re: Atlas Switching - only loading needed atlases
« Reply #5 on: February 16, 2013, 07:40:40 PM »
I found this method to be a great idea but in practice it fails.  The reason is that the Resource.FindAllOfType, only returns and array containing objects that have already been Resource.Load.... ed.

The better method would be to store hard refs to your reference atlas's , then in your resource switch controller, check what res you are working with, then with an organized folder structure load all the atlas's and do replacement that way.  Unfortunately this method loses the great option of just loading all UIAtlas's with a ultra def prefix... makes for more hand coding but with FindAllOfType only working on 'Loaded' assets, we are kinda stuck.