Author Topic: Atlas Switching script  (Read 55964 times)

FizzPow

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 83
    • View Profile
    • FizzPow Games
Re: Atlas Switching script
« Reply #30 on: December 28, 2012, 01:23:45 PM »
FYI, here's the solution I quickly whipped up to automate clearing all Atlas and Font reference prefabs in editor:

  1.     [MenuItem("Custom/Clear References")]
  2.     static void ClearRefs()
  3.     {
  4.         UIAtlas[] atlases = (UIAtlas [])Resources.FindObjectsOfTypeAll(typeof(UIAtlas));
  5.         foreach (UIAtlas atlas in atlases)
  6.         {
  7.             if (atlas.replacement != null)
  8.             {
  9.                 Debug.Log(string.Format("atlas: {0} reference: {1} set to null", atlas.name, atlas.replacement.name));
  10.                 atlas.replacement = null;
  11.                 atlas.spriteMaterial = null;
  12.             }
  13.         }
  14.  
  15.         UIFont[] fonts = (UIFont[])Resources.FindObjectsOfTypeAll(typeof(UIFont));
  16.         foreach (UIFont font in fonts)
  17.         {
  18.             if (font.replacement != null)
  19.             {
  20.                 Debug.Log(string.Format("font: {0} reference: {1} set to null", font.name, font.replacement.name));
  21.                 font.replacement = null;
  22.                 font.material = null;
  23.             }
  24.  
  25.         }
  26.     }
  27.  

Edit: added setting the materials to null too, if they were set prior to switching to reference, they are still there otherwise.
« Last Edit: December 28, 2012, 05:12:21 PM by FizzPow »

filo

  • Guest
Re: Atlas Switching script
« Reply #31 on: January 11, 2013, 09:14:48 AM »
FYI, here's the solution I quickly whipped up to automate clearing all Atlas and Font reference prefabs in editor:

Nubie question. How do you use this script?

FizzPow

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 83
    • View Profile
    • FizzPow Games
Re: Atlas Switching script
« Reply #32 on: January 11, 2013, 08:19:16 PM »
Put it in a script in a directory called 'Editor', and it will show up as a menu item in the Unity editor under 'Custom' at the top.  That's just the function, so the full file/class might look like this:

  1. using UnityEditor;
  2. using UnityEngine;
  3.  
  4. public class EditorHelpers : MonoBehaviour {
  5.  
  6.     [MenuItem("Custom/Clear References")]
  7.     static void ClearRefs()
  8.     {
  9.         UIAtlas[] atlases = (UIAtlas[])Resources.FindObjectsOfTypeAll(typeof(UIAtlas));
  10.         foreach (UIAtlas atlas in atlases)
  11.         {
  12.             if (atlas.replacement != null)
  13.             {
  14.                 Debug.Log(string.Format("atlas: {0} reference: {1} set to null", atlas.name, atlas.replacement.name));
  15.                 atlas.replacement = null;
  16.                 atlas.spriteMaterial = null;
  17.             }
  18.         }
  19.  
  20.         UIFont[] fonts = (UIFont[])Resources.FindObjectsOfTypeAll(typeof(UIFont));
  21.         foreach (UIFont font in fonts)
  22.         {
  23.             if (font.replacement != null)
  24.             {
  25.                 Debug.Log(string.Format("font: {0} reference: {1} set to null", font.name, font.replacement.name));
  26.                 font.replacement = null;
  27.                 font.material = null;
  28.             }
  29.         }
  30.     }
  31. }
  32.  

FritzBraun

  • Guest
Re: Atlas Switching script
« Reply #33 on: January 30, 2013, 09:27:00 AM »
Thanks FizzPow for the script, very useful. I've extended it to have a restore button that will change the references back to the way they were before you hit clear. This was useful for me, so I'm posting in case it's useful for anyone else.
  1. using UnityEditor;
  2. using UnityEngine;
  3. using System.Collections.Generic;
  4.  
  5. public class NGuiAtlasHelper : MonoBehaviour
  6. {
  7.         class StoredAtlasRefs
  8.         {
  9.                 public UIAtlas replacement;
  10.                 public Material spriteMaterial;
  11.         }
  12.        
  13.         class StoredFontRefs
  14.         {
  15.                 public UIFont replacement;
  16.                 public Material material;
  17.         }
  18.        
  19.         static Dictionary<UIAtlas,StoredAtlasRefs> AtlasDictionary;
  20.         static Dictionary<UIFont,StoredFontRefs> FontDictionary;
  21.                
  22.  
  23.     [MenuItem("NGuiAtlasHelper/Clear References")]
  24.     static void ClearRefs()
  25.     {
  26.                 if(AtlasDictionary == null)
  27.                 {
  28.                         AtlasDictionary = new Dictionary<UIAtlas, StoredAtlasRefs> ();
  29.                 }
  30.                
  31.         UIAtlas[] atlases = (UIAtlas[])Resources.FindObjectsOfTypeAll(typeof(UIAtlas));
  32.         foreach (UIAtlas atlas in atlases)
  33.         {
  34.             if (atlas.replacement != null)
  35.             {
  36.                 Debug.Log(string.Format("atlas: {0} reference: {1} set to null", atlas.name, atlas.replacement.name));
  37.                                
  38.                                 StoredAtlasRefs atlas_ref = new StoredAtlasRefs();
  39.                                 atlas_ref.replacement = atlas.replacement;
  40.                                 atlas_ref.spriteMaterial = atlas.spriteMaterial;
  41.                                
  42.                                 AtlasDictionary[atlas] = atlas_ref;
  43.                                
  44.                                 atlas.replacement = null;
  45.                 atlas.spriteMaterial = null;
  46.                                
  47.             }
  48.         }
  49.                
  50.                 if(FontDictionary == null)
  51.                 {
  52.                         FontDictionary = new Dictionary<UIFont, StoredFontRefs> ();
  53.                 }
  54.                
  55.         UIFont[] fonts = (UIFont[])Resources.FindObjectsOfTypeAll(typeof(UIFont));
  56.         foreach (UIFont font in fonts)
  57.         {
  58.             if (font.replacement != null)
  59.             {
  60.                 Debug.Log(string.Format("font: {0} reference: {1} set to null", font.name, font.replacement.name));
  61.                                
  62.                                 StoredFontRefs font_ref = new StoredFontRefs();
  63.                                 font_ref.replacement = font.replacement;
  64.                                 font_ref.material = font.material;
  65.                                
  66.                                 FontDictionary[font] = font_ref;
  67.                                
  68.                                
  69.                                 font.replacement = null;
  70.                 font.material = null;
  71.             }
  72.         }
  73.     }
  74.        
  75.     [MenuItem("NGuiAtlasHelper/Restore References")]
  76.     static void RestoreRefs()
  77.     {
  78.                 if(AtlasDictionary != null)
  79.                 {
  80.                         foreach(KeyValuePair<UIAtlas,StoredAtlasRefs> entry in AtlasDictionary)
  81.                         {
  82.                             entry.Key.replacement = entry.Value.replacement;
  83.                                 entry.Key.spriteMaterial = entry.Value.spriteMaterial;
  84.                 Debug.Log(string.Format("atlas: {0} reference: {1} restored", entry.Key.name, entry.Key.replacement.name));
  85.                         }
  86.                        
  87.                         AtlasDictionary = null;
  88.                 }
  89.                
  90.                 if(FontDictionary != null)
  91.                 {
  92.                         foreach(KeyValuePair<UIFont,StoredFontRefs> entry in FontDictionary)
  93.                         {
  94.                             entry.Key.replacement = entry.Value.replacement;
  95.                                 entry.Key.material = entry.Value.material;
  96.                 Debug.Log(string.Format("Font: {0} reference: {1} restored", entry.Key.name, entry.Key.replacement.name));
  97.                         }
  98.                        
  99.                         FontDictionary = null;
  100.                 }
  101.     }
  102.        
  103. }
  104.  

ashwanikumar04

  • Guest
Re: Atlas Switching script
« Reply #34 on: April 01, 2013, 11:00:54 AM »
Hello,
I am following the same approach for switching the atlases on runtime. But I am facing an issue.
I am explained my issue here.
http://www.tasharen.com/forum/index.php?topic=3660.msg18039

sonicviz

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 58
    • View Profile
Re: Atlas Switching script
« Reply #35 on: May 01, 2013, 11:11:51 PM »
Hi,
I'm still grappling with this problem, even after looking at this and Asset Store solutions.

Essentially there are two issues, positioning - which anchoring takes care of, and scaling - which seems can be handled a couple of different ways.

Shouldn't the scaling threshold switching be triggered by http://docs.unity3d.com/Documentation/ScriptReference/Screen-dpi.html and not screen resolutions per se?


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Atlas Switching script
« Reply #36 on: May 02, 2013, 03:45:35 AM »
DPI setting doesn't work correctly. It's based on guesswork.

sonicviz

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 58
    • View Profile
Re: Atlas Switching script
« Reply #37 on: May 02, 2013, 03:59:33 AM »
ah, ty. That settles that then!

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Atlas Switching script
« Reply #38 on: May 02, 2013, 08:26:05 AM »
On most mobiles Screen.dpi does work, but obviously it doesn't work on PC, Mac etc, because you can change the screen there.

sonicviz

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 58
    • View Profile
Re: Atlas Switching script
« Reply #39 on: May 02, 2013, 09:31:01 PM »
I'm only interested in it for mobiles.

I'm even more confused now. What is the evidence for it being guesswork, or conversely accurate?
Why is it in the API if it's BS?

[though I guess Aren should be pretty definitive source as he is working with Unity on the Gui!]

Not trying to cause controversy here, just wanting to get a complete accurate picture of what works and what doesn't.
« Last Edit: May 02, 2013, 10:00:58 PM by sonicviz »

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Atlas Switching script
« Reply #40 on: May 04, 2013, 08:14:41 PM »
I'm gonna be countering then that it works just fine, you just need to have fallbacks if it returns 0 and make your own guesswork then.

As far as I know, it works for iOS and Android - not sure about other phones like Windows phone.

TokyoDan

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 53
    • View Profile
Atlases are properly replaced but sizes do not change.
« Reply #41 on: September 19, 2013, 08:54:51 PM »
Atlases are properly replaced but sized do not change.

I had a similar problem before with sliced spites but that was because I had the atlas pixel sizes set wrong: http://www.tasharen.com/forum/index.php?topic=5181.msg24795#msg24795

This time I have the pixel sizes set right BUT ALL sprite AND UI elements do not scale.

I am using NGUI 2.7.0.

1. I have an 40x40 sprite in "SDAtlas". Atlas Pixel Size = 1
2. I have an 80x80 sprite in "HDAtlas". Atlas Pixel Size = 0.5

3. I have "MainAtlas" setup as a "Reference" Atlas Type and it points to "SDAtlas".
4. I use the Widget Tool to create a Sprite in the Scene using "MainAtlas".

The sprite is 40x40

I can see that the SD and HD atlases are properly swapped depending on how big I make my Game window (set to Free Aspect). But. The sprite size never changes... it is locked to 40x40:

When "SDAtlas" is swapped in, the 40x40 sprite is displayed correctly at 40x40.
But when "HDAtlas" is swapped in, the 80x80 sprite is displayed but it scaled down to 40x40.

Why,

Shouldn't it be 80x80 so that when looking at it on a HD screen it will look to be the same relative size as a 40x40 sprite on a SD screen?
« Last Edit: September 19, 2013, 11:36:26 PM by TokyoDan »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Atlas Switching script
« Reply #42 on: September 20, 2013, 03:39:22 AM »
The sprite size isn't supposed to change. Only the pixel density changes.

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Atlas Switching script
« Reply #43 on: September 20, 2013, 05:05:41 PM »
It's about how your UIRoot is set as well. If it's at pixel perfect, then it should change to 80x80 - but that's not what that solution with the pixel densities is built for; it's built for FixedSize, so that the virtual pixel height stays the same, but the actual screen resolution doubles on high res, thus filling in 2 pixels of information on each virtual pixel.

TokyoDan

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 53
    • View Profile
Re: Atlas Switching script
« Reply #44 on: September 20, 2013, 07:15:27 PM »
Thank you Nicki. I got it figured out and everything works now. Funny thing is I added another UI and forgot how I set it up a fe months ago when it was working so I dug out an old backup and compared what I  did before. It was the  UIRoot was set to at pixel perfect instead of FixedSizeOnMobiles.