归海一啸
管理员
管理员
  • 社区居民
  • 最爱沙发
  • 忠实会员
  • 喜欢达人
  • 原创写手
阅读:3288回复:0

(Unity3d)Vuforia开发高阶二-虚拟按钮(第二部分)

楼主#
更多 发布于:2015-06-25 22:40
public class MyVirtualButtonControl : MonoBehaviour,
IVirtualButtonEventHandler
{
    #region PUBLIC_MEMBER_VARIABLES
  
    public ImageTargetBehaviour imageTargetBehaviour;
  
    public AudioSource button_audio;
  
    /// <summary>
    /// The materials that will be set for the teapot model
    /// </summary>
    //public Material[] m_TeapotMaterials;
      
    #endregion // PUBLIC_MEMBER_VARIABLES
      
    #region PRIVATE_MEMBER_VARIABLES
      
    private GameObject mTeapot;
    private List<Material> mActiveMaterials;
      
    #endregion // PRIVATE_MEMBER_VARIABLES
      
    private Vector2 top;
    private Vector2 bottom;
  
    public GameObject myObject;
  
    private string labelname = "Hello,ANTVR!";
    #region UNITY_MONOBEHAVIOUR_METHODS
  
    void Start()
    {
        // Register with the virtual buttons TrackableBehaviour
        VirtualButtonBehaviour[] vbs = GetComponentsInChildren<VirtualButtonBehaviour>();
        for (int i = 0; i < vbs.Length; ++i)
        {
            vbs<i>.RegisterEventHandler(this);
            vbs<i>.CalculateButtonArea(out top,out bottom);
            Debug.Log("----->"+vbs.Length);
            //vbs<i>.VirtualButton.Name+
            Debug.Log("---->"+top.x+":"+top.y+"----->"+bottom.x+":"+bottom.y);
        }
  
        VirtualButtonBehaviour vb=
            (VirtualButtonBehaviour)imageTargetBehaviour.CreateVirtualButton ("eleven",new Vector2(-10,10),new Vector2(10,-10));
        if (vb != null) {
            vb.VirtualButton.SetEnabled(true);
            Debug.Log("!!!!!!!!!"+vb.VirtualButton.Name);
        }
        // Get handle to the teapot object
        //mTeapot = transform.FindChild("teapot").gameObject;
          
        // The list of active materials
        mActiveMaterials = new List<Material>();
    }
      
    #endregion // UNITY_MONOBEHAVIOUR_METHODS
      
      
      
    #region PUBLIC_METHODS
      
    /// <summary>
    /// Called when the virtual button has just been pressed:
    /// </summary>
    public void OnButtonPressed(VirtualButtonAbstractBehaviour vb)
    {
        if (!button_audio.isPlaying) {
            button_audio.Play();        
        }
        if (button_audio.isPlaying) {
            button_audio.volume=1;      
        }
        Debug.Log("OnButtonPressed::" + vb.VirtualButtonName);
          
        // Add the material corresponding to this virtual button
        // to the active material list:
        switch (vb.VirtualButtonName)
        {
        case "one":
            //myObject.transform.animation.Play();
            myObject.transform.animation.Play("Take 4");
            labelname = "one -> Run!";
            if (!button_audio.isPlaying) {
                button_audio.Play();        
            }
            if (button_audio.isPlaying) {
                button_audio.volume=1;      
            }
            break;
              
        case "two":
            myObject.transform.animation.Play("Take 002");
            labelname = "two -> Nothing!";
            if (!button_audio.isPlaying) {
                button_audio.Play();        
            }
            break;
              
        case "three":
            //myObject.transform.animation.Play("Take 4");
            myObject.transform.animation.Play();
            labelname = "three -> Stand!";
            if (!button_audio.isPlaying) {
                button_audio.Play();        
            }
            break;
              
        case "four":
            myObject.transform.animation.Play("Take 5");
            labelname = "four -> Hello!";
            if (!button_audio.isPlaying) {
                button_audio.Play();        
            }
            break;
  
        case "five":
            //myObject.transform.animation.Play("Take 5");
            myObject.transform.animation.Play();
            labelname = "five -> Stand!";
            if (!button_audio.isPlaying) {
                button_audio.Play();        
            }
            break;
  
        case "six":
            myObject.transform.animation.Play("Take 5");
            labelname = "six -> Hello!";
            if (!button_audio.isPlaying) {
                button_audio.Play();        
            }
            break;
        case "seven":
            //myObject.transform.animation.Play("Take 5");
            myObject.transform.animation.Play("Take 4");
            labelname = "seven -> Stand!";
            if (!button_audio.isPlaying) {
                button_audio.Play();        
            }
            break;
              
        case "eight":
            myObject.transform.animation.Play("Take 002");
            labelname = "eight -> Hello!";
            if (!button_audio.isPlaying) {
                button_audio.Play();        
            }
            break;
  
        case "nine":
            //myObject.transform.animation.Play("Take 5");
            myObject.transform.animation.Play("Take 4");
            labelname = "nine -> Stand!";
            if (!button_audio.isPlaying) {
                button_audio.Play();        
            }
            break;
              
        case "ten":
            myObject.transform.animation.Play("Take 002");
            labelname = "ten -> Hello!";
            if (!button_audio.isPlaying) {
                button_audio.Play();        
            }
            break;
  
        case "eleven":
            myObject.transform.animation.Play("Take 002");
            labelname = "eleven -> Hello!";
            if (!button_audio.isPlaying) {
                button_audio.Play();        
            }
            break;
        }
  
    }
      
    /// <summary>
    /// Called when the virtual button has just been released:
    /// </summary>
    public void OnButtonReleased(VirtualButtonAbstractBehaviour vb)
    {
          
        // Remove the material corresponding to this virtual button
        // from the active material list:
        switch (vb.VirtualButtonName)
        {
        case "one":
  
            break;
              
        case "two":
  
            break;
              
        case "three":
  
            break;
              
        case "four":
  
            break;
        }
    }
      
    #endregion // PUBLIC_METHODS
      
    void OnGUI(){
        GUI.Label (new Rect (10, 10, 100, 100), labelname);
    }
}</i></i></i>

最新喜欢:

zl856012zl8560...
AR学院(www.arvrschool.com),从这里感触未来!

欢迎分享

游客

返回顶部