android+unity3d遊戲開發之事件總結

來源:互聯網
上載者:User

有編程經驗的同學們都知道如果沒有事件的話,那遊戲物體就不能被控制了,也就說不上玩遊戲了,呵呵呵,大家都懂的,呵呵,下面我們來說說unity的事件吧,

我主要講解button的事件,鍵盤的事件和滑鼠的事件。

下面先來看看1.button事件:

        先回想一下java的button事件吧  

button.addActionListener(new ActionListener(){     public void actionPerformed(ActionEvent e){                xxxxxxxxxx       }});

    再來回想一下android的button事件吧

button.setOnClickListener(new view.OnClickListener(){    public void onClick(View view){         xxxxxxxxxxxx;      }});

   下面我們就來看看unity的button事件吧

if(GUI.Button(Rect(20,100,100,20),"button")){        xxxxxxxx;}

原理很簡單,就是if裡面的值是true,說明button被按下,則處理事件。

 

 

2.我們再來看看鍵盤事件吧

和Button事件一樣的,如果if()返回true,處理事件

var e : Event = Event.current;
    if (e.Equals(Event.KeyboardEvent("w"))) {        Debug.Log("w");    }//按下w鍵

  當然也可以這樣啦

function Update () {        if(Input.anyKeyDown)        {                Debug.Log(Input.inputString);        }}

 

再給大家一個例子:

    控制物體的前後左右移動的,呵呵呵

var z:float=0.0;var x:float=0.0;function OnGUI() {          transform.Translate(x,0,z);    }              function Update() {    if (Input.GetKeyDown(KeyCode.W)){        print("W key was pressed");        x=-20;        }      if (Input.GetKeyUp(KeyCode.W)){              x=0;        }     if (Input.GetKeyDown(KeyCode.S)){        print("W key was pressed");        x=20;        }      if (Input.GetKeyUp(KeyCode.S)){              x=0;        }       if (Input.GetKeyDown(KeyCode.A)){        print("W key was pressed");        z=-20;        }      if (Input.GetKeyUp(KeyCode.A)){              z=0;        }          if (Input.GetKeyDown(KeyCode.D)){        print("W key was pressed");        z=20;        }      if (Input.GetKeyUp(KeyCode.D)){              z=0;        }                 }

 

  同上,

if (e.isMouse) {        Debug.Log("Mouse clicks: " + e.clickCount);    }

這裡有點不夠完善,大夥先看看吧,瞭解下先,然後結合協助文檔慢慢進步吧。呵呵呵~~~

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.