• Unity实现残影和淡入淡出效果


    //残影实现

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;//引入使用List的命名空间

    public class CanYing : MonoBehaviour {

    public float interval = 0.1f;//残影的时间间隔
    public float lifeCycle = 2.0f;//残影的生存周期

    float lastCombinedTime = 0.0f;//上一次组合时间

    MeshFilter[] meshFilters = null;

    MeshRenderer[] meshRenderers = null;

    SkinnedMeshRenderer[] skinedMeshRenderers = null;

    List<GameObject> objs = new List<GameObject>();

    // Use this for initialization
    void Start ()
    {
    meshFilters = gameObject.GetComponentsInChildren<MeshFilter>();
    skinedMeshRenderers = gameObject.GetComponentsInChildren<SkinnedMeshRenderer>();
    }

    void OnDisable()
    {
    foreach (GameObject go in objs)
    {
    DestroyImmediate(go);//销毁go
    }
    objs.Clear();
    objs = null;
    }
    // Update is called once per frame
    void Update ()
    {
    if (Time.time - lastCombinedTime > interval) //控制间隔时间
    {
    lastCombinedTime = Time.time;

    for (int i = 0; skinedMeshRenderers != null && i < skinedMeshRenderers.Length; ++i) //遍历skinedMeshRenderers
    {
    Mesh mesh = new Mesh(); //生成一个新的mesh

    skinedMeshRenderers[i].BakeMesh(mesh); //烘焙到新生成的mesh

    GameObject go = new GameObject(); //生成新的GameObject

    go.hideFlags = HideFlags.HideAndDontSave; //设置属性隐藏不保存

    MeshFilter meshFilter = go.AddComponent<MeshFilter>(); //添加MeshFilter

    meshFilter.mesh = mesh; //将新添加的MeshFilter设置为mesh

    MeshRenderer meshRenderer = go.AddComponent<MeshRenderer>();//添加MeshRenderer

    meshRenderer.material = skinedMeshRenderers[i].material;//将新添加的MeshRenderer的材质设置为获取到的材质

    InitFadeInObj(go, skinedMeshRenderers[i].transform.position,
    skinedMeshRenderers[i].transform.rotation, lifeCycle);//淡入淡出
    }

    for (int i = 0; meshFilters != null && i < meshFilters.Length; ++i)
    {
    GameObject go = Instantiate(meshFilters[i].gameObject) as GameObject;
    InitFadeInObj(go, meshFilters[i].transform.position, meshFilters[i].transform.rotation, lifeCycle); //新生成GameObject并且将其设置淡入淡出
    }
    }
    }

    private void InitFadeInObj(GameObject go, Vector3 position, Quaternion rotation, float lifeCycle)
    {
    go.hideFlags = HideFlags.HideAndDontSave; //设置属性隐藏不保存
    go.transform.position = position; //得到position
    go.transform.rotation = rotation; //得到rotation

    FadInOut fi = go.AddComponent<FadInOut>();//添加FadInOut脚本

    fi.lifeCycle = lifeCycle;//复制生命周期

    objs.Add(go);//放入列表中
    }
    }

    //淡入淡出效果实现

    using UnityEngine;
    using System.Collections;

    public class FadInOut : MonoBehaviour {

    public float lifeCycle = 2.0f;

    float startTime;
    Material mat = null;

    // Use this for initialization
    void Start () {
    startTime = Time.time;//得到启动时间

    MeshRenderer meshRenderer = GetComponent<MeshRenderer>();
    if (!meshRenderer || !meshRenderer.material)
    {
    base.enabled = false;//为空的话 禁用
    }
    else
    {
    mat = meshRenderer.material;//得到材质
    ReplaceShader();//替换Shader
    }
    }

    // Update is called once per frame
    void Update () {
    float time = Time.time - startTime;//获取运行时间
    if (time > lifeCycle)
    {
    DestroyImmediate(gameObject);//达到设置时间,销毁
    }
    else
    {
    float remainderTime = lifeCycle - time;//得到剩余时间
    if (mat)
    {
    //if(mat.HasProperty("_Color")) 得到mat中是否有这个属性

    Color col = mat.GetColor("_Color");//得到材质中的shader中的_Color颜色
    col.a = remainderTime;//设置Alpha(剩余时间越小 Alpha值越小)
    mat.SetColor("_Color", col);//将修改后的颜色设置回去

    col = mat.GetColor("_OutlineColor");//原理同上
    col.a = remainderTime;
    mat.SetColor("_OutlineColor", col);
    }
    }
    }

    private void ReplaceShader()
    {
    if (mat.shader.name.Equals("Custom/Toon/Basic Outline"))//检查当前shader名字是否是“ ”
    {
    mat.shader = Shader.Find("Custom/Toon/Basic Outline Replace");//如果是的话 替换shader
    }
    else if (mat.shader.name.Equals("Custom/Toon/Basic"))
    {
    mat.shader = Shader.Find("Custom/Toon/Basic Replace");
    }
    else
    {
    Debug.LogError("Can't find target shader");
    }
    }
    }

  • 相关阅读:
    php和c++自带的排序算法
    谷歌Chrome浏览器离线安装包
    js阻止表单提交
    Sublime text中文乱码解决办法
    不会飞的鸟
    linux命令**50
    linux基础2
    linux基础1
    css3实现单行文本溢出显示省略号
    null和undefined的区别
  • 原文地址:https://www.cnblogs.com/X-dream/p/5154028.html
Copyright © 2020-2023  润新知