1. 程式人生 > >UGUI常見的回撥方法

UGUI常見的回撥方法

//指令碼生命週期 //當指令碼例項被載入時被呼叫 private void Awake() {

}
//僅在update函式第一次被呼叫前呼叫
private void Start()
{
    
}
//當指令碼處於可用被呼叫
private void OnEnable()
{
    
}
//當MonoBehaviour啟用時,每一幀呼叫一次
private void Update()
{
    
}
//固定時間間隔呼叫
private void FixedUpdate()
{
    
}
//每一幀呼叫一次,當Update執行完後被呼叫
private void LateUpdate()
{
    
}
//渲染和處理GUI事件時呼叫
private void OnGUI()
{
    
}
//當指令碼元件不可用被呼叫
private void OnDisable()
{
    
}
//當指令碼元件被銷燬時被呼叫
private void OnDestroy()
{
    
}

//Input類——滑鼠事件
private void OnMouseDown()
{
    Debug.Log("滑鼠在物體身上按下");
}
private void OnMouseEnter()
{
    Debug.Log("滑鼠進入物體。。。。");
}
private void OnMouseDrag()
{
    Debug.Log("滑鼠在物體身上拖拽");
}
private void OnMouseOver()
{
    Debug.Log("滑鼠在物體身上懸停");
}
private void OnMouseUp()
{
    Debug.Log("滑鼠在物體身上擡起");
}
private void OnMouseUpAsButton()
{
    Debug.Log("滑鼠在物體身上按下擡起一個完整操作");
}
private void OnMouseExit()
{
    Debug.Log("滑鼠從物體身上離開");
}


//3D碰撞回撥方法
private void OnCollisionEnter(Collision collision)
{
    Debug.Log("碰撞開始,碰的是"+collision.gameObject.name);

}
private void OnCollisionStay(Collision collision)
{
    Debug.Log("持續碰撞");
}
private void OnCollisionExit(Collision collision)
{
    Debug.Log("碰撞結束");
}
//2D碰撞回撥方法
private void OnCollisionEnter2D(Collision2D collision)
{
    Debug.Log("碰撞開始,碰的是" + collision.gameObject.name);
}
private void OnCollisionStay2D(Collision2D collision)
{
    Debug.Log("持續碰撞" );
}
private void OnCollisionExit2D(Collision2D collision)
{
    Debug.Log("碰撞結束");
}

//3D,2D觸發器的回撥方法
private void OnTriggerEnter(Collider other)
{
    
}
private void OnTriggerStay(Collider other)
{
    
}
private void OnTriggerExit(Collider other)
{
 
}
private void OnTriggerEnter2D(Collider2D collision)
{
    
}
private void OnTriggerStay2D(Collider2D collision)
{
    
}
private void OnTriggerExit2D(Collider2D collision)
{

}

using UnityEngine.EventSystems;

//UGUI的回撥方法 public class HuidiaoScript : MonoBehaviour, IEndDragHandler, IDragHandler, IBeginDragHandler, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler, IPointerUpHandler, IPointerClickHandler { public void OnBeginDrag(PointerEventData eventData) { Debug.Log(“結束拖拽”); }

public void OnDrag(PointerEventData eventData)
{
    Debug.Log("拖拽中");
}

public void OnEndDrag(PointerEventData eventData)
{
    Debug.Log("開始拖拽");
}

public void OnPointerClick(PointerEventData eventData)
{
    Debug.Log("滑鼠進入");
}

public void OnPointerDown(PointerEventData eventData)
{
    Debug.Log("滑鼠離開");
}

public void OnPointerEnter(PointerEventData eventData)
{
    Debug.Log("滑鼠點下");
}

public void OnPointerExit(PointerEventData eventData)
{
    Debug.Log("滑鼠擡起");
}

public void OnPointerUp(PointerEventData eventData)
{
    Debug.Log("滑鼠點選");
}

}