zoukankan      html  css  js  c++  java
  • 【Unity】透明度渐变

    写给美术大佬的脚本,还要继续改,github地址:TransEffect【github】

    效果图如下:

    Ver.1源码,针对3d Object:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class TransEffect : MonoBehaviour
    {
    
        public List<GoInfo> GoList;
        public float varifySpeed = 0.5f;
        public float aTime = 5f;//每个物体保持出现的时间
        public float dTime = 5f;
    
        private float minAlpha = 0.0f;
        private float maxAlpha = .9f;
        private float curAlpha = 1.0f;
        private float nextAlpha = 0.0f;
        private int i = 0;
    
        public void OnEnable()
        {
            LoadGo();
        }
    
        // Use this for initialization
        void Start()
        {
            //初始化全List隐形
            foreach (GoInfo go in GoList)
            {
                    Color c = go.rend.material.color;
                    c.a = 0;
                    go.rend.material.color = c;
                
            }
        }
    
        // Update is called once per frame
        public void Update()
        {
            Trans();
        }
    
    
        void LoadGo()
        {
            GoList = new List<GoInfo>();
            GoList.Add(new GoInfo("Cylinder", 0, transform.Find("Cylinder").GetComponent<GameObject>(), transform.Find("Cylinder").GetComponent<MeshRenderer>()));
            GoList.Add(new GoInfo("Cube", 1, transform.Find("Cube").GetComponent<GameObject>(), transform.Find("Cube").GetComponent<MeshRenderer>()));
            GoList.Add(new GoInfo("Sphere", 2, transform.Find("Sphere").GetComponent<GameObject>(), transform.Find("Sphere").GetComponent<MeshRenderer>()));
            GoList.Add(new GoInfo("Capsule", 3, transform.Find("Capsule").GetComponent<GameObject>(), transform.Find("Capsule").GetComponent<MeshRenderer>()));
        }
    
    private void Trans()
        {
            GoInfo go = GoList[i];
            GoInfo nextgo;
            Color c = go.rend.material.color;
            Color nextc = go.rend.material.color;
    
            if (i <= GoList.Count)
            {
                if (i == GoList.Count - 1)
                {
                    nextgo = GoList[0];
                }
                else
                {
                    nextgo = GoList[i + 1];
                }
    
                Debug.Log(nextAlpha);
                Debug.Log(curAlpha);
    
                if (Time.time < aTime)//当前物体保持显形
                {
                    c.a = 1;
                    go.rend.material.color = c;
                }
                else if (Time.time >= aTime)
                {
                    curAlpha += Time.deltaTime * varifySpeed * (-1);//当前物体逐渐消失
                    nextAlpha += Time.deltaTime * varifySpeed;//下一个物体逐渐现形
    
                    if (curAlpha <= minAlpha)//当前物体渐变到不透明时
                    {
                        c.a = 0;//设置当前obj保持透明
                        go.rend.material.color = c;
                        i++;
                        //设置数据为下一物体做准备
                        curAlpha = 1;
                        nextAlpha = 0;
                    }
    
                    else//当前物体逐渐透明,下一物体逐渐现形
                    {
                        curAlpha = Mathf.Clamp(curAlpha, minAlpha, maxAlpha);
                        nextAlpha = Mathf.Clamp(nextAlpha, minAlpha, maxAlpha);
                        c.a = curAlpha;
                        nextc.a = nextAlpha;
                        go.rend.material.color = c;
                        nextgo.rend.material.color = nextc;
    
                    }
    
                    if (curAlpha >= maxAlpha)//下一物体完全显形
                    {
                        Debug.Log(nextAlpha);
                        Debug.Log(curAlpha);
                        aTime = Time.time + dTime; //设置新一轮时间限制
                        Debug.Log(aTime);
    
                    }
                }
            }
            else
            {
                i = 0;
            }
        }
    
    }
    
    [System.Serializable]
    public class GoInfo
    {
        public string ID;
        public int index;
        public MeshRenderer rend;
        public GameObject[] obj;
        public GameObject curObj;
        private Color co;
    
        public GoInfo(string id0, int index0, GameObject obj0, MeshRenderer rend0)
        {
            ID = id0;
            index = index0;
            curObj = obj0;
            rend = rend0;
            
        }
    
    }
    View Code

    创建物体:

    写完才发现是要用在UI Image上的...不过其实差别也不大,还略简单点。

    Ver.2源码,针对UI Image:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    
    
    public class TransEffect : MonoBehaviour
    {
        public Transform lib;
        public List<GoInfo> GoList;
        public float varifySpeed = 0.5f;
        public float aTime = 5f;//每个物体保持出现的时间
        public float dTime = 5f;//第一张图片第一轮循环时出现时间
    
        private float minAlpha = 0.0f;
        private float maxAlpha = .9f;
        private float curAlpha = 1.0f;
        private float nextAlpha = 0.0f;
        private int i = 0;
    
    
    
        public void OnEnable()
        {
            LoadGo();
        }
    
        // Use this for initialization
        void Start()
        {
            //初始化全List隐形
            foreach (GoInfo go in GoList)
            {
                    Color c = go.curImg.color;
                    c.a = 0;
                    go.curImg.color = c;
                
            }
        }
    
        // Update is called once per frame
        public void Update()
        {
            Trans();
        }
    
    
        void LoadGo()
        {
            //添加图片列表
            GoList = new List<GoInfo>();
            for (int i = 0; i < lib.childCount; i++) {
                GoList.Add(new GoInfo(lib.GetChild(i).name.ToString(),lib.transform.GetChild(i).GetComponent<Image>()));
            }
            Debug.Log(GoList.Count);
        }
    
    private void Trans()
        {
    
            Debug.Log(i);
            GoInfo go;
            GoInfo nextgo;
    
            if (i >= GoList.Count - 1)
            {
                go = GoList[i];
                nextgo = GoList[0]; 
            }
            else
            {
                go = GoList[i];
                nextgo = GoList[i + 1];
            }
    
    
            Color c = go.curImg.color;
            Color nextc = go.curImg.color;
    
            if (Time.time < aTime)//当前物体保持显形
            {
                c.a = 1;
                go.curImg.color = c;
            }
            else if (Time.time >= aTime)
            {
                curAlpha += Time.deltaTime * varifySpeed * (-1);//当前物体逐渐消失
                nextAlpha += Time.deltaTime * varifySpeed;//下一个物体逐渐现形
    
                if (curAlpha <= minAlpha)//当前物体渐变到不透明时
                {
                    c.a = 0;//设置当前obj保持透明
                    go.curImg.color = c;
    
                    if (i == GoList.Count - 1)
                        i = -1;
                    i++;
                    
                    //设置数据为下一物体做准备
                    curAlpha = 1;
                    nextAlpha = 0;
                }
    
                else//当前物体逐渐透明,下一物体逐渐现形
                {
                    curAlpha = Mathf.Clamp(curAlpha, minAlpha, maxAlpha);
                    nextAlpha = Mathf.Clamp(nextAlpha, minAlpha, maxAlpha);
                    c.a = curAlpha;
                    nextc.a = nextAlpha;
                    go.curImg.color = c;
                    nextgo.curImg.color = nextc;
    
                }
    
                if (curAlpha >= maxAlpha)//下一物体完全显形
                {
                    aTime = Time.time + dTime; //设置新一轮时间限制
                }
            }
    
        }
    
    }
    
    [System.Serializable]
    public class GoInfo
    {
        public string ID;
        public Image[] imgList;
        public Image curImg;
    
        private Color co;
    
        public GoInfo(string id0,Image img)
        {
            ID = id0;
            curImg = img;    
        }
    
    }
    View Code

    直接把存放图片子物体的父物体拖到Lib变量中,再调整所需渐变速度和显示时间即可。

     设置如下:

     

    【over】

  • 相关阅读:
    配置文件管理
    Nacos学习
    dockerCompose学习
    Dockerfile
    vue生命周期
    github使用
    推荐系统
    js笔记17
    js笔记16
    js笔记15
  • 原文地址:https://www.cnblogs.com/liez/p/6984993.html
Copyright © 2011-2022 走看看