playMaker

Author Topic: Cross Platform Input Actions  (Read 2779 times)

KellyRay

  • Full Member
  • ***
  • Posts: 169
    • View Profile
Cross Platform Input Actions
« on: September 23, 2015, 01:21:33 PM »
Hi friends!

I've been toying around with the standard assets package for awhile now and thought I'd share a set of actions I created for the Cross Platform Input asset.

You will need to be sure to import the Cross Platform Input asset:



Let me know if you have any trouble with these! I've attached a zip file with all these down below!

Get Axis Raw:
Code: [Select]
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory("CrossPlatformInput")]
[Tooltip("Gets the value of the specified Input Axis and stores it in a Float Variable. Requires Standard Assets CrossPlatformInput.")]
public class CrossPlatformGetAxisRaw : FsmStateAction
{
[RequiredField]
[Tooltip("The name of the axis. Set in the Unity Input Manager.")]
public FsmString axisName;

[Tooltip("Axis values are in the range -1 to 1. Use the multiplier to set a larger range.")]
public FsmFloat multiplier;

[RequiredField]
[UIHint(UIHint.Variable)]
[Tooltip("Store the result in a float variable.")]
public FsmFloat store;

[Tooltip("Repeat every frame. Typically this would be set to True.")]
public bool everyFrame;

public override void Reset()
{
axisName = "";
multiplier = 1.0f;
store = null;
everyFrame = true;
}

public override void OnEnter()
{
DoGetAxis();

if (!everyFrame)
{
Finish();
}
}

public override void OnUpdate()
{
DoGetAxis();
}

void DoGetAxis()
{
var axisValue = CrossPlatformInputManager.GetAxisRaw(axisName.Value);
// if variable set to none, assume multiplier of 1
if (!multiplier.IsNone)
{
axisValue *= multiplier.Value;
}

store.Value = axisValue;
}
}
}

Axis Event:
Code: [Select]
sing UnityEngine;
using UnityStandardAssets.CrossPlatformInput;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory("CrossPlatformInput")]
[Tooltip("Sends events based on the direction of Cross Platform Input Axis (Left/Right/Up/Down...).")]
public class CrossPlatformAxisEvent : FsmStateAction
{
[Tooltip("Horizontal axis as defined in the Input Manager")]
public FsmString horizontalAxis;

[Tooltip("Vertical axis as defined in the Input Manager")]
public FsmString verticalAxis;

[Tooltip("Event to send if input is to the left.")]
public FsmEvent leftEvent;

[Tooltip("Event to send if input is to the right.")]
public FsmEvent rightEvent;

[Tooltip("Event to send if input is to the up.")]
public FsmEvent upEvent;

[Tooltip("Event to send if input is to the down.")]
public FsmEvent downEvent;

[Tooltip("Event to send if input is in any direction.")]
public FsmEvent anyDirection;

[Tooltip("Event to send if no axis input (centered).")]
public FsmEvent noDirection;

public override void Reset()
{
horizontalAxis = "Horizontal";
verticalAxis = "Vertical";
leftEvent = null;
rightEvent = null;
upEvent = null;
downEvent = null;
anyDirection = null;
noDirection = null;
}

public override void OnUpdate()
{
// get axes offsets

var x = horizontalAxis.Value != "" ? CrossPlatformInputManager.GetAxis(horizontalAxis.Value) : 0;
var y = verticalAxis.Value != "" ? CrossPlatformInputManager.GetAxis(verticalAxis.Value) : 0;

// get squared offset from center

var offset = (x * x) + (y * y);

// no offset?

if (offset.Equals(0))
{
if (noDirection != null)
{
Fsm.Event(noDirection);
}
return;
}

// get integer direction sector (4 directions)
// TODO: 8 directions? or new action?

var angle = (Mathf.Atan2(y, x) * Mathf.Rad2Deg) + 45f;
if (angle < 0f)
{
angle += 360f;
}

var direction = (int)(angle / 90f);

// send events bases on direction

if (direction == 0 && rightEvent != null)
{
Fsm.Event(rightEvent);
//Debug.Log("Right");
}
else if (direction == 1 && upEvent != null)
{
Fsm.Event(upEvent);
//Debug.Log("Up");
}
else if (direction == 2 && leftEvent != null)
{
Fsm.Event(leftEvent);
//Debug.Log("Left");
}
else if (direction == 3 && downEvent != null)
{
Fsm.Event(downEvent);
//Debug.Log("Down");
}
else if (anyDirection != null)
{
// since we already no offset > 0

Fsm.Event(anyDirection);
//Debug.Log("AnyDirection");
}
}
}
}

Get Button:

Code: [Select]
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory("CrossPlatformInput")]
[Tooltip("Gets the pressed state of the specified Button and stores it in a Bool Variable. Requires Standard Assets CrossPlatformInput.")]
public class CrossPlatformGetButton : FsmStateAction
{
[RequiredField]
[Tooltip("The name of the button. Set in the Unity Input Manager.")]
public FsmString buttonName;

[RequiredField]
[UIHint(UIHint.Variable)]
[Tooltip("Store the result in a bool variable.")]
public FsmBool storeResult;

[Tooltip("Repeat every frame.")]
public bool everyFrame;

public override void Reset()
{
buttonName = "Fire1";
storeResult = null;
everyFrame = true;
}

public override void OnEnter()
{
DoGetButton();

if (!everyFrame)
{
Finish();
}
}

public override void OnUpdate()
{
DoGetButton();
}

void DoGetButton()
{
storeResult.Value = CrossPlatformInputManager.GetButton(buttonName.Value);
}
}
}

Get Button Up:

Code: [Select]
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory("CrossPlatformInput")]
[Tooltip("Sends an Event when a Button is released. Requires Standard Assets CrossPlatformInput.")]
public class CrossPlatformGetButtonUp : FsmStateAction
{
[RequiredField]
[Tooltip("The name of the button. Set in the Unity Input Manager.")]
public FsmString buttonName;

[Tooltip("Event to send if the button is released.")]
public FsmEvent sendEvent;

[Tooltip("Set to True if the button is released.")]
[UIHint(UIHint.Variable)]
public FsmBool storeResult;

public override void Reset()
{
buttonName = "Fire1";
sendEvent = null;
storeResult = null;
}

public override void OnUpdate()
{
var buttonUp = CrossPlatformInputManager.GetButtonUp(buttonName.Value);

if (buttonUp)
{
Fsm.Event(sendEvent);
}

storeResult.Value = buttonUp;
}
}
}

Get Button Down:

Code: [Select]
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory("CrossPlatformInput")]
[Tooltip("Sends an Event when a Button is pressed. Requires Standard Assets CrossPlatformInput.")]
public class CrossPlatformGetButtonDown : FsmStateAction
{
[RequiredField]
[Tooltip("The name of the button. Set in the Unity Input Manager.")]
public FsmString buttonName;

[Tooltip("Event to send if the button is pressed.")]
public FsmEvent sendEvent;

[Tooltip("Set to True if the button is pressed.")]
[UIHint(UIHint.Variable)]
public FsmBool storeResult;

public override void Reset()
{
buttonName = "Fire1";
sendEvent = null;
storeResult = null;
}

public override void OnUpdate()
{
var buttonDown = CrossPlatformInputManager.GetButtonDown(buttonName.Value);

if (buttonDown)
{
Fsm.Event(sendEvent);
}

storeResult.Value = buttonDown;
}
}
}
« Last Edit: September 23, 2015, 01:26:33 PM by KellyRay »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 12344
  • Official Playmaker Support
    • View Profile
Re: Cross Platform Input Actions
« Reply #1 on: September 24, 2015, 03:33:35 AM »
Hi,

 That's great. Can I put them on the Ecosystem?

 Bye,

 Jean

KellyRay

  • Full Member
  • ***
  • Posts: 169
    • View Profile
Re: Cross Platform Input Actions
« Reply #2 on: September 24, 2015, 07:18:30 AM »
For sure!

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 12344
  • Official Playmaker Support
    • View Profile
Re: Cross Platform Input Actions
« Reply #3 on: September 24, 2015, 11:05:04 PM »
Hi,

 ok, they are up now :)

I added failsafe compile rules so that if the CrossPlatformInput is not installed, actions will not fail, but show an error in the action interface.

 Bye,

 Jean

KellyRay

  • Full Member
  • ***
  • Posts: 169
    • View Profile
Re: Cross Platform Input Actions
« Reply #4 on: September 25, 2015, 07:11:08 AM »
Awesome! Thanks Jean!