Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Scaling UI elements under a canvas scaler

Discussion in 'UGUI & TextMesh Pro' started by jbooth, Jan 30, 2015.

  1. jbooth

    jbooth

    Joined:
    Jan 6, 2014
    Posts:
    5,445
    So, I have a screen/panel system that I use in my game to manage the transition of UI elements. For ages, it supported a very simple set of procedural transitions (fade in/out), or the ability to play an animation. But making animations for panels that do more than fade gets tedious fast - and is error prone since you want them all to look the same.

    So tonight I tried to convert this into a little modular system that lets you specify the transitions in a nicer way. Essentially, you can fade, scale, or move any panel on enter/exit/etc with a time and easing curve. The idea is that they can make nice juicy animations without touching the animator, and make sure they are consistent throughout the game.

    So here's my issue. The Canvas Scaler just sets the localScale on the objects below it. So if you have a smaller screen, it's going to set the localScale to .5 or something. So I can simply interpolate the scale of my panel between two absolute values; because those values depend on the canvas scaler.

    Is there a way to scale a UI element irregardless of what the canvas scaler is doing to the localScale? Preferably without having to insert extra parents into the hierarchy?

    I know canvas has a scaleFactor property, which perhaps can be multiplied by the desired scale to produce the correct scale - but this would require searching the hierarchy every frame to see what canvas we're in (since that could change at runtime), which seems horribly slow.
     
  2. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,870
    No, it sets the localScale of the GameObject the root canvas is on. Not on GameObject below it, though they will of course be affected by the scale of the parent, like it's always the case. But their own local scale will not be affected.

    Unless you're trying to scale the whole canvas with your script, I'm not sure I understand what the problem is. As long as the screen is a GameObject that's under the canvas and not the same GameObject as the Canvas is on itself, then you should be able to scale those screens with your script just fine.
     
  3. jbooth

    jbooth

    Joined:
    Jan 6, 2014
    Posts:
    5,445
    Ah, that's the issue - thanks. The panel I was testing on just happened to be a canvas when it really should have been contained by a canvas.