这是第一个 Unity 开放项目的repo,是 Unity 和社区合作创建的一个小型开源游戏演示,第一款游戏是一款名为 Chop Chop 的动作冒险游戏。
您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

62 行
1.6 KiB

using System.Collections;
using UnityEngine;
public class DissolveHelper : MonoBehaviour
{
[SerializeField] ParticleSystem _dissolveParticlesPrefab = default;
[SerializeField] float _dissolveDuration = 1f;
private MeshRenderer _renderer;
private ParticleSystem _particules;
private MaterialPropertyBlock _materialPropertyBlock;
[ContextMenu("Trigger Dissolve")]
public void TriggerDissolve()
{
if (_materialPropertyBlock == null)
{
_materialPropertyBlock = new MaterialPropertyBlock();
}
InitParticleSystem();
StartCoroutine(DissolveCoroutine());
}
[ContextMenu("Reset Dissolve")]
private void ResetDissolve()
{
_materialPropertyBlock.SetFloat("_Dissolve", 0);
_renderer.SetPropertyBlock(_materialPropertyBlock);
}
private void InitParticleSystem()
{
_particules = GameObject.Instantiate(_dissolveParticlesPrefab, transform);
_renderer = GetComponent<MeshRenderer>();
ParticleSystem.ShapeModule shapeModule = _particules.shape;
shapeModule.meshRenderer = _renderer;
shapeModule.enabled = true;
ParticleSystem.MainModule mainModule = _particules.main;
mainModule.duration = _dissolveDuration;
}
public IEnumerator DissolveCoroutine()
{
float normalizedDeltaTime = 0;
_particules.Play();
while (normalizedDeltaTime < _dissolveDuration)
{
normalizedDeltaTime += Time.deltaTime;
float remappedValue = VFXUtil.RemapValue(normalizedDeltaTime, 0, _dissolveDuration, 0, 1);
_materialPropertyBlock.SetFloat("_Dissolve", remappedValue);
_renderer.SetPropertyBlock(_materialPropertyBlock);
yield return null;
}
GameObject.Destroy(_particules.gameObject);
}
}