检索从画布缩放器缩放量(统一) [英] Retrieving the amount of scaling from the Canvas Scaler (Unity)

查看:157
本文介绍了检索从画布缩放器缩放量(统一)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前工作的一个healthbar慢慢地排泄少的健康,你有。我跟着这个教程得到我想要的healthbars: https://www.youtube.com/watch? V = NgftVg3idB4

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

在短,它采用了掩模层和一个单独的绿条,以指示卫生的量。由移动绿色棒向左,它消失掩模层的后面,从而显示了越来越少的健康。 计算基于健康的量其位置的方法是在这里: code(CSHARP):

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
*/

这是我使用的healthbar画在画布上的设置。

This is the setting I use for the canvas the healthbar is drawn on.

麻烦的是(可能)因为的缩放画布确实,healthTransform.rect.width仍返回healthbar的原始大小,而不是新的,缩放大小。那么,有没有办法找出画布倍线器多少迈上东西?

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?

推荐答案

我问了同样的问题,在12月的统一论坛:

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. "

不过,不管多么小的UI获得,比例因子的值始终为1.0。我不知道这是为什么,和Unity不会再回应。有没有其他人有任何运气能够提取该值?

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?

作为一种变通方法,该RectTransform的对象上localScale你有CanvasScaler脚本上应该反映整个UI的当前规模。

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

这篇关于检索从画布缩放器缩放量(统一)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆