TheRestDevelop
유니티 버튼 클릭을 하면 캐릭터가 애니메이션하도록 하는 기능
void
2018. 11. 6. 09:00
애니메이션 참고 URL : 내 블로그
버튼만들기 URL : How to create virtual buttons with Vuforia in Unity3D, How to create Virtual buttons with Vuforia AR & Unity3D
error
NullReferenceException: Object reference not set to an instance of an object 에러
OnButtonPressed, OnButtonReleased의 매개변수 VirtualButtonBehaviour가 VirualButtonAbstractBehaviour로 바뀌어야한다
변경전 코드
public void OnButtonPressed(VirtualButtonBehaviour vb) {}public void OnButtonReleased (VirtualButtonBehaviour vb){}변경후 코드public void OnButtonPressed(VirtualButtonAbstractBehaviour vb) {}public void OnButtonReleased (VirtualButtonAbstractBehaviour vb){참고 : Vuforia Forumsusing System.Collections; using System.Collections.Generic; using UnityEngine; using Vuforia; public class babyDanceScript : MonoBehaviour, UserInterfaceButtons { public GameObject edmBtn, hiphopBtn, houseBtn; public Animator baby; public AudioClip audioClip; public AudioSource audioSource; // Reference URL : // Use this for initialization; void Start () { // 버튼 이벤트 리스너 /*edmBtn = GameObject.Find ("edmBtn"); hiphopBtn = GameObject.Find ("hiphopBtn"); houseBtn = GameObject.Find ("houseBtn"); edmBtn.GetComponent<VirtualButtonAbstractBehaviour> ().RegisterEventHandler (this); hiphopBtn.GetComponent<VirtualButtonAbstractBehaviour> ().RegisterEventHandler (this); houseBtn.GetComponent<VirtualButtonAbstractBehaviour> ().RegisterEventHandler (this);*/ Button[] vbs = GetComponentsInChildren<VirtualButtonAbstractBehaviour>(); for (int i = 0; i < vbs.Length; ++i) { // Register with the virtual buttons TrackableBehaviour vbs[i].RegisterEventHandler(this); } // 애기 객체에 애니메이션 baby.GetComponent<Animator> (); // 음악 객체 audioSource.clip = audioClip; } // Update is called once per frame void Update () { } public void OnButtonPressed(VirtualButtonAbstractBehaviourvb){ switch (vb.ButtonName){ case "edmBtn": baby.Play ("hiphopTwo", -1, 0f); Debug.Log ("edmBtn"); break; case "hiphopBtn": baby.Play ("hiphopOne", -1, 0f); Debug.Log ("hiphopBtn"); break; case "houseBtn": baby.Play ("hiphopFour", -1, 0f); Debug.Log ("houseBtn"); break; } Debug.Log ("btn_press"); audioSource.Play (); } public void OnButtonReleased(VirtualButtonAbstractBehaviour vb){ baby.Play ("none", -1, 0f); Debug.Log ("btn_release"); audioSource.Stop (); } }
++)