1、在规定时间时间内将一个值变化到另一个值,使用Mathf.Lerp实现
private float timer;
[Tooltip("当前温度")]
private float curTemp;
[Tooltip("开始温度")]
private float startTemp = 20;
private float maxTemp = 100;
/// <summary>
/// 升温时设置温度计度数
/// </summary>
/// <param name="duration"></param>
private void HeatingUp(float duration = 10f)
{
if (timer <= duration)
{
timer += Time.deltaTime;
float t = timer / duration;
curTemp = Mathf.Lerp(startTemp, maxTemp, t);
Debug.Log(" 时间=" + timer.ToString("0.000") + " <color=red>升温=</color>" + curTemp.ToString("0.000"));
//thermometerFill.fillAmount = curTemp / 100f;
//thermometerText.text = "温度 " + curTemp.ToString("0") + " ℃";
}
}
/// <summary>
/// 降温时设置温度计度数
/// </summary>
/// <param name="duration"></param>
private void Cooling(float duration = 10f)
{
if (timer >= 0)
{
timer -= Time.deltaTime;
float t = timer / duration;
curTemp = Mathf.Lerp(startTemp, maxTemp, t);
Debug.Log("时间=" + timer.ToString("0.00") + " <color=yellow>降温=</color>" + curTemp.ToString("0.00"));
//thermometerFill.fillAmount = curTemp / 100f;
//thermometerText.text = "温度 " + curTemp.ToString("0") + " ℃";
}
}
2、控制粒子特效数量变化
[Tooltip("水蒸气")] private ParticleSystem waterVapor;
private float waterVaporValue = 0f;//水蒸气初始值
private float targetValue = 1000f;//目标值
/// <summary>
/// 水泡粒子特效变化
/// </summary>
/// <param name="isAdd"></param>
/// <param name="duration"></param>
public void WaterVaporValueChanges(bool isAdd = true, float duration = 10f)
{
if (isAdd)
{
waterVaporValue += Time.deltaTime * (targetValue / duration);
if (waterVaporValue >= targetValue)
{
waterVaporValue = targetValue;
}
}
else
{
waterVaporValue -= Time.deltaTime * (targetValue / duration);
if (waterVaporValue <= 0)
{
waterVaporValue = 0;
}
}
SetWaterVaporParticles(waterVaporValue);
}
/// <summary>
/// 水蒸气特效
/// </summary>
/// <param name="value"></param>
public void SetWaterVaporParticles(float value=0)
{
var mainWaterVapor = waterVapor.main;
mainWaterVapor.maxParticles = (int)value;
}
3、比较两个点击物体的距离
private int CompareDistance(RaycastHit r1, RaycastHit r2)
{
var r1Drag = r1.transform != null ? r1.transform.GetComponentInParent<ObjectDrag>() : null;
var r2Drag = r2.transform != null ? r2.transform.GetComponentInParent<ObjectDrag>() : null;
if (r1Drag != null && r2Drag == null)
{
return -1;
}
else if (r1Drag == null && r2Drag != null)
{
return 1;
}
else if (r2Drag != null && r1Drag != null)
{
if (r2Drag.Weight != r1Drag.Weight)
{
return r2Drag.Weight.CompareTo(r1Drag.Weight);
}
}
return r1.distance.CompareTo(r2.distance);
}
/// <summary>
/// 使用示例
/// </summary>
/// <param name="hit"></param>
/// <returns></returns>
private bool IsRayObject(out RaycastHit hit)
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// 如果射线击中了物体
for (int i = 0; i < Raycasts.Length; i++)
{
Raycasts[i].distance = 999;
};
hit = new RaycastHit();
int count = Physics.RaycastNonAlloc(ray, Raycasts);
if (count > 0)
{
Array.Sort(Raycasts, CompareDistance);
hit = Raycasts[0];
var obj = hit.transform.GetComponentInParent<ObjectDrag>();
if (obj != null)
{
if (obj.IsForbidHighLight)
{
return true;
}
return true;
}
}
return false;
}
4、利用 DoTween 实现相机黑屏渐变效果
/// <summary>
/// 黑屏渐变效果
/// </summary>
/// <param name="isActive"></param>
private void BlackScreenFade(bool isActive = true)
{
CanvasGroup canvasGroup = blackScreen.GetComponent<CanvasGroup>();
// 利用 DoTween 淡入到不透明
if (isActive)
{
// 激活黑屏面板
blackScreen.gameObject.SetActive(true);
canvasGroup.alpha = 0; // 确保起始时为透明
canvasGroup.DOFade(1, fadeDuration);
}
else
{
canvasGroup.DOFade(0, fadeDuration).OnComplete(() =>
{
blackScreen.gameObject.SetActive(false);
});
}
}