Unity3D为3D物体添加点击事件

本文介绍了如何在Unity3D中为3D物体添加点击事件。首先,通过为相机添加《ClickEvent》脚本来监听点击,然后,为每个3D物体附上《Threed_Object》脚本以响应这些点击事件,实现3D场景中的交互功能。
摘要由CSDN通过智能技术生成

1.相机添加脚本《ClickEvent》:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class ClickEvent : MonoBehaviour {

    public GameObject[] cube1; //3D物体集合

    private void Start()
    {
        //为相机添加3个组件
        if (this.gameObject.GetComponent<PhysicsRaycaster>() == null) {
            gameObject.AddComponent<PhysicsRaycaster>();
        }
        if (this.gameObject.GetComponent<EventSystem>() == null)
        {
            this.gameObject.AddComponent<EventSystem>();
        }

        if (this.gameObject.GetComponent<StandaloneInputModule>() == null)
        {
            gameObject.AddComponent<StandaloneInputModule>();
        }
        
        //为3D物体添加点击事件
        for (int i = 0; i < cube1.Length; i++)
        {
            AddObjectClickEvent(cube1[i]);
        }
    }

    public void 
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值