C# 为什么缩放变化非常缓慢? 使用系统集合; 使用System.Collections.Generic; 使用UnityEngine; 公共班级规模:单一行为 { 公共浮动持续时间=1f; 公共矢量3分钟; 公共向量3最大大小; 公共bool scaleUp=false; 公共协同程序规模例程; 公共bool scalinghassfinished=false; 公共停车场停车场停车场停车场停车场; 私有void Start() { transform.localScale=minSize; } 私有void更新() { if(depthOfField.depthOfFieldProcessEnded==true) { start例程(ScaleOverTime()); depthOfField.depthOfFieldProcessEnded=false; } } 公共IEnumerator ScaleOverTime() { 浮点计数器=0; Vector3 startScaleSize=transform.localScale; while(计数器
我希望它能在1秒内从minSize扩展到maxSize。相反,它每1秒改变一次比例0.01,直到达到最大值C# 为什么缩放变化非常缓慢? 使用系统集合; 使用System.Collections.Generic; 使用UnityEngine; 公共班级规模:单一行为 { 公共浮动持续时间=1f; 公共矢量3分钟; 公共向量3最大大小; 公共bool scaleUp=false; 公共协同程序规模例程; 公共bool scalinghassfinished=false; 公共停车场停车场停车场停车场停车场; 私有void Start() { transform.localScale=minSize; } 私有void更新() { if(depthOfField.depthOfFieldProcessEnded==true) { start例程(ScaleOverTime()); depthOfField.depthOfFieldProcessEnded=false; } } 公共IEnumerator ScaleOverTime() { 浮点计数器=0; Vector3 startScaleSize=transform.localScale; while(计数器,c#,unity3d,C#,Unity3d,我希望它能在1秒内从minSize扩展到maxSize。相反,它每1秒改变一次比例0.01,直到达到最大值 它使整个缩放平滑了1秒。产生返回新的WaitForSeconds(持续时间)应返回空值您需要更具体一些,“我希望它在1秒内从minSize扩展到maxSize”是非常松散的ambiguous@XenoRo这是不对的yield null不是一条语句,它必须是yield return null@德拉科是的,我在用手机,搞混了。我的意思是收益率返回null,收益率返回任何_数据,并且不能是收益率
它使整个缩放平滑了1秒。
产生返回新的WaitForSeconds(持续时间)
应返回空值代码>您需要更具体一些,“我希望它在1秒内从minSize扩展到maxSize”是非常松散的ambiguous@XenoRo这是不对的yield null
不是一条语句,它必须是yield return null
@德拉科是的,我在用手机,搞混了。我的意思是收益率返回null,收益率返回任何_数据,并且不能是收益率中断。对mcrvaz评论的解释是:yield-return
在一个协同程序中,将在继续代码之前等待;在这种情况下,请等待另一次运行while部分代码<代码>收益返回空值
将仅等待下一帧WaitForSeconds
将等待指定时间所需的帧数。在这种情况下,您只需要跳到下一帧。为此,使用yield return null
(任何不是yield break
的收益都可以,但在这种情况下,产生数据没有意义,因此null
是合适的)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Scale : MonoBehaviour
{
public float duration = 1f;
public Vector3 minSize;
public Vector3 maxSize;
public bool scaleUp = false;
public Coroutine scaleCoroutine;
public bool scalingHasFinished = false;
public DepthOfField depthOfField;
private void Start()
{
transform.localScale = minSize;
}
private void Update()
{
if(depthOfField.depthOfFieldProcessEnded == true)
{
StartCoroutine(ScaleOverTime());
depthOfField.depthOfFieldProcessEnded = false;
}
}
public IEnumerator ScaleOverTime()
{
float counter = 0;
Vector3 startScaleSize = transform.localScale;
while (counter < duration)
{
counter += Time.deltaTime;
transform.localScale = Vector3.Lerp(startScaleSize, maxSize, counter / duration);
yield return new WaitForSeconds(duration);
}
scalingHasFinished = true;
}
}