c# - Unity 3d:如何通过单击在 UI 工具包上设计的 UI 中的按钮在我的角色上显示动画?
问题描述
我在 Unity 3D 的 UI 工具包上设计了这个 UI。在此处输入图像描述
当我单击 UI 上的每个按钮时,我希望我的角色执行不同的动画。我已经用所需的剪辑和一个名为“deporteAn”的布尔变量设置了一个动画师。另外,我有我的 UIController 脚本,在其中我用它们的 UI 名称初始化了所有按钮,然后我调用了一个在单击按钮时执行操作的方法。如此代码所示:
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");
}
}
}
我认为问题可能是该脚本附加到 UIDocument。我试图将它附加到角色游戏对象上,但它没有用。我认为可能创建另一个脚本,将其附加到角色游戏对象并调用那里的方法可以工作,所以我这样做了:
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");
}
}
}
但它只显示“未按下按钮”并且在游戏视图中什么也不做。我能做些什么?或者我做错了什么?
解决方案
推荐阅读
- visual-studio - 带有 GLAD 的 VS 2017 社区中的链接器错误
- amazon-web-services - CloudWatch 自定义日志未正确呈现
- python - 无法使用 Selinium Webdriver 点击 Span Link
- html - 如何使包含 div 不影响悬停在父 div 上
- r - 在数据表列表上应用一个函数,该函数需要引用上一行
- python - 如何从张量中获取随机子张量?
- python - 直接保存时,Matplotlib 未正确渲染曲线下的渐变
- git - 如何查看 git merge 在提交之前做了什么?
- r - 使用组第一行的值为每个组创建新行
- node.js - 使用 node.js 构建一个安静的 api 后端 - 问题如何以及从哪里开始