// 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 ;
namespace Fungus
{
/// <summary>
/// Sends a message to either the owner Flowchart or all Flowcharts in the scene. Blocks can listen for this message using a Message Received event handler.
/// </summary>
[ CommandInfo ( "Flow" ,
"Send Message" ,
"Sends a message to either the owner Flowchart or all Flowcharts in the scene. Blocks can listen for this message using a Message Received event handler." ) ]
[AddComponentMenu("")]
[ExecuteInEditMode]
public class SendMessage : Command
{
public enum MessageTarget
{
SameFlowchart ,
AllFlowcharts
}
[Tooltip("Target flowchart(s) to send the message to")]
[SerializeField] protected MessageTarget messageTarget ;
[Tooltip("Name of the message to send")]
[SerializeField] protected StringData _ message = new StringData ( "" ) ;
public override void OnEnter ( )
{
if ( _ message . Value . Length = = 0 )
{
Continue ( ) ;
return ;
}
MessageReceived [ ] receivers = null ;
if ( messageTarget = = MessageTarget . SameFlowchart )
{
receivers = GetComponents < MessageReceived > ( ) ;
}
else
{
receivers = GameObject . FindObjectsOfType < MessageReceived > ( ) ;
}
if ( receivers ! = null )
{
foreach ( MessageReceived receiver in receivers )
{
receiver . OnSendFungusMessage ( _ message . Value ) ;
}
}
Continue ( ) ;
}
public override string GetSummary ( )
{
if ( _ message . Value . Length = = 0 )
{
return "Error: No message specified" ;
}
return _ message . Value ;
}
public override Color GetButtonColor ( )
{
return new Color32 ( 2 3 5 , 1 9 1 , 2 1 7 , 2 5 5 ) ;
}
#region Backwards compatibility
[HideInInspector] [ FormerlySerializedAs ( "message" ) ] public string messageOLD = "" ;
protected virtual void OnEnable ( )
{
if ( messageOLD ! = "" )
{
_ message . Value = messageOLD ;
messageOLD = "" ;
}
}
# endregion
}
}