![在这里插入图片描述](https://img-blog.csdnimg.cn/3551124fbade4716aecada86cdc4d699.png)
using System;
using UnityEngine;
namespace DefaultNamespace.UI
{
public class CreatDetection : MonoBehaviour
{
private Material material;
private bool iscreat = false;
private void Start()
{
gameObject.AddComponent<Rigidbody>().useGravity = false;
material = GetComponent<MeshRenderer>().material;
}
private void Update()
{
if (Input.GetMouseButton(0))
{
if (iscreat)
{
material.color = Color.white;
Destroy(GetComponent<Rigidbody>());
Destroy(this);
}
else
{
Destroy(gameObject);
}
MessageManage._ins.BroadCast(MyData.OverCreatAr);
}
if (Input.GetMouseButton(1))
{
Destroy(gameObject);
MessageManage._ins.BroadCast(MyData.OverCreatAr);
}
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player") || other.CompareTag("SceneObj"))
{
material.color = Color.red;
iscreat = false;
}
}
private void OnTriggerExit(Collider other)
{
material.color = Color.green;
iscreat = true;
}
}
}
using System;
using UnityEngine;
using UnityEngine.UI;
namespace DefaultNamespace.UI
{
public class UICreat : MonoBehaviour
{
public static UICreat Ins;
public Button btn;
public Text moneyTxt;
private void Awake()
{
moneyTxt.text = "0";
Ins = this;
}
private void Start()
{
btn.onClick.AddListener(() =>
{
MessageManage._ins.BroadCast(MyData.CreatAr);
});
}
}
}