106 lines
2.6 KiB
C#
106 lines
2.6 KiB
C#
// ***********************************************************************
|
|
// Assembly : Game
|
|
// Author : Kimch
|
|
// Created : 2020-09-29
|
|
// Description :
|
|
// Last Modified By :
|
|
// Last Modified On :
|
|
// ***********************************************************************
|
|
// <copyright file= "GhostShadow" company="Kunpo"></copyright>
|
|
// <summary></summary>
|
|
// ***********************************************************************
|
|
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using G;
|
|
using UnityEngine;
|
|
public class GhostItem : MonoBehaviour
|
|
{
|
|
//持续时间
|
|
public float duration;
|
|
//销毁时间
|
|
public float deleteTime;
|
|
|
|
public MeshRenderer meshRenderer;
|
|
|
|
void Update()
|
|
{
|
|
float tempTime = deleteTime - Time.time;
|
|
if (tempTime <= 0)
|
|
{//到时间就销毁
|
|
GameObject.Destroy(this.gameObject);
|
|
}
|
|
else if (meshRenderer.material)
|
|
{
|
|
float rate = tempTime / duration;//计算生命周期的比例
|
|
Color cal = meshRenderer.material.GetColor("_RimColor");
|
|
cal.a *= rate;//设置透明通道
|
|
meshRenderer.material.SetColor("_RimColor", cal);
|
|
}
|
|
}
|
|
}
|
|
|
|
public class GhostShadow : MonoBehaviour
|
|
{
|
|
//持续时间
|
|
public float duration = 2f;
|
|
//创建新残影间隔
|
|
public float interval = 0.1f;
|
|
|
|
//边缘颜色强度
|
|
[Range(-1, 2)]
|
|
public float intension = 1;
|
|
public Color color;
|
|
|
|
public SkinnedMeshRenderer meshRender;
|
|
//X-ray
|
|
public Shader ghostShader;
|
|
|
|
private float _lastTime = 0;
|
|
private Vector3 _lastPos = Vector3.zero;
|
|
|
|
public EntityMainPlayer cha;
|
|
|
|
void Update()
|
|
{
|
|
//人物有位移才创建残影
|
|
if (/*!cha.isDashing ||*/ _lastPos == this.transform.position)
|
|
{
|
|
return;
|
|
}
|
|
_lastPos = this.transform.position;
|
|
if (Time.time - _lastTime < interval)
|
|
{//残影间隔时间
|
|
return;
|
|
}
|
|
_lastTime = Time.time;
|
|
|
|
if (meshRender == null)
|
|
return;
|
|
Mesh mesh = new Mesh();
|
|
meshRender.BakeMesh(mesh);
|
|
|
|
GameObject go = new GameObject();
|
|
go.hideFlags = HideFlags.HideAndDontSave;
|
|
|
|
GhostItem item = go.AddComponent<GhostItem>();//控制残影消失
|
|
item.duration = duration;
|
|
item.deleteTime = Time.time + duration;
|
|
|
|
MeshFilter filter = go.AddComponent<MeshFilter>();
|
|
filter.mesh = mesh;
|
|
|
|
MeshRenderer meshRen = go.AddComponent<MeshRenderer>();
|
|
|
|
meshRen.material = new Material(ghostShader);
|
|
meshRen.material.SetFloat("_Intension", intension);//颜色强度传入shader中
|
|
meshRen.material.SetColor("_RimColor", color);//颜色强度传入shader中
|
|
|
|
go.transform.localScale = meshRender.transform.localScale;
|
|
go.transform.position = meshRender.transform.position;
|
|
go.transform.rotation = meshRender.transform.rotation;
|
|
|
|
item.meshRenderer = meshRen;
|
|
}
|
|
}
|