如何按下按钮并统一运行动画(没有点击,只是按下)

时间:2017-03-14 10:55:35

标签: javascript c# animation unity3d unity5

ON按下按钮运行animcion

See image

1 个答案:

答案 0 :(得分:0)

你的问题不是很明确,但我想你想:

  1. 按下鼠标按钮时启动动画
  2. 释放鼠标按钮时停止动画
  3. 您可以使用Event Trigger来执行此操作。以下是在代码中执行此操作的示例:

    // Drag & Drop the button into the inspector
    public Button button ;
    
    // Drag & Drop your animator into the inspector
    public Animator animator ;
    
    void Start( )
    {
        EventTrigger eventTrigger = button.AddComponent<EventTrigger>( );
    
        // Detect when mouse button is pressed down
        EventTrigger.Entry pointerDownEntry = new EventTrigger.Entry( );
        pointerDownEntry.eventID = EventTriggerType.PointerDown;
        pointerDownEntry.callback.AddListener( ( data ) => { OnPointerDown( (PointerEventData)data ); } );
        eventTrigger.triggers.Add( pointerDownEntry );
    
        // Detect when mouse button is released
        EventTrigger.Entry pointerUpEntry = new EventTrigger.Entry( );
        pointerUpEntry.eventID = EventTriggerType.PointerDown;
        pointerUpEntry.callback.AddListener( ( data ) => { OnPointerUp( (PointerEventData)data ); } );
        eventTrigger.triggers.Add( pointerUpEntry );
    }
    
    public void OnPointerDown( PointerEventData data )
    {
        Debug.Log( "OnPointerDown called." );
    
        // Stop your animation, supposing you have a bool parameter in your animator
        animator.SetBool("boolName", true);
    }
    
    public void OnPointerUp( PointerEventData data )
    {
        Debug.Log( "OnPointerUp called." );
    
        // Stop your animation, supposing you have a bool parameter in your animator
        animator.SetBool("boolName", false);
    }