當前位置:編程學習大全網 - 編程軟體 - unity如何讓ui按鈕響應事件?

unity如何讓ui按鈕響應事件?

按下鼠標左鍵和彈起觸發圖片按鈕事件的兩種狀態變化。

編寫兩個腳本文件

壹個是點擊按鈕時的圖片變化(UI代碼)。

1使用UnityEngine

2使用系統。收藏;

4公共類MyUIButton : MonoBehaviour

5 {

6 public texture 2d[]BTN;

7公共字符串doUpMethodName

8公共字符串doDownMethodName

9 Rect rc

10 int index = 0;

11

12無效更新()

13 {

14 if(輸入。GetMouseButtonUp(0))

15 {

16 RC = GUI texture . pixelinset;

17 RC . x+= transform . position . x * screen . width;

18 RC . y+= transform . position . y * screen . height;

19 if(rc。包含(Input.mousePosition))

20 {

21指數= 1;

22 SendMessage(doUpMethodName,index);//按鈕彈出時事件觸發

23 }

24 }

25

26 if(輸入。GetMouseButtonDown(0))

27 {

28 RC = GUI texture . pixel inset;

29 RC . x+= transform . position . x * screen . width;

30 RC . y+= transform . position . y * screen . height;

31 if(rc。包含(Input.mousePosition))

32 {

33指數= 0;

  • 上一篇:什麽是excel文件?
  • 下一篇:get set 是什麽意思
  • copyright 2024編程學習大全網