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

时间:2016-02-15 17:01:31

标签: c# unity3d slider

擦洗滑块有什么断裂,它识别出咔嗒声,但它不会移动。我有可互动的启用。

我必须重新导入所有资产才能让它再次发挥作用,但即使是现在这也不能确定它会解决它。

这个代码在一周前工作得非常好,我从那时起就做了很少的改动。

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

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;
}
}

非常感谢你的帮助。

1 个答案:

答案 0 :(得分:2)

我很确定问题是这个。在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)确保拖动以在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为滑块制作一个可靠的可重用解决方案,例如用于擦洗器之类的东西。)