UI界面之淡入淡出
1.
using UnityEngine; using System.Collections; using UnityEngine.UI; public class danrudanchu : MonoBehaviour { public Image image; public float showTime = 10;//10秒换一次 public float ShowTimeTrigger = 0;//计时 public float fadeTime = 3; public float fadeTimeTrigger = 0; private bool show = true; // Use this for initialization void Start() { } // Update is called once per frame void Update() { ShowTimeTrigger += Time.deltaTime;//计时 if (ShowTimeTrigger > showTime) { if (fadeTimeTrigger >= 0 && fadeTimeTrigger <fadeTime) { fadeTimeTrigger += Time.deltaTime; if (show) { image.color = new Color(1, 1, 1, 1 - (fadeTimeTrigger / fadeTime)); } else { image.color = new Color(1, 1, 1, (fadeTimeTrigger / fadeTime)); } } else { fadeTimeTrigger = 0; ShowTimeTrigger = 0; if (show) { show = false; } else { show = true; } } } } }
2.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class danruchu : MonoBehaviour { public Image image; public float showTime = 3;//3秒换一次 public float starttime = 0;//计时 public float time = 0; bool b = true; // Use this for initialization void Start () { } // Update is called once per frame void Update () { starttime+= Time.deltaTime; if (starttime > showTime) { if (time >= 0 && time < 1) { time += Time.deltaTime; if (b) { image.color = new Color(1, 1, 1, 1 - time); } else { image.color = new Color(1, 1, 1, time); } } else { time = 0; starttime = 0; if (b) { b = false; } else { b = true; } } } } }
3.
这也可以淡入淡出: //this.gameObject.GetComponent<Image>().CrossFadeAlpha(0, 5, false); GetComponent<Image>().CrossFadeColor(new Color(1, 1, 1, 0), 5, true, true);
莫说我穷的叮当响,大袖揽清风。
莫讥我困时无处眠,天地做床被。
莫笑我渴时无美酒,江湖来做壶。