SliderObject.onValueChanged侦听器,需要很长时间作为参数 [英] SliderObject.onValueChanged listener taking long as a parameter

查看:149
本文介绍了SliderObject.onValueChanged侦听器,需要很长时间作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要用户通过滑块传递long.但是我不能添加long作为参数作为滑块的侦听器的函数.参见事件"下的.

I need the user to pass a long via a slider. However I can't add a function that takes long as a parameter as a listener to the slider. See this under "Events".

浮点导致精度损失问题.我该如何解决?

The float causes a precision loss issue. How can I work around this?

我当前正在使用的脚本与此相似.监听器函数使用float:

The script I'm currently using is similar to this. The listener function takes a float:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.Events;

public class SliderMessageBox : MonoBehaviour
{
  public Slider SliderObject;
  public Text SliderFeedback;

  public void Function(long MaximumValue)
  {
    SliderObject.minValue = 1000;
    SliderObject.maxValue = MaximumValue;
    SliderObject.onValueChanged.AddListener(SetFeedback);
    SetFeedback(SliderObject.value);
  }

  void SetFeedback(float value)
  {
    SliderFeedback.text = value.ToString("#");
  }
}

推荐答案

每次单击Button时,您不会调用SliderObject.onValueChanged.AddListener(SetFeedback);.您应该一次调用该函数,以注册到一个滑块值更改事件.该行代码应移至Start()函数.

You don't call SliderObject.onValueChanged.AddListener(SetFeedback); each time a Button is clicked. You should call that function once, to register to a slider value change event. That line of code should be moved to the Start() function.

关于在滑块值更改时将long传递给SetFeedback函数,这是可能,但完全没有用.这样做的原因是因为Slider组件旨在与minValuemaxValuevalue属性中的浮点数一起使用.将long分配给这些属性将无法解决您的精度问题.

As for passing long to the SetFeedback function when slider value changes, that is possible but totally useless. The reason for this is because the Slider component is designed to work with with floats in its minValue, maxValue and value properties. Assigning long to these properties will not solve your precision problem.

如果您仍然想这样做,可以使用delegate和强制转换来做到这一点:

If you still want to do that, here is a way to do it with delegate and casting:

public Slider SliderObject;
public Text SliderFeedback;

void Start()
{
    SliderObject.onValueChanged.AddListener(delegate { SetFeedback((long)SliderObject.value); });
}

public void Function(long MaximumValue)
{
    SliderObject.minValue = 1000;
    SliderObject.maxValue = MaximumValue;
    SetFeedback((long)SliderObject.value);
}

void SetFeedback(long value)
{
    SliderFeedback.text = value.ToString();
}

应该可以编译,但是解决问题的正确方法是实现自己的自定义Slider组件并将其minValuemaxValuevalue属性设置为long.您可以借助Image组件来实现.

That should compile but the correct way to fix your problem is to implement your own custom Slider component and make its minValue, maxValue and value properties to be long. You can implement this with the help of Image component.

此外,Unity UI是开源的.您还可以从此处获取源代码,复制Slider脚本,将其命名为BigSlider然后将所有float变量更改为long.

Also, Unity UI is open source. You can also get the source code from here, duplicate the Slider script, name it BigSlider then change all float variables to long.

这篇关于SliderObject.onValueChanged侦听器,需要很长时间作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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