文章是学习了momo的unity 3d 游戏开发中的方法的记录,他的方法主要是继承了EventSystem.EventTrigger 重写了OnpointClick()方法。
Step1 创建Image
Step2 编写点击方法脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class Script_05_07 : MonoBehaviour
{
public Button btn1;
public Button btn2;
public Text text;
public Image image;
private void Awake()
{
btn1.onClick.AddListener(delegate()
{
OnClick(btn1.gameObject);
});
btn2.onClick.AddListener(delegate ()
{
OnClick(btn2.gameObject);
});
UGUIEventListener.Get(text.gameObject).onClick = OnClick;
UGUIEventListener.Get(image.gameObject).onClick = OnClick;
}
void OnClick(GameObject obj)
{
if(obj == btn1.gameObject)
{
Debug.Log("按钮1");
}
else if(obj == btn2.gameObject)
{
Debug.Log("按钮2");
}
else if(obj == image.gameObject)
{
Debug.Log("图片");
}
else if (obj == text.gameObject)
{
Debug.Log("文本");
}
}
}
重写EventSystem.EventTrigger的OnpointClick()
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
public class UGUIEventListener : UnityEngine.EventSystems.EventTrigger
{
public UnityAction<GameObject> onClick;
public override void OnPointerClick(UnityEngine.EventSystems.PointerEventData eventData)
{
base.OnPointerClick(eventData);
if(onClick != null)
{
onClick(gameObject);
}
}
static public UGUIEventListener Get(GameObject go)
{
UGUIEventListener listener = go.GetComponent<UGUIEventListener>();
if(listener == null)
listener = go.AddComponent<UGUIEventListener>();
return listener;
}
}
step4 添加
把各种东西挂上去调用,,我直接放在了画布上  okk~ 
|