我目前正在编写一些代码,但在执行我的逻辑和代码时遇到了一些困难。这是我的问题。我目前正在用 C# 编写代码,但遇到了障碍。
我正在尝试编写一个,以便当我的球体碰撞器与任何具有名为 Larynx 的目标标签的网格碰撞器交互或碰撞时,这些 3d 对象的名称将在文本网格专业对象中更新。
作为参考,我附上了定时器脚本,它是一个简单的定时器并且可以工作。
或者,对象有一个名为 XRDirectInteractable 的组件,而不是标签,可用于区分我希望显示其名称的对象。任何建议将不胜感激。
这是我的计时器代码和 SphereColliderDisplay 代码:
---- 定时器代码 ---
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class Timer : MonoBehaviour
{
private float startTime;
private TMP_Text timerText;
[SerializeField] private float fontSize = 20f;
[Range(10, 50)] [SerializeField] private int fontSizeSlider = 20;
private void Start()
{
startTime = Time.time;
timerText = GetComponent<TMP_Text>();
timerText.fontSize = fontSizeSlider;
}
private void Update()
{
float t = Time.time - startTime;
string minutes = ((int)t / 60).ToString();
string seconds = (t % 60).ToString("f2");
timerText.text = minutes + ":" + seconds;
}
private void OnValidate()
{
timerText.fontSize = fontSizeSlider;
}
}
--- SphereCollider显示代码---
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR;
using TMPro;
public class SphereColliderDisplay : MonoBehaviour
{
[SerializeField] private SphereCollider leftControllerCollider;
[SerializeField] private SphereCollider rightControllerCollider;
private TMP_Text textMeshPro;
private List<string> collidedObjectNames = new List<string>();
private void Start()
{
textMeshPro = GetComponent<TMP_Text>();
}
private void OnTriggerEnter(Collider other)
{
if (other.GetComponent<MeshCollider>() != null)
{
if (other.gameObject.GetComponent<SphereCollider>() == leftControllerCollider ||
other.gameObject.GetComponent<SphereCollider>() == rightControllerCollider)
{
collidedObjectNames.Add(other.gameObject.name);
UpdateText();
}
}
}
private void OnTriggerExit(Collider other)
{
if (other.GetComponent<MeshCollider>() != null)
{
if (other.gameObject.GetComponent<SphereCollider>() == leftControllerCollider ||
other.gameObject.GetComponent<SphereCollider>() == rightControllerCollider)
{
collidedObjectNames.Remove(other.gameObject.name);
UpdateText();
}
}
}
private void UpdateText()
{
string displayText = "";
foreach (string name in collidedObjectNames)
{
displayText += name + "\n";
}
textMeshPro.text = displayText;
}
}