ElectricityBusinessHall_Dig.../Assets/Resources/Scripts/Function/UI3DEventTrigger.cs

45 lines
1.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
}
}
}