首页 文章

如何获取标准化值来更改Unity中的UI文本元素?

提问于
浏览
1

我有一个来自动画当前位置的标准化值 . 如何根据 string 中的值将其转换为文本输出?

例如

if (animation.Time < 0.1)
{
    text = January;
}

else (0.1 < animation.Time < 0.2)
{
    text = February;
}

等,直到1,因为标准化值 .

我意识到这段代码根本不起作用,但我认为这是让它工作所需的逻辑,但到目前为止,我没有运气 .

编辑,详解问题 . 我有一个滑块根据动画的进展而移动,它通过将animationTime转换为标准化值来实现这一点,以便滑块相对于动画填满 .

我想取这个标准化时间的值来在屏幕上显示动画的当前相关日期,所以如果动画显示年份进展,当滑块向上移动时,标准化值也可以有一些文本这将在几个月内计入 .

我希望现在更有意义 .

1 回答

  • 2

    要在Unity中获取“月份字符串”,请执行以下操作...

    说你有“3”..

    string monthString = new System.DateTime(1,3,1).ToString("MMMM");
    Debug.Log("Teste " + monthString );
    

    结果,“三月” .

    所以让自己成为一个功能

    private string MonthFromInt(int m)
     {
     string monthString = new System.DateTime(1, m ,1).ToString("MMMM");
     return monthString;
     }
    

    然后使用它 .


    关于您需要的控制结构 . 你提到它是“两个值之间” . 要做到这一点你就是

    if ( 0.00f < t && t <= 0.23f ) do something here...
    

    我建议KISS做以下事情 . 只需填写值:

    float t = animation.time (or whatever)
    string text = "?";
    
    if ( 0.00f <= t && t <= 0.23f ) text = MonthFromInt(0);
    if ( 0.23f < t && t <= 0.41f ) text = MonthFromInt(1);
    if ( 0.41f < t && t <= 0.66f ) text = MonthFromInt(2);
    if ( 0.66f < t && t <= 0.68f ) text = MonthFromInt(3);
    ... etc ...
    if ( 0.91f < t && t <= 1.00f ) text = MonthFromInt(11);
    

    使用“<”然后“<=”如上所述 . 希望能帮助到你!

相关问题