Author Topic: Switch between SD and HD atlas with a script!  (Read 14056 times)

divertap

  • Guest
Switch between SD and HD atlas with a script!
« on: April 14, 2012, 01:41:54 PM »
Hi,

I have a script that switches from SD to HD atlas. The script has 3 variables

   public UIAtlas iPhoneSDAtlas;
   public UIAtlas iPhoneHDAtlas;
   public UIAtlas gameGUI;

The last one is the atlas of type "reference". The script is attached to the UIRoot and on the awake method does:

Debug.Log("Screen resolution: " + Screen.currentResolution.width + ", "+Screen.currentResolution.height);
if ( Screen.currentResolution.width == 640 )
        {
            Debug.Log("Hight resolution!");
         gameGUI.replacement = iPhoneHDAtlas;
        }
else if ( Screen.currentResolution.width == 320 )
   {
      Debug.Log("Low resolution!");
gameGUI.replacement = iPhoneSDAtlas;
   }

The scripts does not work but I don't know why. The variables are initializated from the editor just dragging the prefabs for the 3 atlases. In fact, I don't know if I should change the variable "replacement", but there is not documentation regarding this...

Any ideas?
Tx,
M.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Switch between SD and HD atlas with a script!
« Reply #1 on: April 14, 2012, 02:18:01 PM »
Well, first of all, you're doing it wrong here. You should never be referencing all atlases, as doing so means they are ALL loaded into memory at the same time.

What you need to do is have your script reference the gameGUI atlas, and in Awake() it should Resources.Load the appropriate full atlas, updating the gameGUI's atlas reference.

You should also be basing the decision on the Mathf.Max(width, height), as I believe the width and height may change due to the phone being rotated.

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Switch between SD and HD atlas with a script!
« Reply #2 on: April 14, 2012, 02:57:40 PM »
Everything Aren says is true. Resources.Load is much better memory wise. If you only have a few atlases, it's negligible though - 10 MB extra won't kill you, and if it does - then do Resources.Load.

I can't remember what Unity's Screen.width/height does when you rotate the device - check unity's reference for that. The solution put forth does handle that, but it "might" not be needed.

Your individual widgets should point to the gameGUI atlas, not any of the others. If you edit one of the atlases, it some times borks and changes your widgets references - if that happens, just go into your gameGUI atlas and set it to high and low again, then the widget change back. Odd behavior to be sure, but it works. :)

Also, you should keep it in the other thread http://www.tasharen.com/forum/index.php?topic=37.0 since it's essentially a continuation of that discussion.

divertap

  • Guest
Re: Switch between SD and HD atlas with a script!
« Reply #3 on: April 14, 2012, 03:33:12 PM »
Ok. Could you write the piece of code to load the atlas and update the reference of the atlas?

And also, is it ok to attach the script to the uiroot?

Tx,
M.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Switch between SD and HD atlas with a script!
« Reply #4 on: April 14, 2012, 06:07:34 PM »
You can attach things to UIRoot, sure. As for loading the atlas? Just:
  1. Resources.Load("YourHDAtlasName").GetComponent<UIAtlas>();
Be sure that the YourHDAtlasName prefab is in the Resources folder, or Unity won't be able to load it.

divertap

  • Guest
Re: Switch between SD and HD atlas with a script!
« Reply #5 on: April 14, 2012, 07:10:05 PM »
Aren, I cannot get things working. I've tried the following:

Object atlas = Resources.Load("iPhone HD");
Debug.Log("New atlas: "+atlas);
gameGUI.replacement = ((GameObject)atlas).GetComponent<UIAtlas>();

On the console I get:

New atlas: iPhone HD (UnityEngine.GameObject) on GUI: Game GUI (UIAtlas)   (all seems ok!)

But on the iphone screen I don't see the HD altas. The gameGUI var is the atlas of type "reference". But, once loaded the new atlas (the HD), how do I update the reference attribute on the gameGUI in code?

M.

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Switch between SD and HD atlas with a script!
« Reply #6 on: April 14, 2012, 08:42:07 PM »
Here's some code for you off the top of my head

  1. UIAtlas myAtlas = Resources.Load("iPhone HD") as UIAtlas;
  2. gameGUI.replacement = myAtlas;
  3.  

You should not change the type from reference, since it's still just referencing the HD atlas.

Make sure that you're actually loading the atlas. Resources.Load takes a full path, if you have subfolders to Resources. Assume Resources is your root folder.

Also make sure that your widgets are using the gameGUI as their atlas, not your SD atlas.

divertap

  • Guest
Re: Switch between SD and HD atlas with a script!
« Reply #7 on: April 15, 2012, 04:10:19 AM »
Ok. My widgets weren't pointing to the atlas references. I have changed them and now seems that the reference to the HD atlas is updated. But it seems that the sprites does not find their piece of texture inside the atlas and nothing is drawn. It's like if the Load() atlas does not find the material or something like this. Any ideas?

I also see that the AtlasMaker names the material, the texture and the atlas with the same name. Is it ok?

M.   

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Switch between SD and HD atlas with a script!
« Reply #8 on: April 15, 2012, 04:12:01 AM »
You need to ensure that the names of sprites within your SD and HD atlases match. That is, if one has a sprite called "Hello World", another one must have the same sprite, just of different size.

Yes, different types of objects having the same name is fine -- they have a different extension.

divertap

  • Guest
Re: Switch between SD and HD atlas with a script!
« Reply #9 on: April 15, 2012, 04:49:48 AM »
Nothing... I think that the Resources.Load is not enought. Why? I have tried the following test:

1) Create a public var called "currentGameAtlas" and assign it dragging the HD atlas from the editor. And then:
 if ( Screen.currentResolution.width == 640 )
        {
            Debug.Log("Hight resolution!");
       Debug.Log("New atlas: "+currentGameAtlas+" on GUI: "+gameGUI);
      gameGUI.replacement = currentGameAtlas;
        }

It works!!! (Despite the memory use thing...)

2) Do not create the public var and instead use the Resources.Load like this:

 if ( Screen.currentResolution.width == 640 )
        {
            Debug.Log("Hight resolution!");
      UIAtlas currentLocalAtlas = Resources.Load("iPhone HD") as UIAtlas;
         Debug.Log("New atlas: "+currentLocalAtlas+" on GUI: "+gameGUI);
         gameGUI.replacement = currentLocalAtlas;
        }

It does not work!!!

So, anybody has tested it? Should I load something else?

Tx,
M.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Switch between SD and HD atlas with a script!
« Reply #10 on: April 15, 2012, 05:03:35 AM »
Yes, it has been tested, and used by more than a few people now. I suggest looking into how Resources.Load works. If you can't load it, then the problem is with how you're using it.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Switch between SD and HD atlas with a script!
« Reply #11 on: April 15, 2012, 05:07:15 AM »
And as Nicki mentioned, you need to specify the path to your resource, not just the resource's name.

divertap

  • Guest
Re: Switch between SD and HD atlas with a script!
« Reply #12 on: April 15, 2012, 06:46:24 AM »
I have the iPhone HD atlas into a folder named "Resources" from the Project Tab.

I have tried to load Resources.Load("iPhone HD") and Resources.Load("Resources/iPhone HD") but both fails to load the atlas. What should be the rigth path to load?

M.

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Switch between SD and HD atlas with a script!
« Reply #13 on: April 15, 2012, 09:19:42 AM »
Consider renaming your resource to not have a space in it, that might make it break.

Other than that, your paths should be like this:

  1. UIAtlas myAtlas = Resources.Load("iphonehd", typeof(UIAtlas)) as UIAtlas;
  2.  
if it's directly in the Resources folder, otherwise you would do like this if it's in a subfolder named atlases

  1. UIAtlas myAtlas = Resources.Load("atlases/iphonehd", typeof(UIAtlas)) as UIAtlas;
  2.  

Look into this: http://unity3d.com/support/documentation/ScriptReference/Resources.Load.html

loopyllama

  • Guest
Re: Switch between SD and HD atlas with a script!
« Reply #14 on: May 16, 2012, 07:29:24 AM »
Nicki's way definitely works.
You can also do:
  1. UIAtlas myAtlas = (Resources.Load("iphonehd") as GameObject).GetComponent<UIAtlas>();
although I think Nicki's way looks cleaner. This way is a more verbose but maybe it can help you see what is happening?