中餐厅摆台-点击下一步一次显示骨碟碗勺并显示文字 距离
需要的预制体
需要的脚本
还有一个下一步的button
最后的成品是
最后是代码
using UnityEngine;
public class Billboard : MonoBehaviour
{
void LateUpdate()
{
transform.LookAt(transform.position + Camera.main.transform.forward);
}
}
using UnityEngine;
[System.Serializable]
public class PlaceStep
{
// 当前步骤要生成的餐具预制体
public GameObject itemPrefab;
// 对应场景里的点位Transform
public Transform pointTrans;
// 餐具显示名称(用于悬浮文字标注)
public string showName;
// 和上一个物体的标准间距(cm),用来显示距离文字
public float spaceCm;
}
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class TableStepManager : MonoBehaviour
{
[Header("摆台步骤列表,按顺序填:骨碟→汤碗→味碟...")]
public PlaceStep[] stepList;
[Header("下一步按钮")]
public Button btnNext;
[Header("名称&距离文字UI预制体(世界空间画布)")]
public GameObject tipUIPrefab;
// 当前进行到第几步
private int currentStepIndex = 0;
void Start()
{
// 绑定按钮点击事件
btnNext.onClick.AddListener(NextStep);
}
void NextStep()
{
// 所有步骤执行完毕直接返回
if (currentStepIndex >= stepList.Length)
{
Debug.Log("全部餐具摆放完成");
return;
}
// 取出当前步骤配置
PlaceStep curStep = stepList[currentStepIndex];
Transform point = curStep.pointTrans;
// 核心生成代码:按点位坐标、旋转生成,父物体为table6
GameObject itemObj = Instantiate(
curStep.itemPrefab,
point.position,
point.rotation,
transform
);
// 生成餐具名称悬浮文字
GameObject nameTip = Instantiate(tipUIPrefab, itemObj.transform.position + Vector3.up * 0.15f, Quaternion.identity, transform);
nameTip.GetComponentInChildren<TextMeshProUGUI>().text = curStep.showName;
nameTip.AddComponent<Billboard>();
// 如果不是第一步,计算和上一个物体的中点,显示间距文字
if (currentStepIndex > 0)
{
Transform lastItem = transform.GetChild(transform.childCount - 2);
Vector3 midPos = (itemObj.transform.position + lastItem.position) / 2;
GameObject spaceTip = Instantiate(tipUIPrefab, midPos, Quaternion.identity, transform);
spaceTip.GetComponentInChildren<TextMeshProUGUI>().text = curStep.spaceCm + "cm";
spaceTip.AddComponent<Billboard>();
}
// 步骤+1,等待下一次点击下一步
currentStepIndex++;
}
}
