Here is the local SendFSMEvent<T> data version as extension method for Gameobject.
Note: I set the gameobject sender automatically out of convenience, if the data is free.
#region GameObjectExtensions
public static class GameObjectExtensions
{
private enum TypeCodeExt
{
Unknown,
Vector2,
Vector3,
Quaternion,
Color,
Rect,
UnityObject,
GameObject,
Material,
Texture
}
private static readonly Dictionary<Type, TypeCodeExt> TypeDict = new Dictionary<Type, TypeCodeExt>
{
{typeof(GameObject), TypeCodeExt.GameObject},
{typeof(Object), TypeCodeExt.UnityObject},
{typeof(Vector2), TypeCodeExt.Vector2},
{typeof(Vector3), TypeCodeExt.Vector3},
{typeof(Rect), TypeCodeExt.Rect},
{typeof(Quaternion), TypeCodeExt.Quaternion},
{typeof(Color), TypeCodeExt.Color},
{typeof(Material), TypeCodeExt.Material},
{typeof(Texture), TypeCodeExt.Texture}
};
/// <summary>
/// simple wrapper to send a data event to a FSM/Childs
/// </summary>
public static void SendFsmEvent<T>(this GameObject inObj, string inEventName, T inData)
{
if (string.IsNullOrEmpty(inEventName) || Equals(inData, default(T))) {
inObj.DebugLogErrorInputs(); // check "invalid" values on T?? (string/reftype/value/nullable)
return;
}
var foundFsm = inObj.GetComponentInChildren<PlayMakerFSM>(); // TODO: searchChild and global version!
if (foundFsm == null) {
inObj.DebugLogError("Cant find a FSM on the gameobject + childs");
return;
}
TypeCode dataType = Type.GetTypeCode(typeof(T));
if (dataType == TypeCode.Object && TypeDict.ContainsKey(typeof(T))) {
TypeCodeExt extType = TypeDict[typeof(T)];
switch (extType) {
// NOTE: we need to down/up-cast all, since c# has no other way without IConvertible
case TypeCodeExt.Vector2:
Fsm.EventData.Vector2Data = (Vector2)(System.Object)inData;
break;
case TypeCodeExt.Vector3:
Fsm.EventData.Vector3Data = (Vector3)(System.Object)inData;
break;
case TypeCodeExt.Quaternion:
Fsm.EventData.QuaternionData = (Quaternion)(System.Object)inData;
break;
case TypeCodeExt.Color:
Fsm.EventData.ColorData = (Color)(System.Object)inData;
break;
case TypeCodeExt.Rect:
Fsm.EventData.RectData = (Rect)(System.Object)inData;
break;
case TypeCodeExt.UnityObject:
Fsm.EventData.ObjectData = (Object)(System.Object)inData;
break;
case TypeCodeExt.GameObject:
Fsm.EventData.GameObjectData = (GameObject)(System.Object)inData;
break;
case TypeCodeExt.Material:
Fsm.EventData.MaterialData = (Material)(System.Object)inData;
break;
case TypeCodeExt.Texture:
Fsm.EventData.TextureData = (Texture)(System.Object)inData;
break;
default:
inObj.DebugLogError("Unsupported EventDataType: " + typeof(T));
break;
}
if (extType != TypeCodeExt.GameObject) {
Fsm.EventData.GameObjectData = inObj; // NOTE: Helper we always set the senderObj here also!
}
}
else {
switch (dataType) {
case TypeCode.Boolean:
Fsm.EventData.BoolData = (bool)(System.Object)inData;
break;
case TypeCode.Int32:
case TypeCode.UInt32:
Fsm.EventData.IntData = (int)(System.Object)inData;
break;
case TypeCode.Single:
case TypeCode.Double:
Fsm.EventData.FloatData = (float)(System.Object)inData;
break;
case TypeCode.String:
Fsm.EventData.StringData = (string)(System.Object)inData;
break;
default:
inObj.DebugLogError("Unsupported EventDataType: " + typeof(T));
break;
}
Fsm.EventData.GameObjectData = inObj; // NOTE: Helper we always set the senderObj here also!
}
foundFsm.SendEvent(inEventName);
}
}
#endregion GameObjectExtensions