C# 为什么在TextMesh it';s仅为一个对象切换?

C# 为什么在TextMesh it';s仅为一个对象切换?,c#,unity3d,C#,Unity3d,脚本已附加到空游戏对象: using System.Collections; using System.Collections.Generic; using UnityEngine; public class SwitchText : MonoBehaviour { public GameObject[] objectsTexts; private bool isDisplaying = false; private Vector3[] lastFwd; pr

脚本已附加到空游戏对象:

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;
    }
}
objectsWithText包含3个对象,objectsTexts也包含3个对象。 问题是objectsWithText在更新中还没有旋转,它已经启动了协同程序

主要目标是将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;
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类:单一行为
{
公共游戏对象[]对象旋转;
公共浮动持续时间=5f;
私有void OnMouseDown()
{
start例程(startRotationFobjects());
}
私有IEnumerator StartRotationFobjects()
{
for(int i=0;i
您应该只调用
start例程
一次。。您可能会得到并发例程,因为只要条件为真,您每帧都会启动一个新例程……对,我发现了问题,并组织了其他脚本。它现在正按照我想要的那样工作。
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;
    }
}