BreakOnClick.cs 585 B

12345678910111213141516171819202122
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class BreakOnClick : MonoBehaviour
  5. {
  6. [Header("破碎效果预制体")]
  7. public GameObject fractureEffect; // 拖入粒子预制体
  8. private void OnMouseDown()
  9. {
  10. if (fractureEffect != null)
  11. {
  12. // 生成粒子效果并自动销毁
  13. GameObject effect = Instantiate(fractureEffect, transform.position, transform.rotation);
  14. Destroy(effect, effect.GetComponent<ParticleSystem>().main.duration);
  15. }
  16. Destroy(gameObject); // 销毁被点击的物体
  17. }
  18. }