我目前正在开发一个健康栏,它会慢慢消耗你较少的健康。我按照本教程获得了我想要的生命条:https://www.youtube.com/watch?v=NgftVg3idB4 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
*/
This is the setting I use for the canvas the healthbar is drawn on.
问题在于(可能)由于画布的缩放,healthTransform.rect.width 仍然返回健康栏的原始大小,而不是新的缩放后的大小。那么有没有办法知道 Canvas Scaler 缩放了多少东西呢?
我去年 12 月在 Unity 论坛上问过同样的问题:
http://forum.unity3d.com/threads/canvasscaler-current-scale.285134/ http://forum.unity3d.com/threads/canvasscaler-current-scale.285134/
Unity 给出的答案是:
“画布缩放后,您应该能够从画布上的scaleFactor 读取它,因为画布缩放器仅控制该值。”
但是,无论 UI 变得多么小,scaleFactor 的值始终为 1.0。不知道为什么,Unity再也没有回复过。还有其他人有幸能够提取这个值吗?
作为解决方法,您拥有 CanvasScaler 脚本的对象上的 RectTransform 的 localScale 应反映整个 UI 的当前比例。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)