C# 如何淡入/淡出textmesh?不起作用

C# 如何淡入/淡出textmesh?不起作用,c#,unity3d,C#,Unity3d,顶部: private Vector3 lastFwd; private float curAngleX = 0; 开始时: private void Start() { lastFwd = transform.forward; } 更新中: void Update() { var curFwd = transform.forward; // measure the angle rotated since last frame: var ang = Vecto

顶部:

private Vector3 lastFwd;
private float curAngleX = 0;
开始时:

private void Start()
{
    lastFwd = transform.forward;
}
更新中:

void Update()
{
    var curFwd = transform.forward;
    // measure the angle rotated since last frame:
    var ang = Vector3.Angle(curFwd, lastFwd);

    if (myApproximation(ang, 179f, 1f) == true)
    {
        var t = transform.GetChild(0).GetComponent<TextMesh>();
        StartCoroutine(FadeTextToZeroAlpha(1, t));
        t.text = "Hello";
        StartCoroutine(FadeTextToFullAlpha(0, t));
    }
}
然后从0变回1

但这并没有让它褪色。 如果我只留下从1到0的一行,它将使文本褪色。 但当这两条线存在时,它根本不会消失。文本是相同的

完整的脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class OnMouseOverEvent : MonoBehaviour
{
    public float speed = 5f;
    public float distanceToMove = 1f;
    public bool goForward;
    public Vector3 startPos;
    public Vector3 endPos;

    private bool isRotating = false;
    private Vector3 lastFwd;
    private float curAngleX = 0;

    private void Start()
    {
        lastFwd = transform.forward;

        startPos = transform.position;
        endPos = transform.position - Vector3.forward * distanceToMove;
    }

    void Update()
    {
        if (goForward && isRotating == false)
        {
            transform.position = Vector3.MoveTowards(transform.position, endPos, speed * Time.deltaTime);
        }
        else if (isRotating == false)
        {
            transform.position = Vector3.MoveTowards(transform.position, startPos, speed * Time.deltaTime);
        }

        var curFwd = transform.forward;
        // measure the angle rotated since last frame:
        var ang = Vector3.Angle(curFwd, lastFwd);

        if (myApproximation(ang, 179f, 1f) == true)
        {
            var t = transform.GetChild(0).GetComponent<TextMesh>();
            StartCoroutine(FadeTextToZeroAlpha(1, t));
            t.text = "Hello";
            StartCoroutine(FadeTextToFullAlpha(0, t));
        }
    }

    public IEnumerator FadeTextToFullAlpha(float t, TextMesh i)
    {
        i.color = new Color(i.color.r, i.color.g, i.color.b, 0);
        while (i.color.a < 1.0f)
        {
            i.color = new Color(i.color.r, i.color.g, i.color.b, i.color.a + (Time.deltaTime / t));
            yield return null;
        }
    }

    public IEnumerator FadeTextToZeroAlpha(float t, TextMesh i)
    {
        i.color = new Color(i.color.r, i.color.g, i.color.b, 1);
        while (i.color.a > 0.0f)
        {
            i.color = new Color(i.color.r, i.color.g, i.color.b, i.color.a - (Time.deltaTime / t));
            yield return null;
        }
    }

    private bool myApproximation(float a, float b, float tolerance)
    {
        return (Mathf.Abs(a - b) < tolerance);
    }

    private void OnMouseOver()
    {
        goForward = true;
    }

    private void OnMouseExit()
    {
        goForward = false;
    }

    private void OnMouseDown()
    {
        if (isRotating == false && transform.name == "Options")
            StartCoroutine(Rotate(5));
    }

    IEnumerator Rotate(float duration)
    {
        Quaternion startRot = transform.rotation;
        float t = 0.0f;
        while (t < duration)
        {
            isRotating = true;
            t += Time.deltaTime;

            transform.rotation = startRot * Quaternion.AngleAxis(t / duration * 360f, Vector3.up); 

            yield return null;
        }
        transform.rotation = startRot;

        isRotating = false;
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.UI;
关于MouseOverevent的公共类:MonoBehavior
{
公共浮子速度=5f;
公共浮动距离移动=1f;
公共事业发展;
公共矢量3 startPos;
公共向量3 endPos;
私有bool isRotating=false;
私人矢量3最新版本;
私有浮点数x=0;
私有void Start()
{
lastFwd=transform.forward;
startPos=transform.position;
endPos=transform.position-Vector3.forward*distanceToMove;
}
无效更新()
{
if(goForward&&isRotating==false)
{
transform.position=Vector3.moveToward(transform.position,endPos,speed*Time.deltaTime);
}
否则如果(isRotating==false)
{
transform.position=Vector3.moveToward(transform.position,startPos,speed*Time.deltaTime);
}
var curFwd=transform.forward;
//测量自上一帧起旋转的角度:
var ang=矢量3.角度(curFwd,lastFwd);
如果(My近似值(ang,179f,1f)=真)
{
var t=transform.GetChild(0.GetComponent();
start例程(fadetexttozerolapha(1,t));
t、 text=“你好”;
start例程(fadetexttofullapha(0,t));
}
}
公共IEnumerator FadeTextToFullAlpha(浮点t,TextMesh i)
{
i、 颜色=新颜色(i.color.r,i.color.g,i.color.b,0);
而(i.color.a<1.0f)
{
i、 颜色=新颜色(i.color.r,i.color.g,i.color.b,i.color.a+(Time.deltaTime/t));
收益返回空;
}
}
公共IEnumerator FadeTextToZeroAlpha(浮点t,TextMesh i)
{
i、 颜色=新颜色(i.color.r,i.color.g,i.color.b,1);
而(i.color.a>0.0f)
{
i、 颜色=新颜色(i.color.r,i.color.g,i.color.b,i.color.a-(Time.deltaTime/t));
收益返回空;
}
}
私有布尔myApproximation(浮点a、浮点b、浮点公差)
{
返回(数学绝对值(a-b)<公差);
}
私有void OnMouseOver()
{
goForward=true;
}
私有void OnMouseExit()
{
goForward=false;
}
私有void OnMouseDown()
{
if(isRotating==false&&transform.name==“选项”)
开始例行程序(旋转(5));
}
IEnumerator旋转(浮动持续时间)
{
四元数startRot=transform.rotation;
浮球t=0.0f;
while(t
主要目标是在对象旋转时淡出文本,淡出时更改文本。

所有这些调用

StartCoroutine(FadeTextToZeroAlpha(1, t));
t.text = "Hello";
StartCoroutine(FadeTextToFullAlpha(0, t));
同时发生,因此有并发例程

此外,这可能会在多个后续帧中调用->您也应该只执行一次

你应该把它打包成一个单独的程序,比如

private bool isUpdatingDisplay;

// Get this reference only ONCE
// better would even be if you can already reference it via the Inspector
[SerializeField] privtae TextMesh textMesh;

private void Awake()
{
    if(!textMesh) textMesh = transform.GetChild(0).GetComponent<TextMesh>();
}

void Update()
{

    ....

    // only start fading if it isn't doing it already
    if(!isUpdatingDisplay)
    {
        StartCoroutine(UpdateDisplayPhrase("Hello"), t);
    }

    ....
}

private IEnumerator UpdateDisplayPhrase(string text, TextMesh t)
{
     // this should actually be impossible to happen
     // but just in case ignore this call if a routine is already running
     if(isUpdatingDisplay) yield brake;

     // block multiple calls
     isUpdatingDisplay = true;

     // you can simply yield IEnumerators so they are executed and at the same time
     // wait until finished
     yield return FadeTextToZeroAlpha(1, t);

     t.text = "Hello";

     // NOTE: you pass in 0 
     // -> DIVIDING BY 0 ?!
     yield return FadeTextToFullAlpha(0, t);

     // when done reset the blocking flag
     isUpdatingDisplay = false;
}

public IEnumerator FadeTextToFullAlpha(float t, TextMesh i)
{
    i.color = new Color(i.color.r, i.color.g, i.color.b, 0);
    while (i.color.a < 1.0f)
    {
        // DIVIDING BY 0 ?!
        i.color = new Color(i.color.r, i.color.g, i.color.b, i.color.a + (Time.deltaTime / t));
        yield return null;
    }
}

public IEnumerator FadeTextToZeroAlpha(float t, TextMesh i)
{
    i.color = new Color(i.color.r, i.color.g, i.color.b, 1);
    while (i.color.a > 0.0f)
    {
        i.color = new Color(i.color.r, i.color.g, i.color.b, i.color.a - (Time.deltaTime / t));
        yield return null;
    }
}
但是在
FadeTextToFullAlpha(float t,TextMesh i)
中使用

Time.deltaTime / t
您应该极力避免除以
0
;)


您可以只使用一种方法对其进行重构,使其更加高效,比如(还要习惯使用正确的变量名!)。然后我会使用它,允许额外的放松进出

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);

        i.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
    i.color = toColor;
}
public IEnumerator FadeTextTo(float-targetAlpha、float-maxDuration、TextMesh-TextMesh)
{
//更有效地预先获得两种颜色
var fromColor=textMesh.color;
var toColor=新颜色(fromColor.r、fromColor.g、fromColor.b、targetAlpha);
//这当然是可选的,但我喜欢这样做是为了
//始终保持相同的淡入速度,即使它已稍微淡入一个方向
var actualDuration=maxDuration*Mathf.Abs(从color.a到color.a);
var passedTime=0f;
while(经过的时间<实际持续时间)
{
var lerpFactor=经过的时间/实际持续时间;
//现在最大的优势是,如果你喜欢的话,你可以增加进出的便利。
//lerpFactor=Mathf.SmoothStep(0,1,lerpFactor);
i、 color=color.Lerp(fromColor,toColor,lerpFactor);
//避免过冲
passedTime+=Mathf.Min(Time.deltaTime,actualDuration-passedTime);
收益返回空;
}
//只是为了确保最后总是设定一次
i、 颜色=颜色;
}

现在
0
除法也不再有问题了

行:var actualDuration=maxDuration*Mathf.Abs(fromColor.a,toColor.a);Mathf.Abs只有一个浮点数。它也不能从color.a和toColor.aoh抱歉,有点小错误。。我们希望在这方面有所不同
FadeTextToFullAlpha(0, t);
Time.deltaTime / t
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);

        i.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
    i.color = toColor;
}