Playmaker Forum

PlayMaker Updates & Downloads => Share New Actions => Topic started by: KellyRay on September 23, 2015, 04:21:33 PM

Title: Cross Platform Input Actions
Post by: KellyRay on September 23, 2015, 04: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:

(http://s3.postimg.org/6cpfx4o1b/Screen_Shot_2015_09_23_at_3_23_22_PM.jpg?noCache=1443039830)

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;
}
}
}
Title: Re: Cross Platform Input Actions
Post by: jeanfabre on September 24, 2015, 06:33:35 AM
Hi,

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

 Bye,

 Jean
Title: Re: Cross Platform Input Actions
Post by: KellyRay on September 24, 2015, 10:18:30 AM
For sure!
Title: Re: Cross Platform Input Actions
Post by: jeanfabre on September 25, 2015, 02:05:04 AM
Hi,

 ok, they are up (https://twitter.com/JeanAtPlayMaker/status/647290386617094144) 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
Title: Re: Cross Platform Input Actions
Post by: KellyRay on September 25, 2015, 10:11:08 AM
Awesome! Thanks Jean!
Title: Re: Cross Platform Input Actions
Post by: homemacai on October 08, 2020, 09:11:53 PM
Strange i can't get this to work, when i import the package on the post i get this:
 The type or namespace name 'UnityStandardAssets' could not be found etc...

And when i try use the ones that are on the ecossytem, i get the error that i don't have the Cross Platform input asset, which i have no idea where to get.
Any help would be appreciated, thanks
Title: Re: Cross Platform Input Actions
Post by: djaydino on October 09, 2020, 05:34:33 AM
Hi.
The Unity Standard Assets package should be on the asset store
You need to get that 1st.

it also can be on the package manger for later versions than 2018.4
(Window/Package Manager)
Title: Re: Cross Platform Input Actions
Post by: homemacai on October 09, 2020, 12:03:05 PM
Thank you!!