单击销毁克隆
Destroy Clone On Click
我目前已经尝试了 2 天来通过单击鼠标来破坏实例化的预制克隆。
下面的代码做了 2 件事,在设定的时间间隔后自动销毁它们,效果很好。单击功能会破坏所有克隆,即使我是否单击预制件也是如此,这不是我想要的。
更新:这个项目是二维的
我在这里搜索过,并在不同的脚本平台上询问过其他人,他们的建议似乎没有帮助。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyOnClick : MonoBehaviour
{
public float lifeTime = 10f;
void Update()
{
if (Input.GetMouseButton(0))
{
Destruction();
}
if (lifeTime > 0)
{
lifeTime -= Time.deltaTime;
if(lifeTime <= 0)
{
Destruction();
}
}
}
void Destruction()
{
Destroy(this.gameObject);
}
}
因为更新会运行所有游戏对象,所以不要在更新
中使用Input.GetMouseButton(0)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyOnClick : MonoBehaviour
{
public float lifeTime = 10f;
void Update()
{
if (lifeTime > 0)
{
lifeTime -= Time.deltaTime;
if(lifeTime <= 0)
{
Destruction();
}
}
}
//write here , it only work in this gameobject
void OnMouseDown() { Destruction(); }
void Destruction()
{
Destroy(this.gameObject);
}
}
让您的 Monobehaviour 实现 PointerClick 或 PointerDown 事件处理程序,并确保您的场景中有一个 EventSystem,并且在活动相机上有一个 raycaster。
//something along these lines:
using UnityEngine;
public class DestroyOnClick : Monobehaviour, IPointerClickHandler
{
public void OnPointerClick(PointerClickEventData data)
{
Destroy(this.gameObject);
}
}
一个快速的解决方案是在你试图摧毁的克隆体上放置一个碰撞器,然后在你的克隆体上放置一个新脚本(或者修改你的克隆体上已有的脚本)。
脚本必须包含此方法:
private void OnMouseDown()
{
Destroy(gameObject);
}
或
private void OnMouseUp()
{
Destroy(gameObject);
}
这两种方法都会销毁被点击的游戏对象,但它们会在点击的不同阶段执行。当您按下鼠标按钮时调用 OnMouseDown(),当您按下鼠标按钮时调用 OnMouseUp()
我目前已经尝试了 2 天来通过单击鼠标来破坏实例化的预制克隆。
下面的代码做了 2 件事,在设定的时间间隔后自动销毁它们,效果很好。单击功能会破坏所有克隆,即使我是否单击预制件也是如此,这不是我想要的。
更新:这个项目是二维的
我在这里搜索过,并在不同的脚本平台上询问过其他人,他们的建议似乎没有帮助。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyOnClick : MonoBehaviour
{
public float lifeTime = 10f;
void Update()
{
if (Input.GetMouseButton(0))
{
Destruction();
}
if (lifeTime > 0)
{
lifeTime -= Time.deltaTime;
if(lifeTime <= 0)
{
Destruction();
}
}
}
void Destruction()
{
Destroy(this.gameObject);
}
}
因为更新会运行所有游戏对象,所以不要在更新
中使用Input.GetMouseButton(0)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyOnClick : MonoBehaviour
{
public float lifeTime = 10f;
void Update()
{
if (lifeTime > 0)
{
lifeTime -= Time.deltaTime;
if(lifeTime <= 0)
{
Destruction();
}
}
}
//write here , it only work in this gameobject
void OnMouseDown() { Destruction(); }
void Destruction()
{
Destroy(this.gameObject);
}
}
让您的 Monobehaviour 实现 PointerClick 或 PointerDown 事件处理程序,并确保您的场景中有一个 EventSystem,并且在活动相机上有一个 raycaster。
//something along these lines:
using UnityEngine;
public class DestroyOnClick : Monobehaviour, IPointerClickHandler
{
public void OnPointerClick(PointerClickEventData data)
{
Destroy(this.gameObject);
}
}
一个快速的解决方案是在你试图摧毁的克隆体上放置一个碰撞器,然后在你的克隆体上放置一个新脚本(或者修改你的克隆体上已有的脚本)。
脚本必须包含此方法:
private void OnMouseDown()
{
Destroy(gameObject);
}
或
private void OnMouseUp()
{
Destroy(gameObject);
}
这两种方法都会销毁被点击的游戏对象,但它们会在点击的不同阶段执行。当您按下鼠标按钮时调用 OnMouseDown(),当您按下鼠标按钮时调用 OnMouseUp()