滑块代码,不能用作洗涤器,下降时不会移动?



破坏的是擦洗滑块,它识别出点击,但它不移动。我有可交互的启用。

我必须重新导入所有资产才能使其再次工作,但即使是现在,这也不是一个确定的赌注,它会修复它。

大约一周前,这段代码运行良好,从那以后我对它进行了很少的更改。

其中很多都被注释了,所以我可以学习代码的不同部分。如果它困扰您,请随时删除它。

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class NewAnimController : MonoBehaviour {

public Slider scrubSlider;    //This is the slider that controls the current position of the animation. Values have to be 0 to 1 for normalized time of the animation, called later in the script
public Slider speedSlider;       //This is the slider that controls the playback speed of the animation
public Animator animator;        //Animator that is attached to the model that we are looking to control the animation of
public float playbackSpeedAdjustment = 0.5f;  //This is a variable that can be easily adjusted to change the total playback speed of the animation. If it's too fast make smaller and vice versa
public Text currentDateText;
public int monthsInProject;
private float rememberTheSpeedBecauseWeMightNeedIt;  //Float needed to keep the speed of the animation between pausing and playing
public void Update()
{
    float animationTime = animator.GetCurrentAnimatorStateInfo(0).normalizedTime;   //float a new value animationTime that is equal to the animators animation state then converted to normalized time
    //Debug.Log("animationTime (normalized) is " + animationTime);   //logging the normalized time to be able to store it for the scrubbing slider. Doesn't need to be logged for this to work, good to log it to make sure that it's working
    scrubSlider.value = animationTime;  //making the slider value equal to the now logged normalized time of the animation state
}

public void ScrubSliderChanged(float ScrubSliderchangedValue)  // this value has to be floated so that the scrubbing slider can be attached to in the inspector to be able to change the current frame of the animation
{
    animator.Play("Take 001", -1, scrubSlider.normalizedValue);
}

public void SpeedSliderChanged(float SpeedSliderchangedValue)  //value is floated to be able to change the speed of the animation playback
{
    animator.speed = speedSlider.normalizedValue * playbackSpeedAdjustment;  // here the speed is multiplied by the adjustment value set in the editor just in case the playback speed needs to be changed outside of normalized values
}

public void UserClickedPauseButton()
{
    if (animator.speed > 0f)
    {
        // we need to pause animator.speed
        rememberTheSpeedBecauseWeMightNeedIt = animator.speed;
        animator.speed = 0f;
    }
    else
    {
        // we need to "unpause"
        animator.speed = rememberTheSpeedBecauseWeMightNeedIt;
    }
}
public void UserClickedBackButton()
{
    scrubSlider.value = scrubSlider.value - (1f / monthsInProject);
}
public void UserClickedForwardButton()
{
    scrubSlider.value = scrubSlider.value + (1f / monthsInProject);
}
public void UserClickedStartButton()
{
    scrubSlider.value = 0;
}
public void UserClickedEndButton()
{
    scrubSlider.value = 1;
}
}

非常感谢您的帮助。

我很

确定问题是这个。在Update中,您正在执行如下操作:

scrubSlider.value = animationTime

这意味着每一帧,"无论如何",你都在设置滑块位置,到,动画所在的位置。如果用户尝试移动滑块 - 您正在将其向后移动,相同的帧!

解决这个问题并不容易。Unity 没有为此包含一个微不足道的内置函数。您需要一个单独的脚本,该脚本位于滑块上,用于确定是否按住手柄。您必须使用 OnPointerDownOnPointerUp 处理程序。

如何在现代 Unity 中使用指针处理程序:

第一步,制作这个叫做Teste.cs的小脚本

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;
public class Teste:MonoBehaviour, IPointerDownHandler, IPointerUpHandler
    {
    public Slider theSlider;
    public bool sliderIsBeingHeldDown;
    public void OnPointerDown(PointerEventData data)
        {
        sliderIsBeingHeldDown = true;
        Debug.Log("holding down!");
        }
    public void OnPointerUp(PointerEventData data)
        {
        sliderIsBeingHeldDown = false;
        Debug.Log("holding up!");
        }
    }

不要忘记宣言...

Teste:MonoBehaviour, IPointerDownHandler, IPointerUpHandler

而不是通常的MonoBehavior.

将该脚本拖到您的UI.Slider上。

请注意,一般来说,这些只有在它们"在"所讨论的东西上时才有效。

运行,并尝试上下单击滑块按钮 - 检查控制台。 它有效吗?现在您必须在 NewAnimController 中使用该变量

(1(在NewAnimController中添加一个公共变量,

public Teste teste;

(2( 请务必拖动以在检查器中连接该变量

(3(现在,您可以参考teste.sliderIsBeingHeldDown以查看是否按住该滑块。因此,与其每帧都这样做...

scrubSlider.value = animationTime;

你必须每一帧都这样做...

if (teste.sliderIsBeingHeldDown)
  Debug.Log("don't try to work against the user!");
else
  scrubSlider.value = animationTime;

我希望这有效! 我认为这是知道何时按住滑块的最简单方法。

你真的为你的第一个项目选择了一个困难的项目!地狱!

值得注意的是,您还可以将滑块调整代码(我的意思是说洗涤器的实际代码("放在"实际在滑块上的脚本中 - 但我现在不担心这一点。您的解决方案很好。

请注意,根据实现它的方式,您可能需要在按住滑块手柄时但在移动滑块手柄之前使其更优雅地暂停。为了实现这一点,一种方法是安排调用UserClickedPauseButton()或类似的概念,当调用"Teste"中显示的向下/向上例程时。相反,你可以不打扰ScrubSliderChanged,而是在句柄"关闭"时运行的代码中完成整个工作。

(一般来说,您几乎肯定会在这里使用UnityEvent来为滑块制作一个可靠的可重用解决方案,例如与洗涤器之类的东西一起使用。

最新更新