You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
2.7 KiB
87 lines
2.7 KiB
// This code is part of the Fungus library (http://fungusgames.com) maintained by Chris Gregan (http://twitter.com/gofungus). |
|
// It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE) |
|
|
|
using UnityEngine; |
|
using UnityEngine.Serialization; |
|
using Fungus.Variables; |
|
|
|
namespace Fungus.Commands |
|
{ |
|
/// <summary> |
|
/// Makes a sprite visible / invisible by setting the color alpha. |
|
/// </summary> |
|
[CommandInfo("Sprite", |
|
"Show Sprite", |
|
"Makes a sprite visible / invisible by setting the color alpha.")] |
|
[AddComponentMenu("")] |
|
[ExecuteInEditMode] |
|
public class ShowSprite : Command |
|
{ |
|
[Tooltip("Sprite object to be made visible / invisible")] |
|
[SerializeField] protected SpriteRenderer spriteRenderer; |
|
|
|
[Tooltip("Make the sprite visible or invisible")] |
|
[SerializeField] protected BooleanData _visible = new BooleanData(false); |
|
|
|
[Tooltip("Affect the visibility of child sprites")] |
|
[SerializeField] protected bool affectChildren = true; |
|
|
|
public override void OnEnter() |
|
{ |
|
if (spriteRenderer != null) |
|
{ |
|
if (affectChildren) |
|
{ |
|
SpriteRenderer[] children = spriteRenderer.gameObject.GetComponentsInChildren<SpriteRenderer>(); |
|
foreach (SpriteRenderer sr in children) |
|
{ |
|
SetSpriteAlpha(sr, _visible.Value); |
|
} |
|
} |
|
else |
|
{ |
|
SetSpriteAlpha(spriteRenderer, _visible.Value); |
|
} |
|
} |
|
|
|
Continue(); |
|
} |
|
|
|
protected virtual void SetSpriteAlpha(SpriteRenderer renderer, bool visible) |
|
{ |
|
Color spriteColor = renderer.color; |
|
spriteColor.a = visible ? 1f : 0f; |
|
renderer.color = spriteColor; |
|
} |
|
|
|
public override string GetSummary() |
|
{ |
|
if (spriteRenderer == null) |
|
{ |
|
return "Error: No sprite renderer selected"; |
|
} |
|
|
|
return spriteRenderer.name + " to " + (_visible.Value ? "visible" : "invisible"); |
|
} |
|
|
|
public override Color GetButtonColor() |
|
{ |
|
return new Color32(221, 184, 169, 255); |
|
} |
|
|
|
#region Backwards compatibility |
|
|
|
[HideInInspector] [FormerlySerializedAs("visible")] public bool visibleOLD; |
|
|
|
protected virtual void OnEnable() |
|
{ |
|
if (visibleOLD != default(bool)) |
|
{ |
|
_visible.Value = visibleOLD; |
|
visibleOLD = default(bool); |
|
} |
|
} |
|
|
|
#endregion |
|
} |
|
} |