我正在研究Unity3D和C#,我正在努力理解这段代码的内部工作原理。有人可以向我解释一下吗?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// This script will shrink and grow the game object over time.
/// </summary>
public class Resizer : MonoBehaviour
{
// fields for timer support
const float TotalResizeSeconds = 1;
float elapsedResizeSeconds = 0;
// fields for resizing control
const float ScaleFactorPerSecond = 1;
int scaleFactorSignMultiplier = 1;
void Update()
{
// resize the game object
Vector3 newScale = transform.localScale;
newScale.x += scaleFactorSignMultiplier * ScaleFactorPerSecond * Time.deltaTime;
newScale.y += scaleFactorSignMultiplier * ScaleFactorPerSecond * Time.deltaTime;
transform.localScale = newScale;
print(Time.deltaTime);
// update timer and check if it's done
elapsedResizeSeconds += Time.deltaTime;
if (elapsedResizeSeconds >= TotalResizeSeconds)
{
// reset timer and start resizing the game object
// in the opposite direction
elapsedResizeSeconds = 0;
scaleFactorSignMultiplier *= -1;
}
}
}
具体来说,我试图理解这一部分:
newScale.x += scaleFactorSignMultiplier * ScaleFactorPerSecond * Time.deltaTime;
提前感谢您的帮助!
答案 0 :(得分:1)
scaleFactorSignMultiplier
将在1
和-1
之间切换。到达了。当累计时间超过TotalResizeSeconds
时,scaleFactorSignMultiplier
的符号将通过乘以-1
来改变,这将控制增长和缩小之间的比例方向。
ScaleFactorPerSecond
是一个常量,用于描述对象增长和缩小的速度。每隔一秒,该值将从对象的比例中添加/删除。
Time.Delta
是一个系统变量,它告诉函数自上一帧计算以来经过了多长时间。这是确定在此特定帧中应用多少比例所必需的。