111 lines
3.1 KiB
C#
111 lines
3.1 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class EffectHideAndShowCtr : MonoBehaviour
|
|
{
|
|
//要注意一下这里有两套,一套是动画实现的,一套是代码实现的。
|
|
|
|
Animator animator;
|
|
|
|
public MeshRenderer targetMaterial;
|
|
public MeshRenderer targetMaterial2;
|
|
public AudioSource audio_Hide;
|
|
public AudioSource audio_Show;
|
|
private float duration = 0.5f; // 完成0到1变化所需时间
|
|
private float currentValue = 0f;
|
|
private float startTime;
|
|
private bool isShow = false; //是否显示
|
|
private bool isHide = false; //是否隐藏
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
animator = this.GetComponent<Animator>();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (isShow)
|
|
{
|
|
if (targetMaterial == null) return;
|
|
float t = (Time.time - startTime) / duration; // 计算当前进度(0到1之间)
|
|
currentValue = Mathf.Lerp(0f, 1f, t); // 使用Lerp进行平滑插值
|
|
//Debug.Log("什么值:"+currentValue);
|
|
targetMaterial.material.SetFloat("_Dissolve", currentValue);
|
|
if (targetMaterial2 != null)
|
|
{
|
|
targetMaterial2.material.SetFloat("_Dissolve", currentValue);
|
|
}
|
|
if (t >= 1f)
|
|
{
|
|
isShow = false;
|
|
targetMaterial.material.SetFloat("_Dissolve", 1f);
|
|
|
|
if (targetMaterial2 != null)
|
|
{
|
|
targetMaterial2.material.SetFloat("_Dissolve", 1f);
|
|
}
|
|
}
|
|
}
|
|
if (isHide)
|
|
{
|
|
if (targetMaterial == null) return;
|
|
float t = (Time.time - startTime) / duration; // 计算当前进度(0到1之间)
|
|
currentValue = Mathf.Lerp(1f, 0f, t); // 使用Lerp进行平滑插值
|
|
targetMaterial.material.SetFloat("_Dissolve", currentValue);
|
|
if (targetMaterial2 != null)
|
|
{
|
|
targetMaterial2.material.SetFloat("_Dissolve", currentValue);
|
|
}
|
|
if (t >= 1f)
|
|
{
|
|
isHide = false;
|
|
targetMaterial.material.SetFloat("_Dissolve", 0f);
|
|
if (targetMaterial2 != null)
|
|
{
|
|
targetMaterial2.material.SetFloat("_Dissolve", 0f);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Hide()
|
|
{
|
|
if (animator != null)
|
|
{
|
|
//Debug.Log("播放了11111111111111111111");
|
|
animator.Play("EffectClose");
|
|
}
|
|
IsShowObj(false);
|
|
if (audio_Hide != null)
|
|
audio_Hide.Play();
|
|
}
|
|
|
|
public void Show()
|
|
{
|
|
if (animator != null)
|
|
{
|
|
//Debug.Log("播放了22222222222222222222222");
|
|
animator.Play("EffectOpen");
|
|
}
|
|
IsShowObj(true);
|
|
if (audio_Show != null)
|
|
audio_Show.Play();
|
|
}
|
|
|
|
public void IsShowObj(bool obj)
|
|
{
|
|
if (obj)
|
|
{
|
|
startTime = Time.time;
|
|
isShow = true;
|
|
}
|
|
else
|
|
{
|
|
startTime = Time.time;
|
|
isHide = true;
|
|
}
|
|
}
|
|
}
|