一、先介紹一下Inspector面板里的東西
Button里一共有兩個腳本。

Image(Script)
- 這個跟Image里是一樣的
Button(Script)
Interactable(交互):Button是否可以點(diǎn)擊的開關(guān),關(guān)閉進(jìn)入Disabled狀態(tài)
image.png
Transition(變化):設(shè)置Button按下,彈起,不可用的顯示狀態(tài)
1.None(空):不管怎么樣都只使用Image(Script)里設(shè)置的圖片的
2.Color Tint(著色):在Image(Script)里設(shè)置的圖片基礎(chǔ)上進(jìn)行著色
- Target Graphic(目標(biāo)圖形):可選擇任意Graphic對象進(jìn)行著色
- Normal Color(正常狀態(tài)顏色)
- Highlighted Color(突出狀態(tài)顏色):鼠標(biāo)懸停或者選擇狀態(tài)的顏色
- Pressed Color(按下狀態(tài)顏色)
- Disabled Color(不可用狀態(tài)顏色)
- Color Multiplier(增加顏色):這個不知道有什么用,emmmm 保持默認(rèn)就好了
- Fade Duration(消失時間):其實就是著色時間與褪色時間
3.Sprite Swap(圖片切換):選項跟Color Tint中的相似,區(qū)別在于設(shè)置的是圖片
4.Animation(執(zhí)行動畫)
image.png
Navigation(導(dǎo)航):選中該按鈕后可以用方向鍵比如WASD以及上下左右按鍵選擇其他按鈕,前提是導(dǎo)航目標(biāo)按鈕也開啟了導(dǎo)航功能,可通過回車或者空格點(diǎn)擊按鈕響應(yīng)點(diǎn)擊事件
1.None(關(guān)閉)
2.Horizontal(水平導(dǎo)航):左右能導(dǎo)航到按鈕
3.Automatic(自動導(dǎo)航):上下左右按鍵都能導(dǎo)航到按鈕
4.Vertical(垂直導(dǎo)航):上下能導(dǎo)航到按鈕
5.Explicit(指定導(dǎo)航):指定上下左右按鍵能導(dǎo)航到那個按鈕上
image.png
Visualize(可視化):把按鍵能夠?qū)Ш降降穆窂娇梢暬?,高亮的黃色箭頭為當(dāng)前按鈕可導(dǎo)航到的目標(biāo)(目前只看到該可視化,可能還有其他?)
二、Button點(diǎn)擊事件
點(diǎn)擊事件很簡單,直接在Button里能找到一個On Click()的地方,選擇Editor And Runtime。

然后選擇組件

然后選擇執(zhí)行該控件的腳本函數(shù),像顯示隱藏就直接GameObject>>SetActive(bool)

如果該函數(shù)需要傳參,在下面會需要填寫參數(shù),比如我選擇的是GameObject>>SetActive(bool),則需傳送bool值,空為false,√為true。之后點(diǎn)擊該按鈕就能隱藏BackImage這個控件了??刂破渌靡餐?。

三、Button按下、抬起、長按事件
按下事件對于游戲開發(fā)來說很重要,之前工作開發(fā)項目是MMORPG游戲,像攻擊類的按鍵,都是通過按下來觸發(fā)
與其說是Button長按,不如說是所有能接受事件的所有控件的長按都可以這么實現(xiàn)。
點(diǎn)擊Button,然后看到該Button的Inspector然后點(diǎn)擊AddComponent >> Event >> Event Trigger。或者可以直接上面搜索框搜索。

之后能看到Event Trigger組件,然后點(diǎn)擊里面的Add New Event Type >> PointerDown,再添加PointerUp,現(xiàn)在估計有人知道我為什么把按下,抬起,長按放在一起了,長按是通過PointerDow(按下)、PointerUp(抬起)實現(xiàn)的

創(chuàng)建一個腳本,代碼貼在下面
using UnityEngine;
public class ChangAn : MonoBehaviour
{
public float Ping;
private bool IsStart = false;
private float LastTime = 0;
void Update () {
if (IsStart && Ping > 0 && LastTime > 0 && Time.time - LastTime > Ping)
{
Debug.Log("長按觸發(fā)");
IsStart = false;
LastTime = 0;
}
}
public void LongPress(bool bStart)
{
IsStart = bStart;
if(IsStart)
{
LastTime = Time.time;
Debug.Log("長按開始");
}
else if(LastTime != 0)
{
LastTime = 0;
Debug.Log("長按取消");
}
}
}
然后把寫好的腳本文件拖到你想設(shè)置長按功能的控件里,然后設(shè)置好,Down傳True,Up傳false,Ping傳3,長按3秒觸發(fā)長按

Unity3D的C# API文檔中還有有很多其他得函數(shù),目前我知道的覺得能使用到的函數(shù)就整理出來了下面幾個,進(jìn)入按鈕離開按鈕在PC的某些游戲會用到,鼠標(biāo)懸停在按鈕上的話出現(xiàn)一些小提示,離開這些小提示又會消失,通過這連個函數(shù)能夠?qū)崿F(xiàn)
using UnityEngine;
using UnityEngine.EventSystems;
public class OnImage : EventTrigger
{
public override void OnPointerDown(PointerEventData eventData)
{
base.OnPointerDown(eventData);
Debug.Log("按下" + this.gameObject.name);
}
public override void OnPointerUp(PointerEventData eventData)
{
base.OnPointerUp(eventData);
Debug.Log("抬起" + this.gameObject.name);
}
public override void OnPointerExit(PointerEventData eventData)
{
base.OnPointerExit(eventData);
Debug.Log("離開" + this.gameObject.name);
}
public override void OnPointerEnter(PointerEventData eventData)
{
base.OnPointerEnter(eventData);
Debug.Log("進(jìn)入" + this.gameObject.name);
}
}
這段時間通過學(xué)習(xí),更新一下用來實現(xiàn)Button相關(guān)、點(diǎn)擊、按下、抬起、長按事件,原理還是差不多,只是調(diào)用的方法變了
代碼如下
using UnityEngine;
using UnityEngine.EventSystems;
public class OnImage : MonoBehaviour,IPointerDownHandler,IPointerUpHandler,IPointerExitHandler,IPointerEnterHandler
{
public float Ping;
private bool IsStart = false;
private float LastTime = 0;
void Update()
{
if (IsStart && Time.time - LastTime > Ping)
{
IsStart = false;
Debug.Log("長按");
}
}
public void OnPointerDown(PointerEventData eventData)
{
LongPress(true);
Debug.Log("按下");
}
public void OnPointerUp(PointerEventData eventData)
{
if(IsStart)
{
LongPress(false);
Debug.Log("抬起");
}
}
public void OnPointerExit(PointerEventData eventData)
{
Debug.Log("離開");
}
public void OnPointerEnter(PointerEventData eventData)
{
Debug.Log("進(jìn)入");
}
public void LongPress(bool bStart)
{
IsStart = bStart;
LastTime = Time.time;
}
}
如果對Event Trigger其他的函數(shù)還有興趣的話可以看官方的API
Event Trigger官方API


