FANDOM


Description: This is a modified SpriteFader and ActionSpriteFade. Made to also fade all child SpriteRenderers. Like with the SpriteFader.cs, you must place the MultiSpriteFader script on the parent Sprite. Then use the "Fade Multiple Sprites" action.

To use these, you need to create two text files and rename them MultiSpriteFader.cs and ActionMultiSpriteFade.cs and then overwrite their contents with the code below. Finally, the ActionMultiSpriteFade.cs needs to be placed in your Custom actions folder (assigned in the Actions manager) or you can drop it in the Actions folder under Adventure Creator/Scripts/Actions.

Base script:

/*
 *
 *	Adventure Creator
 *	by Chris Burton, 2013-2016
 *	
 *	"SpriteFader.cs"
 * 
 *	Attach this to any sprite you wish to fade.
 *  
 *      Alverik:
 *	Modified to fade all child SpriteRenderers
 */

using UnityEngine;
using System.Collections;

namespace AC
{

	/**
	 * Provides functions that can fade a sprite in and out.
	 */
	[AddComponentMenu("Adventure Creator/Misc/Multi Sprite fader")]
	[RequireComponent (typeof (SpriteRenderer))]
	#if !(UNITY_4_6 || UNITY_4_7 || UNITY_5_0)
	[HelpURL("http://www.adventurecreator.org/scripting-guide/class_a_c_1_1_sprite_fader.html")]
	#endif
	public class MultiSpriteFader : MonoBehaviour
	{

		/** True if the Sprite attached to the GameObject this script is attached to is currently fading */
		[HideInInspector] public bool isFading = true;
		/** The time at which the sprite began fading */
		[HideInInspector] public float fadeStartTime;
		/** The duration of the sprite-fading effect */
		[HideInInspector] public float fadeTime;
		/** The direction of the sprite-fading effect (fadeIn, fadeOut) */
		[HideInInspector] public FadeType fadeType;

		private SpriteRenderer[] _spriteRenderers;


		private void Awake ()
		{
			_spriteRenderers = GetComponentsInChildren<SpriteRenderer>();
		}


		/**
		 * <summary>Forces the alpha value of a sprite to a specific value.</summary>
		 * <param name = "_alpha">The alpha value to assign the sprite attached to this GameObject</param>
		 */
		public void SetAlpha (float _alpha)
		{
		    for (var index = 0; index < _spriteRenderers.Length; index++)
		    {
		        var sr = _spriteRenderers[index];
		        Color color = sr.color;
		        color.a = _alpha;
		        sr.color = color;
		    }
		}


		/**
		 * <summary>Fades the Sprite attached to this GameObject in or out.</summary>
		 * <param name = "_fadeType">The direction of the fade effect (fadeIn, fadeOut)</param>
		 * <param name = "_fadeTime">The duration, in seconds, of the fade effect</param>
		 * <param name = "startAlpha">The alpha value that the Sprite should have when the effect begins. If <0, the Sprite's original alpha will be used.</param>
		 */
		public void Fade (FadeType _fadeType, float _fadeTime, float startAlpha = -1)
		{
		    StopAllCoroutines();
		    float currentAlpha = 0f;

		    for (var index = 0; index < _spriteRenderers.Length; index++)
		    {
		        var sr = _spriteRenderers[index];
		        currentAlpha = sr.color.a;

		        if (startAlpha >= 0)
		        {
		            currentAlpha = startAlpha;
		            SetAlpha(startAlpha);
		        }
		        else
		        {
		            if (sr.enabled) continue;
		            sr.enabled = true;

		            if (_fadeType != FadeType.fadeIn) continue;
		            currentAlpha = 0f;
		            SetAlpha(0f);
		        }
		    }


		    if (_fadeType == FadeType.fadeOut)
			{
				fadeStartTime = Time.time - (currentAlpha * _fadeTime);
			}
			else
			{
				fadeStartTime = Time.time - ((1f - currentAlpha) * _fadeTime);
			}
		
			fadeTime = _fadeTime;
			fadeType = _fadeType;

			if (fadeTime > 0f)
			{
			    for (var index = 0; index < _spriteRenderers.Length; index++)
			    {
			        var sr = _spriteRenderers[index];
			        StartCoroutine(DoFade(sr));
			    }
			}
			else
			{
				EndFade ();
			}
		}


		/**
		 * Ends the sprite-fading effect, and sets the Sprite's alpha to its target value.
		 */
		public void EndFade ()
		{
			StopAllCoroutines();

			isFading = false;
		    for (var index = 0; index < _spriteRenderers.Length; index++)
		    {
		        var sr = _spriteRenderers[index];
		        Color color = sr.color;
		        color.a = fadeType == FadeType.fadeIn ? 1f : 0f;
		        sr.color = color;
		    }
		}


		private IEnumerator DoFade (SpriteRenderer sr)
		{
			sr.enabled = true;
			isFading = true;
			Color color = sr.color;
			if (fadeType == FadeType.fadeIn)
			{
				while (color.a < 1f)
				{
					color.a = -1f + AdvGame.Interpolate (fadeStartTime, fadeTime, MoveMethod.Linear, null);
					sr.color = color;
					yield return new WaitForFixedUpdate ();
				}
			}
			else
			{
				while (color.a > 0f)
				{
					color.a = 2f - AdvGame.Interpolate (fadeStartTime, fadeTime, MoveMethod.Linear, null);
					sr.color = color;
					yield return new WaitForFixedUpdate ();
				}
			}
			isFading = false;
		}

	}

}

Modified Action:

/*
 *
  *	Adventure Creator
 *	by Chris Burton, 2013-2016
 *	
  *	"ActionSpriteFade.cs"
 * 
  *	Fades a sprite in or out.
 *	
  *	Alverik:
  *	Modified to use MultiSpriteFader
 * 
  */
 
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 #if UNITY_EDITOR
using UnityEditor;
#endif
 
 namespace AC
 {
 	
 	[System.Serializable]
 	public class ActionMultiSpriteFade : Action
 	{
 		
 		public int constantID = 0;
 		public int parameterID = -1;
 		public MultiSpriteFader spriteFaders;
 		
 		public FadeType fadeType = FadeType.fadeIn;
 		public float fadeSpeed;
 
 		
 		public ActionMultiSpriteFade()
 		{
 			this.isDisplayed = true;
 			category = ActionCategory.Object;
 			title = "Fade multiple sprites";
 			description = "Fades multiple sprites in or out.";
 		}
 		
 		
 		override public void AssignValues (List<ActionParameter> parameters)
 		{
 			spriteFaders = AssignFile <MultiSpriteFader> (parameters, parameterID, constantID, spriteFaders);
 		}
 		
 		
 		override public float Run ()
 		{
 			if (spriteFaders == null)
 			{
 				return 0f;
 			}
 
 			if (!isRunning)
 			{
 				isRunning = true;
 
 				spriteFaders.Fade (fadeType, fadeSpeed);
 
 				if (willWait)
 				{
 					return fadeSpeed;
 				}
 			}
 			else
 			{
 				isRunning = false;
 			}
 			
 			return 0f;
 		}
 
 
 		override public void Skip ()
 		{
 			if (spriteFaders != null)
 			{
 				spriteFaders.Fade (fadeType, 0f);
 			}
 		}
 	
 		
 		#if UNITY_EDITOR
 		
 		override public void ShowGUI (List<ActionParameter> parameters)
 		{
 			parameterID = Action.ChooseParameterGUI ("Sprite to fade:", parameters, parameterID, ParameterType.GameObject);
 			if (parameterID >= 0)
 			{
 				constantID = 0;
 				spriteFaders = null;
 			}
 			else
 			{
 				spriteFaders = (MultiSpriteFader) EditorGUILayout.ObjectField ("Sprite to fade:", spriteFaders, typeof (MultiSpriteFader), true);
 				
 				constantID = FieldToID <MultiSpriteFader> (spriteFaders, constantID);
 				spriteFaders = IDToField <MultiSpriteFader> (spriteFaders, constantID, false);
 			}
 
 			fadeType = (FadeType) EditorGUILayout.EnumPopup ("Type:", fadeType);
 			
 			fadeSpeed = EditorGUILayout.Slider ("Time to fade:", fadeSpeed, 0, 3);
 			willWait = EditorGUILayout.Toggle ("Wait until finish?", willWait);
 
 			AfterRunningOption ();
 		}
 
 
 		override public void AssignConstantIDs (bool saveScriptsToo)
 		{
 			if (saveScriptsToo)
 			{
 				AddSaveScript <RememberVisibility> (spriteFaders);
 			}
 			AssignConstantID <MultiSpriteFader> (spriteFaders, constantID, parameterID);
 		}
 
 		
 		override public string SetLabel ()
 		{
 			string labelAdd = "";
 			
 			if (spriteFaders != null)
 			{
 				labelAdd = " (" + fadeType.ToString () + " " + spriteFaders.gameObject.name + ")";
 			}
 			
 			return labelAdd;
 		}
 		
 		#endif
 		
 	}
 	
 }

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.