75 lines
1.8 KiB
C#
75 lines
1.8 KiB
C#
using UnityEngine;
|
||
|
||
public class TzqController : MonoBehaviour
|
||
{
|
||
[Header("核心设置")]
|
||
// 1. 拖入隐藏的物体 tzq
|
||
public GameObject tzq;
|
||
|
||
[Header("挂载点设置 (手动拖入)")]
|
||
// 2. 在这里把所有可能的点都拖进来
|
||
public Transform[] points;
|
||
|
||
/// <summary>
|
||
/// 根据名称移动 tzq 到指定的点并显示
|
||
/// </summary>
|
||
/// <param name="pointName">点的名称 (必须与Hierarchy中的子物体名称一致)</param>
|
||
public void SetPos(string pointName)
|
||
{
|
||
// 检查 tzq 是否绑定
|
||
if (tzq == null)
|
||
{
|
||
Debug.LogWarning("错误:未绑定 tzq 物体!");
|
||
return;
|
||
}
|
||
|
||
if (points == null || points.Length == 0)
|
||
{
|
||
Debug.LogError("错误:Points 数组为空,请在 Inspector 中拖入点!");
|
||
return;
|
||
}
|
||
|
||
// 1. 遍历数组,查找名字匹配的物体
|
||
Transform targetPoint = null;
|
||
|
||
foreach (var point in points)
|
||
{
|
||
if (point != null && point.name == pointName)
|
||
{
|
||
targetPoint = point;
|
||
break; // 找到了就跳出循环
|
||
}
|
||
}
|
||
|
||
// 2. 如果找到了对应的点
|
||
if (targetPoint != null)
|
||
{
|
||
// 匹配位置
|
||
tzq.transform.position = targetPoint.position;
|
||
|
||
// 匹配旋转
|
||
tzq.transform.rotation = targetPoint.rotation;
|
||
|
||
// 显示物体
|
||
tzq.SetActive(true);
|
||
|
||
// Debug.Log($"成功移动 tzq 到点: {pointName}");
|
||
}
|
||
else
|
||
{
|
||
// 如果遍历完了还没找到
|
||
Debug.LogError($"错误:在 points 数组中未找到名为 '{pointName}' 的点!");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 隐藏物体 tzq
|
||
/// </summary>
|
||
public void Hide()
|
||
{
|
||
if (tzq != null)
|
||
{
|
||
tzq.SetActive(false);
|
||
}
|
||
}
|
||
} |