Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving the amount of scaling from the Canvas Scaler (Unity)

Tags:

c#

unity3d

2d

I am currently working on a healthbar which slowly drains the less health you have. I followed this tutorial to get the healthbars I wanted: https://www.youtube.com/watch?v=NgftVg3idB4

In short, it uses a mask layer and a seperate green bar to indicate the amount of health. By moving the green bar to the left, it disappears behind the mask layer and thus shows less and less health. The method to calculate its position based on the amount of health is here: Code (CSharp):

float maxXValue = healthTransform.position.x;
float minXValue = healthTransform.position.x - healthTransform.rect.width;

private void HandleHealth()
{
    Stats attachedStats = attachedObject.GetComponent<Stats>();

    float currentXValuePlayer = MapValues(attachedStats.currentHealth, 0, attachedStats.maxHealth, minXValue, maxXValue);
    healthTransform.position = new Vector3(currentXValuePlayer, cachedY);
}

private float MapValues(float x, float inMin, float inMax, float outMin, float outMax)
{
    return (x - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
}
/*
EXPLANATION:
attachedStats.currentHealth is the current health the player has
attachedStats.maxHealth is the maximum amount of health the player can have
minXValue is the furthest left point the bar is at when health is 0
maxXValue is the furthest right point the bar is at when health is full
*/

This is the setting I use for the canvas the healthbar is drawn on. [​IMG]

The trouble is(probably) that because of the scaling the canvas does, healthTransform.rect.width still returns the original size of the healthbar and not the new, scaled size. So is there a way to find out how much the Canvas Scaler has scaled things?

like image 501
Lionlake Avatar asked Jan 06 '15 18:01

Lionlake


People also ask

How do I get UI to scale with screen size in unity?

In such cases, we can add it again by clicking inside the Inspector window on Add Component and then going to Layout | Canvas Scaler. Next, we have to change the Ui Scale Mode property to Scale With Screen Size and ensure that Screen Match Mode is set to Match Width Or Height.

What is scale factor in unity?

The Virtual Scene Scale Factor configures how many Unity scene units correspond to one meter in the physical world. Poses reported by Vuforia will be transformed accordingly, but scene content will not be scaled.

How do I change the canvas ratio in unity?

To change the screen size, go to the Game window (accessed by the menu "Window->General->Game"). At the top of the window will be a screen size menu (outlined in green, below) to change the screen size. When pressed, it will display the list of screen sizes that will be emulated.


1 Answers

I asked the same question back in December on the Unity forums:

http://forum.unity3d.com/threads/canvasscaler-current-scale.285134/

The answer from Unity was: "After the canvas has been scaled you should be able to just read it back from the scaleFactor on the canvas as the canvas scaler just controls this value. "

But, no matter how small the UI gets, the value of scaleFactor is always 1.0. I don't know why this is, and Unity never responded again. Has anyone else had any luck being able to extract this value?

As a workaround, The RectTransform's localScale on the object you have CanvasScaler script on should reflect the current scale of the entire UI.

like image 121
Famer Joe Avatar answered Sep 25 '22 07:09

Famer Joe