【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】

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM