滑块代码,不起作为擦洗器,下降时不会移动?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了滑块代码,不起作为擦洗器,下降时不会移动?相关的知识,希望对你有一定的参考价值。

什么打破了擦洗滑块,它识别一个点击,但它不移动。我有可互动的启用。

我不得不重新导入所有资产以使其再次运行,但即使是现在这也不是一个肯定的赌注它会解决它。

一个星期前,这段代码工作得很好,从那以后我对它做了一些改动。

很多都是评论,所以我可以了解代码的不同部分。如果它困扰你,请随意删除它。

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处理程序。

How to use pointer handlers in modern 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!");
        }
    }

Don't forget the declaration...

Teste:MonoBehaviour, IPointerDownHandler, IPointerUpHandler

而不是通常的MonoBehavior

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

Note that in general these only work when they are "on" the thing in question.

运行,然后尝试单击滑块按钮上下 - 检查控制台。它的工作正常吗?现在你必须在NewAnimController中使用该变量

(1)向NewAnimController添加一个公共变量,

public Teste teste;

(2)确保拖动以在Inspector中连接该变量

(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”中的Down / Up例程时。相反,你可以不打扰ScrubSliderChanged,而是在句柄“向下”运行的代码中完成整个工作。

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

以上是关于滑块代码,不起作为擦洗器,下降时不会移动?的主要内容,如果未能解决你的问题,请参考以下文章

如何模仿 iPod 剩余时间和播放时间擦洗标签

更改曲目 NAudio 后滑块不起作用

滑块不起作用并更新图表中的值

Swift - UISlider 作为视频洗涤器的平滑度问题

如何以编程方式设置控制图像视图宽度的滑块

如何将 View 类中的代码片段移动到 OnAppearing() 方法?