Skip to content

Instantly share code, notes, and snippets.

@g0ody
Created June 5, 2013 12:45
Show Gist options
  • Save g0ody/5713595 to your computer and use it in GitHub Desktop.
Save g0ody/5713595 to your computer and use it in GitHub Desktop.
Unity3D 2DPlatformController (Plugin) States to PlayMaker (Plugin) Events
using UnityEngine;
using System.Collections.Generic;
using System;
using HutongGames.PlayMaker;
using HutongGames.PlayMaker.Actions;
/// <summary>
/// Logs the animation state to the console.
/// </summary>
public class CharacterStateSender : MonoBehaviour {
private RaycastCharacterController _controller;
[HideInInspector]
public CharacterStateEvent[] stateEvents;
private Dictionary<CharacterState,FsmEvent> _stateEventLookup = new Dictionary<CharacterState,FsmEvent>();
void Awake() {
_controller = GetComponent<RaycastCharacterController>();
_controller.CharacterAnimationEvent += new RaycastCharacterController.CharacterControllerEventDelegate (CharacterAnimationEvent);
Array.ForEach<CharacterStateEvent>(stateEvents, delegate(CharacterStateEvent stateEvent) { _stateEventLookup.Add(stateEvent.state, stateEvent.fsmEvent); });
}
public void CharacterAnimationEvent (CharacterState state, CharacterState previousState) {
if(_stateEventLookup.ContainsKey(state))
PlayMakerFSM.BroadcastEvent(_stateEventLookup[state].Name);
}
}
[System.Serializable]
public class CharacterStateEvent {
public CharacterState state = CharacterState.IDLE;
public FsmEvent fsmEvent = new FsmEvent("CHARACTER_IDLE");
}
using UnityEngine;
using UnityEditor;
using HutongGames.PlayMakerEditor;
using HutongGames.PlayMaker;
using System.Collections;
[CustomEditor(typeof(CharacterStateSender))]
public class CharacterStateSenderEditor : Editor {
bool showStates = false;
int size = 0;
int lostFocusCounter = 0;
CharacterStateSender stateSender;
void OnEnable() {
stateSender = target as CharacterStateSender;
size = stateSender.stateEvents.Length;
}
public override void OnInspectorGUI() {
EditorGUILayout.BeginVertical();
EditorGUILayout.BeginHorizontal();
showStates = EditorGUILayout.Foldout(showStates, "Character State Events");
if(GUI.GetNameOfFocusedControl() == "size")
lostFocusCounter = 0;
else
lostFocusCounter++;
GUI.SetNextControlName("size");
size = EditorGUILayout.IntField(size);
if(size != stateSender.stateEvents.Length && lostFocusCounter > 2 ) {
System.Array.Resize<CharacterStateEvent>(ref stateSender.stateEvents, size);
}
EditorGUILayout.EndHorizontal();
if (showStates) {
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField("Character State");
GUILayout.Box("", GUILayout.ExpandWidth(true), GUILayout.Width(1));
EditorGUILayout.LabelField("Event");
EditorGUILayout.EndHorizontal();
System.Array.ForEach<CharacterStateEvent>(stateSender.stateEvents, delegate(CharacterStateEvent stateEvent) {
if(stateEvent != null) {
EditorGUILayout.BeginHorizontal();
stateEvent.state = (CharacterState)EditorGUILayout.EnumPopup(stateEvent.state);
GUILayout.Box("", GUILayout.ExpandWidth(true), GUILayout.Width(1));
stateEvent.fsmEvent = FsmEditorGUILayout.EventPopup(GUIContent.none, FsmEvent.EventList, stateEvent.fsmEvent);
EditorGUILayout.EndHorizontal();
}
});
}
EditorGUILayout.EndVertical();
if (GUI.changed)
EditorUtility.SetDirty(target);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment