Log in or register to post comments

Trigger more actions per virtual button

July 10, 2012 - 7:45am #3

Hello everyone,

 

I like to trigger more than one action on a virtual button. Therefore I like to use a static variable to keep a integer value to store wich action to trigger.

Where can I set this variable I tried to use the button handler script but that does not work.

 

Thanks in advise.

Trigger more actions per virtual button

July 10, 2012 - 8:45am #2

You should be able to simply define a member variable for the event handler class - unless I've misunderstood your question.

 

e.g.

 

int mActionIndex = 0;

 

can you post your code?

Trigger more actions per virtual button

July 11, 2012 - 12:21am #1

I'm sorry I was not clear on that. I need the button to trigger a action every time the button is touched. So the variable is used to store wich action to trigger.

I have three actions action1, action2 and action3. When I touch the button for the first time action1 needs to be triggert, when I touch the button for the second time action2 needs to be triggert and so on.

 

using UnityEngine;

using System.Collections; 

public class VirtualButtonHandler : MonoBehaviour, IVirtualButtonEventHandler{

        public static int action;

        private GameObject cube;

        void Start () {

                VirtualButtonBehaviour[] vbs = GetComponentsInChildren<VirtualButtonBehaviour>();

                for (int i = 0; i < vbs.Length; ++i)

                {

                    vbs[i].RegisterEventHandler(this);

                }

                cube = GameObject.Find("Cube").gameObject;

        }

        public void OnButtonPressed(VirtualButtonBehaviour vb)

        {

                switch (vb.VirtualButtonName)

                {

                        case "btn1":

                                playAction();

                        break;

                }

        }

        public void playAction(){

                if(action == 0){

                        cube.animation.Play("action1");

                        action = 1;

               }

                if(action == 1){

                       cube.animation.Play("action2");

                       action = 2;

                }

                if(action == 2){

                        cube.animation.Play("action3");

                        action = 0;

                }

        }

        public void OnButtonReleased(VirtualButtonBehaviour vb)

        {}

}

Log in or register to post comments