在开发过程中遇到做一个技能需要带拖影效果,参考别人的资料写了一个拖影效果,记录一下以备使用。一共两个脚本,但只需要挂载EffectMotionGhost即可,代码内容如下所示
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
/// <summary>
/// 运动残影效果
/// </summary>
public class EffectMotionGhost : MonoBehaviour {
//残影生成间隔
public float m_createDuration = 0.2f;
//残影生命周期
public float m_lifeCycle = 0.4f;
//上一次组合时间
private float m_lastCombinedTime = 0.0f;
//残影颜色
public Color m_ghostColor = new Color(0.54f,0.17f,0.88f);
//是否使用原模型特性
public bool m_isUseModelDefault = false;
//需要剔除的渲染网格
public MeshFilter[] m_exceptRenderMeshFilters;
//存贮对象包含的MeshFilter组件
private MeshFilter[] m_meshFilters;
//存贮对象包含的MeshRenderer组件
private MeshRenderer[] m_meshRenders;
//存贮对象包含的m_skinnedMeshRenderers组件
private SkinnedMeshRenderer[] m_skinnedMeshRenderers;
//存储残影
private List<EffectMotionGhostData> m_ghostList = new List<EffectMotionGhostData>();
private List<EffectMotionGhostData> m_deleteGhostList = new List<EffectMotionGhostData>();
private Object m_ghostMaterial;
void Start() {
m_meshFilters = gameObject.GetComponentsInChildren<MeshFilter>();
//获取蒙皮网格
m_skinnedMeshRenderers = gameObject.GetComponentsInChildren<SkinnedMeshRenderer>();
m_ghostMaterial = Resources.Load("EffectMaterial/EffectMitionGhostMaterial");
}
void OnDisable() {
//消灭所有残影
for(int i = 0;i < m_ghostList.Count;i++){
DestroyImmediate(m_ghostList[i].m_gh