Author Topic: Script for PixelPerfect across all resolutions with atlas swapping  (Read 3071 times)

mahewitt

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 5
    • View Profile
I am trying to create a Pixelperfect GUI that will work across all resolutions. Practically fixed size seems to handle different resolutions easiest but without pixel perfect. You can work around this by increasing the UIRoot manualheight in fixed size mode as the resolution increases. Applying a scale factor to this for the different atlases then allows for have a larger pixel sized button for higher resolution textures. This keeps a fixed size output for each atlas across the target range and then allows for switching size when you have a new range / atlas.

A test script is included below. Putting this in Update() seems to work well in the editor when you resize the game window. Is this a valid way of doing things? I see there is a new Adjust by DPI setting, but I am unsure the differences with that and my method and how to simulate this without access to many different devices. Would it also work for all DPI's (or DPI ranges), or just a few key targeted ones?


   void Start() {
      int manualHeight;
      if (Screen.height >= 1080) {
         atlasRef.replacement = Resources.Load ("TestAtlas4x", typeof(UIAtlas)) as UIAtlas;
         manualHeight = (int)Screen.height;
      } else if (Screen.height >= 640) {
         atlasRef.replacement = Resources.Load ("TestAtlas2x", typeof(UIAtlas)) as UIAtlas;
         manualHeight = (int)(Screen.height * 2);
      } else {
         atlasRef.replacement = Resources.Load ("TestAtlas1x", typeof(UIAtlas)) as UIAtlas;
         manualHeight = (int)(Screen.height * 4);
      }

      UIRoot root = this.gameObject.GetComponent<UIRoot> ();
      if (root.scalingStyle != UIRoot.Scaling.FixedSize) {
         root.scalingStyle = UIRoot.Scaling.FixedSize;
      }

      if (root.manualHeight != manualHeight) {
         root.manualHeight = manualHeight;
      }

   }



Any help appreciated. Thanks

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Script for PixelPerfect across all resolutions with atlas swapping
« Reply #1 on: May 04, 2014, 10:37:59 AM »
The UIRoot size should remain the same as it was. Don't change it. Swap the atlases like you are doing now, but don't touch the UIRoot. Instead make sure that your atlases have a proper Pixel Size set. 1 for 1x, 0.5 for 2x, 0.25 for 4x.