45 lines
1.1 KiB
C#
45 lines
1.1 KiB
C#
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();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 给UI添加碰撞体
|
||
/// </summary>
|
||
/// <param name="uiobj"></param>
|
||
void AddUICollider()
|
||
{
|
||
if (!GetComponent<BoxCollider>())
|
||
{
|
||
gameObject.AddComponent<BoxCollider>();
|
||
Vector2 pivot = GetComponent<RectTransform>().pivot;
|
||
Vector3 center = new Vector3(GetComponent<RectTransform>().sizeDelta.x * (0.5f - pivot.x), GetComponent<RectTransform>().sizeDelta.x * (0.5f - pivot.y));
|
||
GetComponent<BoxCollider>().size = GetComponent<RectTransform>().sizeDelta;
|
||
GetComponent<BoxCollider>().center = center;
|
||
}
|
||
}
|
||
|
||
}
|