简体   繁体   English

Unity 3d:如何通过单击 UI 工具包上设计的 UI 中的按钮在我的角色上显示动画?

[英]Unity 3d: how can I display an animation on my character by clicking on a button from my UI designed on UI toolkit?

I have this UI designed on UI Toolkit in Unity 3D.我在 Unity 3D 中的 UI Toolkit 上设计了这个 UI。 enter image description here在此处输入图片说明

I want my character to do a different animation when I click each of the buttons on the UI.当我单击 UI 上的每个按钮时,我希望我的角色执行不同的动画。 I have already set an animator with the needed clips and a boolean var called "deporteAn".我已经设置了一个带有所需剪辑的动画师和一个名为“deporteAn”的布尔变量。 Also, I have my UIController script where I initialized all of the buttons with their UI names and then I call a method that does the action when the buttons are clicked.此外,我有我的 UIController 脚本,我用它们的 UI 名称初始化了所有按钮,然后我调用了一个方法,该方法在单击按钮时执行操作。 As it is shown on this code:正如此代码所示:

public class UIController : MonoBehaviour
{
    public Button deporteButton;
    public Button lecturaButton;
    public Button trabajoButton;
    public Button vacunacionButton;
    public Button gatoButton;
    public Button perroButton;
    public Button ratonButton;

    private void OnEnable()
    {
        deporteButton = rootVisualElement.Q<Button>("deporte-button");
        lecturaButton = rootVisualElement.Q<Button>("lectura-button");
        trabajoButton = rootVisualElement.Q<Button>("trabajo-button");
        vacunacionButton = rootVisualElement.Q<Button>("vacunacion-button");

        gatoButton = rootVisualElement.Q<Button>("gato-button");
        perroButton = rootVisualElement.Q<Button>("perro-button");
        ratonButton = rootVisualElement.Q<Button>("raton-button");


        deporteButton.clicked += DeporteButton_clicked;//calling the method when deporteButton is clicked.

        public void DeporteButton_clicked()//Method where the animation should be displayed.
        {
            //Shows the animation on my character.
            animator.SetBool("deporteAn", true);
            UnityEngine.Debug.Log("Deporte clicked");
        }
    }
}

I think the problem might be that this script is attached to the UIDocument.我认为问题可能是此脚本附加到 UIDocument。 I've tried to attached it to the character gameobject but it didn't work.我试图将它附加到角色游戏对象上,但没有奏效。 I have thought that maybe creating another script, attach it to the character gameobject and call the method there could work so I did this:我认为可能创建另一个脚本,将其附加到角色游戏对象并调用那里的方法可以工作,所以我这样做了:

public class PlayerController : MonoBehaviour

{
   
    public UIController uiButton;

    public void Start()
    {
        AnimationButtons();
    }
    public void AnimationButtons()
    {
        uiButton = GetComponent<UIController>();

        if (uiButton)
        {
            uiButton.deporteButton.clicked += uiButton.DeporteButton_clicked;
            UnityEngine.Debug.Log("Button pressed");
        }
        else
        {
            UnityEngine.Debug.Log("Button not pressed");
        }
    }

}

But it only shows "Button not pressed" and do nothing on the game view.但它只显示“按钮未按下”并且在游戏视图中什么也不做。 What can I do?我能做什么? Or what am I doing wrong?或者我做错了什么?

From the top of my head uiButton variable is null so always "Button not pressed" will be written从我的头顶 uiButton 变量是空的所以总是“按钮未按下”将被写入

look @ this code in my pic I think same error occurs at your code在我的图片中查看@此代码我认为您的代码中发生了同样的错误

在此处输入图片说明

So check if UIController , PlayerController attached to the same game object所以检查UIControllerPlayerController 是否附加到同一个游戏对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM