using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class UI3DEventTrigger : MonoBehaviour { public Action onClick; // Start is called before the first frame update void Start() { //给UI添加碰撞,防止自适应大小,等0.1s后再添加 Invoke("AddUICollider", 0.1f); } // Update is called once per frame void Update() { } private void OnMouseDown() { onClick.Invoke(); } /// /// 给UI添加碰撞体 /// /// void AddUICollider() { if (!GetComponent()) { gameObject.AddComponent(); Vector2 pivot = GetComponent().pivot; Vector3 center = new Vector3(GetComponent().sizeDelta.x * (0.5f - pivot.x), GetComponent().sizeDelta.x * (0.5f - pivot.y)); GetComponent().size = GetComponent().sizeDelta; GetComponent().center = center; } } }