237 lines
7.9 KiB
C#
237 lines
7.9 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
using Unity.VisualScripting;
|
|
using UnityEngine;
|
|
|
|
public class ExplodeOnClick : MonoBehaviour
|
|
{
|
|
public Transform parentObject;
|
|
private Vector3[] originalPositions; // 存储原始位置
|
|
private bool isExploded = false; // 标识是否已爆炸
|
|
public List<Vector3> points;
|
|
public List<GameObject> Replacebattery = new List<GameObject>();
|
|
private bool isp = false;
|
|
private GameObject Opjgame;
|
|
public List<Vector3> colidpoints;
|
|
private void Awake()
|
|
{
|
|
for (int i = 0; i < parentObject.childCount; i++)
|
|
{
|
|
points.Add(parentObject.GetChild(i).localPosition);
|
|
Replacebattery.Add(parentObject.GetChild(i).gameObject);
|
|
}
|
|
}
|
|
void Update()
|
|
{
|
|
if (Input.GetMouseButtonDown(0) && isExploded) // 鼠标左键点击
|
|
{
|
|
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
|
|
RaycastHit hit;
|
|
|
|
if (Physics.Raycast(ray, out hit))
|
|
{
|
|
if (Namedata(hit.collider.gameObject.transform.parent.name))
|
|
{
|
|
Debug.Log("有相同名字");
|
|
Debug.Log(hit.collider.gameObject.transform.parent.name);
|
|
Showdata(hit.collider.gameObject.transform.parent.name);
|
|
Opjgame = hit.collider.gameObject.transform.parent.gameObject;
|
|
}
|
|
// 检查点击的物体是否有 ModelChilderComponent
|
|
//ModelChilderComponent childComponent = hit.transform.GetComponent<ModelChilderComponent>();
|
|
//if (childComponent != null)
|
|
//{
|
|
// parentObject = hit.transform.parent; // 保存父对象
|
|
// originalPositions = new Vector3[parentObject.childCount]; // 初始化原始位置数组
|
|
|
|
// // 保存原始位置
|
|
// for (int i = 0; i < parentObject.childCount; i++)
|
|
// {
|
|
// originalPositions[i] = parentObject.GetChild(i).localPosition;
|
|
// }
|
|
//}
|
|
}
|
|
}
|
|
}
|
|
public void Show()
|
|
{
|
|
if (Replacebattery.Count > 0)
|
|
{
|
|
for(int i = 0;i < Replacebattery.Count; i++)
|
|
{
|
|
Replacebattery[i].gameObject.SetActive(true);
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 子物体实现爆炸效果
|
|
/// </summary>
|
|
public void Blowup()
|
|
{
|
|
if (!isp && Opjgame)
|
|
{
|
|
if (colidpoints.Count > 0)
|
|
{
|
|
colidpoints.Clear();
|
|
}
|
|
foreach (Transform child in Opjgame.transform)
|
|
{
|
|
colidpoints.Add(child.localPosition);
|
|
float moveDistance = CalculateMoveDistance(1);
|
|
Vector3 targetPosition = GetTargetPosition(child.transform, moveDistance);
|
|
StartCoroutine(MoveToPosition(child, targetPosition));
|
|
}
|
|
isp = true;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 还原子物体
|
|
/// </summary>
|
|
public void Restore()
|
|
{
|
|
if (isp && Opjgame)
|
|
{
|
|
for (int i = 0; i < Opjgame.transform.childCount; i++)
|
|
{
|
|
Transform child = Opjgame.transform.GetChild(i);
|
|
StartCoroutine(MoveToPosition(child, colidpoints[i]));
|
|
}
|
|
isp = false;
|
|
}
|
|
}
|
|
public void Explode()
|
|
{
|
|
if (!isExploded)
|
|
{
|
|
foreach (Transform child in parentObject)
|
|
{
|
|
float moveDistance = CalculateMoveDistance(0); // 假设为第一层
|
|
Vector3 targetPosition = GetTargetPosition(child, moveDistance);
|
|
|
|
// 这里可以实现动画效果,比如使用协程来移动物体
|
|
StartCoroutine(MoveToPosition(child, targetPosition));
|
|
}
|
|
isExploded = true; // 标记为已爆炸
|
|
}
|
|
}
|
|
|
|
public void ResetPositions() // 复原位置的方法
|
|
{
|
|
if (isExploded)
|
|
{
|
|
for (int i = 0; i < parentObject.childCount; i++)
|
|
{
|
|
Transform child = parentObject.GetChild(i);
|
|
StartCoroutine(MoveToPosition(child, points[i]));
|
|
}
|
|
isExploded = false; // 标记为未爆炸
|
|
}
|
|
}
|
|
|
|
private IEnumerator MoveToPosition(Transform child, Vector3 targetPosition)
|
|
{
|
|
float elapsedTime = 0f;
|
|
Vector3 startingPosition = child.localPosition;
|
|
|
|
while (elapsedTime < 1f) // 1秒内完成移动
|
|
{
|
|
child.localPosition = Vector3.Lerp(startingPosition, targetPosition, elapsedTime);
|
|
elapsedTime += Time.deltaTime;
|
|
yield return null; // 等待下一帧
|
|
}
|
|
|
|
child.localPosition = targetPosition; // 确保最终位置
|
|
}
|
|
|
|
private Vector3 GetTargetPosition(Transform child, float distance)
|
|
{
|
|
string direction = child.GetComponent<ModelChilderComponent>()?.Direction;
|
|
|
|
switch (direction)
|
|
{
|
|
case "前":
|
|
return child.localPosition + new Vector3(0, 0, distance);
|
|
case "后":
|
|
return child.localPosition + new Vector3(0, 0, -distance);
|
|
case "左":
|
|
return child.localPosition + new Vector3(-distance, 0, 0);
|
|
case "右":
|
|
return child.localPosition + new Vector3(distance, 0, 0);
|
|
case "上":
|
|
return child.localPosition + new Vector3(0, distance, 0);
|
|
case "下":
|
|
return child.localPosition + new Vector3(0, -distance, 0);
|
|
case "前上":
|
|
return child.localPosition + new Vector3(0, distance, distance);
|
|
case "前下":
|
|
return child.localPosition + new Vector3(0, -distance, distance);
|
|
case "后上":
|
|
return child.localPosition + new Vector3(0, distance, -distance);
|
|
case "后下":
|
|
return child.localPosition + new Vector3(0, -distance, -distance);
|
|
case "左上":
|
|
return child.localPosition + new Vector3(-distance, distance, 0);
|
|
case "左下":
|
|
return child.localPosition + new Vector3(-distance, -distance, 0);
|
|
case "右上":
|
|
return child.localPosition + new Vector3(distance, distance, 0);
|
|
case "右下":
|
|
return child.localPosition + new Vector3(distance, -distance, 0);
|
|
default:
|
|
return child.localPosition; // 默认不移动
|
|
}
|
|
}
|
|
|
|
private float CalculateMoveDistance(int level)
|
|
{
|
|
if (level == 0)
|
|
return 5; // 自定义第一层的移动距离
|
|
else if (level == 1)
|
|
return 3f; // 第二层的移动距离
|
|
else if (level == 2)
|
|
return 1f; // 第三层的移动距离
|
|
return 0f; // 更深层次不移动
|
|
}
|
|
/// <summary>
|
|
/// 判断链表是否有这个名字
|
|
/// </summary>
|
|
/// <param name="name"></param>
|
|
/// <returns></returns>
|
|
private bool Namedata(string name)
|
|
{
|
|
if (Replacebattery.Count > 0)
|
|
{
|
|
for (int i = 0; i < Replacebattery.Count; i++)
|
|
{
|
|
if (Replacebattery[i].name == name)
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
/// <summary>
|
|
/// 展示对应的子物体
|
|
/// </summary>
|
|
/// <param name="name"></param>
|
|
public void Showdata(string name)
|
|
{
|
|
if (Replacebattery.Count > 0)
|
|
{
|
|
for (int i = 0; i < Replacebattery.Count; i++)
|
|
{
|
|
if (Replacebattery[i].name == name)
|
|
{
|
|
Replacebattery[i].SetActive(true);
|
|
}
|
|
else
|
|
{
|
|
Replacebattery[i].SetActive(false);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|