【发布时间】:2015-01-06 18:50:15
【问题描述】:
我目前正在制作一个生命条,它会慢慢消耗你的生命值。我按照本教程获得了我想要的健康条:https://www.youtube.com/watch?v=NgftVg3idB4
简而言之,它使用一个遮罩层和一个单独的绿色条来指示生命值。通过将绿色条向左移动,它会消失在遮罩层后面,从而显示出越来越少的健康状况。 根据生命值计算其位置的方法在这里: 代码(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
*/
这是我用于绘制健康栏的画布的设置。
问题是(可能)由于画布的缩放,healthTransform.rect.width 仍然返回健康栏的原始大小,而不是新的缩放大小。那么有没有办法找出 Canvas Scaler 缩放了多少?
【问题讨论】: