- Home /
GUI button-play a 3d model animation
HI Everyone. I import a FBX 3d model and i Split the animation in 4 parts. I added a gui button (canvas) to the scene and i want play an animation when i clicked it. I test this code :
// Update is called once per frame
void Update ()
{
if(Input.GetKeyDown("w")){
animation.Play("caminar");
}
if(Input.GetKeyDown("a")){
animation.Play("arrodillarse");
}
if(Input.GetButtonDown("Jump")){
animation.Play("brazos");
}
}
and work fine using the keyboard. But i want do the same with the gui button.
I write this code:
using UnityEngine;
using System.Collections;
public class MouseButton : MonoBehaviour {
void OnMouseDown()
{
Debug.Log("FUNCIONA_1");
}
void OnMouseUp()
{
Debug.Log("FUNCIONA_2");
}
}
And i add it to the button but doesnt work!!
Can i someone how i could do that? Thanks
Answer by dorpeleg · Feb 18, 2015 at 08:08 AM
The new GUI system works a bit differently. What you need to do is something like:
public void LogTest()
{
Debug.Log("FUNCIONA_1");
}
and in your button component, chose the object that contains the script and chose the function.
Check this UI BUTTON (about 8 minutes in).
O$$anonymous$$, I will try it tonight and i will reply if i have success, while i will watch the video that you suggest me. Thank You!
HI dorpeleg, I did it!!!
I just write a script with:
public void Walk(){
animation.Play("walk");
}
Then i added this script to my 3d model. After that i added the 3d model to the button and then i set the function "Walk" (it must apper in the combo list when you add the 3d model), and ready!! The video help me so much. Now i will try to test the button in a touch device, I guess the process must be similar?
Thank you very much.!
The UI should be working with touch by default. If my answer helped you, please mark as answered :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613183624im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Can the animation editor create local rotational data? 3 Answers
Adding animation clips via script 2 Answers
Can I make animations snap to a frame? 1 Answer
Unity GUI 4.6 stops animation 0 Answers