Unity3D教程:鼠标点选物体后删除物体脚本
发表于2017-02-08
做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); } } } } |