【问题标题】:Retrieving the amount of scaling from the Canvas Scaler (Unity)从 Canvas Scaler (Unity) 检索缩放量
【发布时间】: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 缩放了多少?

【问题讨论】:

    标签: c# unity3d 2d


    【解决方案1】:

    去年 12 月,我在 Unity 论坛上问过同样的问题:

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

    Unity 的回答是: “在画布被缩放后,你应该能够从画布上的 scaleFactor 中读取它,因为画布缩放器只是控制这个值。”

    但是,无论 UI 变得多小,scaleFactor 的值始终是 1.0。我不知道这是为什么,Unity 再也没有回应。有没有其他人运气好能够提取这个值?

    作为一种解决方法,您拥有 CanvasScaler 脚本的对象上的 RectTransform 的 localScale 应该反映整个 UI 的当前比例。

    【讨论】:

    • 非常感谢!这正是我的问题,我已经想知道 scaleFactor 的真正用途。但是非常感谢,这让我难倒了很长一段时间,你让我很开心!
    【解决方案2】:

    有一种更简单的方法来处理绿色条的宽度 - 将其设为“填充”图像类型。这样你就根本不需要面具,这大大简化了事情。


    在 Unity 4.6+ 中使用填充创建健康条

    创建一个包含您需要的图像的画布对象(在本例中为红色背景栏、绿色前景栏和轮廓)。

    按此顺序放置时,最低的图像将出现在顶部(见下文)。

    结果将是一个如下所示的健康栏:

    但是,现在我们需要能够根据生命值来控制绿色条的宽度。为此,我们可以修改“绿条”的图像组件:

    • 将类型更改为“填充”
    • 将填充方法更改为“水平”
    • 将填充原点更改为“左”。

    现在可以通过修改填充量来控制绿色条的宽度。 (您可以使用滑块进行测试)

    最后,您必须添加一个脚本来设置绿色条的宽度,在 C# 中可能如下所示:

    using UnityEngine;
    using System.Collections;
    using UnityEngine.UI;
    
    public class HealthBar : MonoBehaviour {
    
        public float MaxHealth = 100;
        private float _playerHealth;
        private Image _greenBar
    
        void Start () {
                _playerHealth = MaxHealth;
                _greenBar = transform.FindChild("GreenBar").GetComponent<Image>();
        }
    
        void Update () {
    
                //logic to set _playerHealth goes here
    
                _greenBar.fillAmount = _playerHealth/MaxHealth;
        }
    }
    

    【讨论】:

      【解决方案3】:

      我今天恰好遇到了这个问题(问这个问题大约三年后),并发现通过在 Start()Update() 的 Canvas 对象上使用 scaleFactor,我能够得到正确的因子缩放蒙版移动。不过貌似Awake()中设置的值不可靠,可能会引起一些悲痛。

      为了将来参考,您不需要任何解决方法,正如 Farmer Joe 的回答中所建议的那样。如果曾经有过错误——我对此表示怀疑——它已经被修复了。

      【讨论】:

        猜你喜欢
        • 2023-03-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-03
        • 2013-03-29
        • 1970-01-01
        相关资源
        最近更新 更多