2016-02-15 31 views
1

擦洗滑塊有什麼突破,它識別出咔嗒聲,但它不會移動。我有可交互的啓用。滑塊代碼,不起作用的洗滌器,下降時不會移動?

我不得不重新導入所有資產以使其再次正常工作,但即使現在也不能肯定它會修復它。

這段代碼在一個星期前工作得很好,我對它做了一些小改動。

它有很多評論,所以我可以瞭解代碼的不同部分。隨時刪除它,如果它困擾你。

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

非常感謝您的全力幫助。

+0

'NewAnimController'不是好名字的傢伙。應該像'Scrubber',也許。 – Fattie

+0

對於使用Slider的人來說,這將是一個非常有趣的問題,所以我編輯了一些不相關的代碼塊。好的 – Fattie

回答

2

我很確定問題是這樣的。在Update你正在做這樣的事情:

scrubSlider.value = animationTime 

這意味着每一幀,「不管」,您正在設置滑塊位置,來,這裏的動畫。如果用戶試圖移動滑塊 - 您正在將它移回到原來的位置!

解決這個問題並不容易。 Unity沒有包含一個簡單的內置函數。您需要一個單獨的腳本,它位於滑塊上,用於確定手柄是否被按住。您必須使用OnPointerDownOnPointerUp處理程序。

如何在現代統一使用指針處理:

第一步,讓這個叫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; 

我希望有效!我想認爲這是知道滑塊何時被按住的最簡單方法。

你真的爲你的第一個項目選擇了一個困難的項目!地獄!

值得注意的是,您也可以將滑塊調整代碼(我的意思是說您的實際代碼爲scrubber)「內部」腳本實際上在滑塊上 - 但我不會擔心目前。您的解決方案作爲看臺是好的。

注意根據實現方式的不同,當按住滑塊手柄但在移動滑塊手柄之前,可能需要更優雅地暫停。爲了實現這一點,當調用「Teste」中顯示的向下/向上例程時,一種方法可以調用UserClickedPauseButton()或類似的概念。相反,您可以不用打擾ScrubSliderChanged,而是在代碼中執行整個作業,該代碼在句柄「關閉」時運行。

(一般情況下,你幾乎可以肯定使用UnityEvent這裏做了一個滑塊堅實的可重複使用的解決方案,如本作使用的東西,如洗滌器。)希望它可以幫助別人

+0

這不起作用,因爲當代碼在'NewAnimController'腳本中實現時,它不會被點擊滑塊。 'Teste'正常工作,正在記錄被點擊的滑動條,在放入'NewAnimController'時不記錄任何東西。 –

+0

我剛剛將代碼恢復到第一個工作的代碼public slider SliderScrubber; public animator animator; public void Update() float animationTime = animator.GetCurrentAnimatorStateInfo(0).normalizedTime; Debug.Log(「animationTime(normalized)is」+ animationTime); sliderScrubber.value = animationTime; } public void OnValueChanged(float changedValue) animator.Play(「Take 001」,-1,sliderScrubber.normalizedValue); } }'這個作品非常適合作爲洗滌器,我不明白我在其他地方出了什麼問題。 –

+0

我用'NewAnimController'腳本做了這件事,它沒有做任何事情。即使我把它放在可以工作的腳本中,它也不會在「teste」腳本之外的控制檯中給我任何東西。我認爲這有點關係,因爲在NewAnimController腳本中有兩個滑塊,所以它不知道你點擊的是哪個滑塊。 –