c# - 为什么在 TextMesh 上切换文本时只切换一个对象?
问题描述
该脚本附加到空的游戏对象:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SwitchText : MonoBehaviour
{
public GameObject[] objectsTexts;
private bool isDisplaying = false;
private Vector3[] lastFwd;
private float curAngleX = 0;
private GameObject[] objectsWithText;
public void Init()
{
objectsWithText = GameObject.FindGameObjectsWithTag("ObjectToAddText");
lastFwd = new Vector3[objectsWithText.Length];
for(int i = 0; i < objectsWithText.Length; i++)
{
lastFwd[i] = objectsWithText[i].transform.forward;
}
}
private void Update()
{
if (isDisplaying == false)
{
StartCoroutine(UpdateDisplayPhrase(objectsWithText));//objectsTexts[i].name, objectsWithText[i].GetComponent<TextMesh>()));
}
}
private bool myApproximation(float a, float b, float tolerance)
{
return (Mathf.Abs(a - b) < tolerance);
}
private IEnumerator UpdateDisplayPhrase(GameObject[] objects)//string text, TextMesh t)
{
// block multiple calls
//if (isDisplaying) yield break;
isDisplaying = true;
for (int i = 0; i < objectsWithText.Length; i++)
{
var curFwd = objectsWithText[i].transform.forward;
// measure the angle rotated since last frame:
var ang = Vector3.Angle(curFwd, lastFwd[i]);
if (myApproximation(ang, 179f, 1f) == true)
{
var t = objectsWithText[i].GetComponent<TextMesh>();
var text = objectsTexts[i].name;
// you can simply yield IEnumerators so they are executed and at the same time
// wait until finished
yield return StartCoroutine(FadeTextTo(0, 0.6f, t));//FadeTextToZeroAlpha(1, t);
t.text = text;
// NOTE: you pass in 0
// -> DIVIDING BY 0 ?!
yield return StartCoroutine(FadeTextTo(1, 0.6f, t));//FadeTextToFullAlpha(0, t);
}
// when done reset the blocking flag
//isDisplaying = false;
}
}
public IEnumerator FadeTextTo(float targetAlpha, float maxDuration, TextMesh textMesh)
{
// more efficient to get both colors beforehand
var fromColor = textMesh.color;
var toColor = new Color(fromColor.r, fromColor.g, fromColor.b, targetAlpha);
// this is optional ofcourse but I like to do this in order to
// always have the same fading speed even if it is already slightly faded into one direction
var actualDuration = maxDuration * Mathf.Abs(fromColor.a - toColor.a);
var passedTime = 0f;
while (passedTime < actualDuration)
{
var lerpFactor = passedTime / actualDuration;
// now the huge advantage is that you can add ease-in and -out if you like e.g.
//lerpFactor = Mathf.SmoothStep(0, 1, lerpFactor);
textMesh.color = Color.Lerp(fromColor, toColor, lerpFactor);
// avoid overshooting
passedTime += Mathf.Min(Time.deltaTime, actualDuration - passedTime);
yield return null;
}
// just to be sure in the end always set it once
textMesh.color = toColor;
}
}
在第一次向游戏对象添加文本后,我在 Start 中的另一个脚本中调用 Init 方法。
在该行的 SwitchText 脚本中使用断点:
StartCoroutine(UpdateDisplayPhrase(objectsWithText));
objectsWithText 包含 3 个对象, objectsTexts 包含 3 个对象。问题是 objectsWithText 在 Update 中尚未旋转,它已经在启动 Coroutine。
主要目标是将TextMesh对象上的文本切换为objectsTexts中对象的名称。
此脚本旋转对象:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rotate : MonoBehaviour
{
public GameObject[] objectsToRotate;
public float duration = 5f;
private void OnMouseDown()
{
StartCoroutine(StartRotationOfObjects());
}
private IEnumerator StartRotationOfObjects()
{
for (int i = 0; i < objectsToRotate.Length; i++)
{
// Random wait period before rotation starts
if (i == 0)
{
yield return new WaitForSeconds(0);
}
else
{
yield return new WaitForSeconds(Random.Range(0, 2f));
}
StartCoroutine(Rotates(objectsToRotate[i].transform, duration));
}
}
private IEnumerator Rotates(Transform objectToRotate, float duration)
{
Quaternion startRot = objectToRotate.rotation;
float t = 0.0f;
while (t < duration)
{
t += Time.deltaTime;
objectToRotate.rotation = startRot * Quaternion.AngleAxis(t / duration * 360f, Vector3.up);
yield return null;
}
objectToRotate.rotation = startRot;
}
}
解决方案
推荐阅读
- python - 两个数据框的 Pandas 合并输出不会产生预期的形状
- excel - 为什么 Excel 2007 pt-PT Trim all cells 会导致错误?
- c# - 如何将表达式理解为 C# 中的方法参数
- reactjs - 如何在我的主要组件之外注入 rootStore?
- c++ - 带有 wchar_t* 的字符串参数的 C++/CLI 编组委托
- r - 大家好,我正在尝试根据过滤标准对数据帧进行子集化,并希望将多个数据帧(子集版本)作为最终结果
- python - 与 ADLS 交互的 Azure Python 模块
- php - 如何将方法替换为 foreach 循环内的变量?
- sql-server - 错误 = [Microsoft][ODBC Driver 13 for SQL Server]无法打开 BCP 主机数据文件
- unit-testing - 如何在不修改数据库的情况下运行集成测试?