Quantcast
Channel: Answers by "ThePersister"
Viewing all articles
Browse latest Browse all 99

Answer by ThePersister

$
0
0
Hi @Biggerplay, @nestordu and others interested as well. Here's an example scene, using the code below: https://www.dropbox.com/s/uxsoj802z1sphpn/FadeImageInOut.unitypackage?dl=0 Try it without plugins! :) using UnityEngine; using UnityEngine.UI; using System.Collections; public class FadeImageInOut : MonoBehaviour { public Image m_targetImage; public float m_fadeDuration = 1f; public float m_stayDuration = 2f; public AnimationCurve m_smoothCurve = new AnimationCurve( new Keyframe[] { new Keyframe( 0f, 0f ), new Keyframe( 1f, 1f ) } ); private float m_timerCurrent; private readonly WaitForSeconds m_skipFrame = new WaitForSeconds( 0.01f ); void Start() { if (m_targetImage != null) { StartCoroutine( FadeInOut() ); } else { Debug.LogWarning( "Please set TargetImage in the Inspector before trying this." ); } } private IEnumerator FadeInOut() { float start = 0f; float end = 1f; yield return StartCoroutine( Fade( start, end ) ); // Fade in. yield return new WaitForSeconds( m_stayDuration ); // Stay yield return StartCoroutine( Fade( end, start ) ); // Fade out. } private IEnumerator Fade(float start, float end) { m_timerCurrent = 0f; while (m_timerCurrent <= m_fadeDuration) { m_timerCurrent += Time.deltaTime; Color c = m_targetImage.color; m_targetImage.color = new Color( c.r, c.g, c.b, Mathf.Lerp( start, end, m_smoothCurve.Evaluate( m_timerCurrent / m_fadeDuration ) ) ); yield return m_skipFrame; } } } I noticed this is a question from 2015. I hope it helps regardless! If you're reading this OP and it did help, then accepting the answer would be much appreciated! Best of luck!

Viewing all articles
Browse latest Browse all 99

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>