Unity3D教程:鼠标点选物体后删除物体脚本

发表于2017-02-08
评论0 2.2k浏览

做Unity3D开发有些物体不想要想要删除掉,可能有些人还不知道这个功能是如何实现的,为此下面就给大家介绍下Unity3D中鼠标点选物体后删除物体的脚本,一起来看看吧。

  代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using UnityEngine;
using System.Collections;
 
public class MouseCheck : MonoBehaviour
{
    RaycastHit hit;
    bool isDelect = false;
 
    void Start()
    {
 
    }
 
    void Update()
    {
        if (Input.GetButtonDown("Fire1"))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit))
            {
                isDelect = true;
            }
        }
    }
 
    void OnGUI()
    {
        if (isDelect)
        {
            if (Input.GetKeyDown(KeyCode.Delete))
            {
                Destroy(hit.collider.gameObject);
            }
        }
    }
}

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引

0个评论