112 lines
3.2 KiB
C#
112 lines
3.2 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using Cinemachine;
|
|
using UnityEngine.Playables;
|
|
|
|
public class Dissolve : MonoBehaviour
|
|
{
|
|
|
|
public float Duration = 1;
|
|
public float Delay = 0;
|
|
public bool DissolveOnAwake = false;
|
|
|
|
private float StartValue = 0;
|
|
private float EndValue = 1;
|
|
private bool StartSolve = false;
|
|
|
|
private float TimeCross = 0;
|
|
private const string _mainTextureName = "_MainTex";
|
|
private const string _bumpTextureName = "_BumpTex";
|
|
private const string _channelTextureName = "_ChannelTex";
|
|
|
|
private List<Material> _NewMaterials = new List<Material>();
|
|
|
|
private void OnEnable()
|
|
{
|
|
if (DissolveOnAwake)
|
|
StartDissolove();
|
|
}
|
|
|
|
public void StartDissolove()
|
|
{
|
|
if (_NewMaterials.Count <= 0)
|
|
{
|
|
var DissolveMat = CommonUtility.LoadSharedMaterial("Dissolve");
|
|
|
|
Renderer[] renders = GetComponentsInChildren<Renderer>();
|
|
for (int i = 0; i < renders.Length; i++)
|
|
{
|
|
var material = Object.Instantiate(DissolveMat);
|
|
if (material == null)
|
|
continue;
|
|
_NewMaterials.Add(material);
|
|
ReplaceTexture(renders[i].sharedMaterial, material, _mainTextureName);
|
|
ReplaceTexture(renders[i].sharedMaterial, material, _bumpTextureName);
|
|
ReplaceTexture(renders[i].sharedMaterial, material, _channelTextureName);
|
|
renders[i].sharedMaterial = material;
|
|
}
|
|
}
|
|
|
|
for (int i = 0; i < _NewMaterials.Count; i++)
|
|
{
|
|
var material = _NewMaterials[i];
|
|
material.SetFloat("_DissolveEnd", StartValue);
|
|
}
|
|
|
|
TimeCross = Delay < 0 ? 0 : Delay;
|
|
StartSolve = false;
|
|
}
|
|
|
|
public void ReplaceTexture(Material source, Material target, string textureName)
|
|
{
|
|
var result = source.HasProperty(textureName) && target.HasProperty(textureName);
|
|
if (result)
|
|
{
|
|
var texture = source.GetTexture(textureName);
|
|
var offset = source.GetTextureOffset(textureName);
|
|
var scale = source.GetTextureScale(textureName);
|
|
target.SetTexture(textureName, texture);
|
|
target.SetTextureOffset(textureName, offset);
|
|
target.SetTextureScale(textureName, scale);
|
|
}
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
for(int i=0;i< _NewMaterials.Count;i++)
|
|
{
|
|
Object.Destroy(_NewMaterials[i]);
|
|
}
|
|
_NewMaterials.Clear();
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if(TimeCross > 0 && StartSolve == false)
|
|
{
|
|
TimeCross -= Time.deltaTime;
|
|
return;
|
|
}
|
|
if(StartSolve == false)
|
|
{
|
|
StartSolve = true;
|
|
TimeCross = 0;
|
|
}
|
|
if (TimeCross < 0)
|
|
return;
|
|
float value = Mathf.Lerp(StartValue, EndValue, TimeCross / Duration);
|
|
if (TimeCross > Duration)
|
|
TimeCross = -1;
|
|
else
|
|
TimeCross += Time.deltaTime;
|
|
for(int i=0;i< _NewMaterials.Count;i++)
|
|
{
|
|
var material = _NewMaterials[i];
|
|
material.SetFloat("_DissolveEnd", value);
|
|
}
|
|
}
|
|
|
|
}
|