Multi Sprite Fader (Script+CustomAction)

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 (MultiSpriteFader.cs):

```
using UnityEngine;
using System.Collections;

namespace AC
{

 [AddComponentMenu("Adventure Creator/Misc/Multi Sprite fader")]
 [RequireComponent (typeof (SpriteRenderer))]
 [HelpURL("http://www.adventurecreator.org/scripting-guide/class\_a\_c\_1\_1\_sprite\_fader.html")]
 #endif
 public class MultiSpriteFader : MonoBehaviour
 {

 [HideInInspector] public bool isFading = true;
 [HideInInspector] public float fadeStartTime;
 [HideInInspector] public float fadeTime;
 [HideInInspector] public FadeType fadeType;

 private SpriteRenderer[] \_spriteRenderers;

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

 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;
 }
 }

 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 ();
 }
 }

 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;
 }

 }

}

```
Custom Action (ActionMultiSpriteFade.cs):

```
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 override ActionCategory Category { get { return ActionCategory.Object; }}
 public override string Title { get { return "Fade multiple sprites"; }}
 public override string Description { get { return "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);
 }

 override public void AssignConstantIDs (bool saveScriptsToo, bool fromAssetFile)
 {
 if (saveScriptsToo)
 {
 AddSaveScript <RememberVisibility> (spriteFaders);
 }
 AssignConstantID <MultiSpriteFader> (spriteFaders, constantID, parameterID);
 }

 #endif

 }

 }

```