Skip to content

Instantly share code, notes, and snippets.

@Shaun-Meechan
Last active January 7, 2024 17:44
Show Gist options
  • Save Shaun-Meechan/bdadfb56fe1c8d02872281d664d0fe1b to your computer and use it in GitHub Desktop.
Save Shaun-Meechan/bdadfb56fe1c8d02872281d664d0fe1b to your computer and use it in GitHub Desktop.
Example of input switching from Shadow Play. This works by having the ControlWatcher in a scene detecting what input method the player is using. If it changes it alerts the GlobalData class which with then send an event to subscribers of IsUsingControllerChanged. An example of a subscriber being TutorialIcon
//Credit: https://forum.unity.com/threads/how-can-i-know-that-any-button-on-gamepad-is-pressed.757322/
//Hnandling device changes doc: https://docs.unity3d.com/Packages/com.unity.inputsystem@1.4/api/UnityEngine.InputSystem.InputDeviceChange.html?q=onDeviceChange
using System;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Utilities;
public class ControlWatcher : MonoBehaviour
{
IDisposable eventListener;
Vector2 joystickDirection;
private void Start()
{
GlobalData.CurrentGamepad = Gamepad.current;
eventListener = InputSystem.onAnyButtonPress.Call(OnControllerButtonPressed);
InputSystem.onDeviceChange += OnDeviceChanged;
}
private void FixedUpdate()
{
if(GlobalData.CurrentGamepad != null)
{
joystickDirection = GlobalData.CurrentGamepad.leftStick.ReadValue();
}
if (joystickDirection != new Vector2(0, 0) && !GlobalData.IsUsingController)
{
GlobalData.IsUsingController = true;
}
}
void OnControllerButtonPressed(InputControl button)
{
var device = button.device;
if (device is Gamepad)
{
if(GlobalData.IsUsingController == false)
{
GlobalData.IsUsingController = true;
}
}
else
{
if(GlobalData.IsUsingController == true)
{
GlobalData.IsUsingController = false;
}
}
}
void OnDeviceChanged(InputDevice device, InputDeviceChange change)
{
switch (change)
{
case InputDeviceChange.Added:
if(device is Gamepad)
{
GlobalData.CurrentGamepad = Gamepad.current;
}
break;
case InputDeviceChange.Removed:
if (device is Gamepad)
{
GlobalData.CurrentGamepad = null;
}
break;
default:
Debug.LogWarning("ERROR: Unable to determine change in device status");
break;
}
}
private void OnDestroy()
{
if (eventListener != null)
{
eventListener.Dispose();
}
}
}
//Parts of this code are AI generated. Please see: https://chat.openai.com/share/73a7ed84-2429-4793-980b-7fc493511306
using System;
using UnityEngine;
using UnityEngine.InputSystem;
public static class GlobalData
{
#region local variables
static bool isInCinematic;
static bool canSpawnClones = true;
static bool isUsingController = false;
static Gamepad currentGamepad;
static bool canUseMultiInteraction = true;
#endregion
#region public variables
public static bool IsInCinematic
{
get { return isInCinematic; }
set
{
isInCinematic = value;
OnIsInCinematicChanged(value);
}
}
public static bool CanSpawnClones
{
get { return canSpawnClones; }
set
{
canSpawnClones = value;
OnCanSpawnClonesChanged(value);
}
}
public static bool IsUsingController
{
get { return isUsingController; }
set
{
if(value == true)
{
Cursor.visible = false;
}
else
{
Cursor.visible = true;
}
isUsingController = value;
OnIsUsingControllerChanged(value);
}
}
public static Gamepad CurrentGamepad
{
get { return currentGamepad; }
set
{
currentGamepad = value;
OnCurrentControllerChanged(value);
}
}
public static bool CanUseMultiInteraction
{
get { return canUseMultiInteraction; }
set
{
canUseMultiInteraction = value;
OnCanUseMultiInteractionChanged(value);
}
}
#endregion
#region event handlers
public static event EventHandler<bool> IsInCinematicChanged;
public static event EventHandler<bool> CanSpawnClonesChanged;
public static event EventHandler<bool> IsUsingControllerChanged;
public static event EventHandler<Gamepad> CurrentGamepadChanged;
public static event EventHandler<bool> CanUseMultiInteractionChanged;
static void OnIsInCinematicChanged(bool newValue)
{
IsInCinematicChanged?.Invoke(null, newValue);
}
static void OnCanSpawnClonesChanged(bool newValue)
{
CanSpawnClonesChanged?.Invoke(null, newValue);
}
static void OnIsUsingControllerChanged(bool newValue)
{
IsUsingControllerChanged?.Invoke(null, newValue);
}
static void OnCurrentControllerChanged(Gamepad newValue)
{
CurrentGamepadChanged?.Invoke(null, newValue);
}
static void OnCanUseMultiInteractionChanged(bool newValue)
{
CanUseMultiInteractionChanged?.Invoke(null, newValue);
}
#endregion
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TutorialIcon : MonoBehaviour
{
[Header("First Set of Icons")]
public GameObject Icon;
private GameObject IconTransition;
[SerializeField]
GameObject controllerIcon;
[SerializeField]
GameObject keyboardIcon;
[Header("Second Set of Icons")]
public GameObject Icon2;
private GameObject IconTransition2;
[SerializeField]
GameObject controllerIcon2;
[SerializeField]
GameObject keyboardIcon2;
//public ParticleSystem Signpost;
public bool Shrink = true;
private float transitionTime;
private float transitionScale;
public bool playerInTrigger;
public bool NextIcon;
// Start is called before the first frame update
void Start()
{
GlobalData.IsUsingControllerChanged += IsUsingControllerChanged;
//IsUsingControllerChanged(null, true);
if (GlobalData.IsUsingController)
{
Icon = controllerIcon;
if (controllerIcon2 != null)
{
Icon2 = controllerIcon2;
IconTransition2 = controllerIcon2;
}
}
else
{
Icon = keyboardIcon;
if (keyboardIcon2 != null)
{
Icon2 = keyboardIcon2;
IconTransition2 = keyboardIcon2;
}
}
}
void IsUsingControllerChanged(object sender, bool e)
{
if (playerInTrigger)
{
if (GlobalData.IsUsingController)
{
IconTransition = controllerIcon;
if (controllerIcon2 != null)
{
IconTransition2 = controllerIcon2;
}
StopCoroutine(Transition());
StartCoroutine(Transition());
}
else
{
StopCoroutine(Transition());
StartCoroutine(Transition());
IconTransition = keyboardIcon;
if (keyboardIcon2 != null)
{
IconTransition2 = keyboardIcon2;
}
}
}
}
// Update is called once per frame
void Update()
{
//print(GlobalData.IsUsingController);
if (Shrink == true)
{
if (transitionTime < 1)
{
transitionTime += Time.deltaTime;
}
transitionScale = Mathf.Lerp(1, 0, transitionTime / 0.2f);
}
else if (Shrink == false)
{
if (transitionTime > 0)
{
transitionTime -= Time.deltaTime;
}
transitionScale = Mathf.Lerp(1, 0, transitionTime /0.2f);
}
if (NextIcon == false)
{
Icon.transform.localScale = new Vector3(transitionScale, transitionScale, transitionScale);
}
else if (NextIcon == true)
{
Icon2.transform.localScale = new Vector3(transitionScale, transitionScale, transitionScale);
}
}
public void IconScaleShrink()
{
Shrink = true;
}
public void IconScaleGrow()
{
Shrink = false;
}
IEnumerator Transition()
{
Shrink = true;
yield return new WaitForSeconds(0.2f);
Icon = IconTransition;
Icon2 = IconTransition2;
Shrink = false;
}
public void StartNextIconCoroutine()
{
StartCoroutine(NextIconTransition());
}
public IEnumerator NextIconTransition()
{
Shrink = true;
yield return new WaitForSeconds(0.2f);
NextIcon = true;
//Icon = IconTransition;
Icon2 = IconTransition2;
Shrink = false;
}
//private void OnTriggerEnter2D(Collider2D collision)
//{
// if (collision.gameObject.CompareTag("Player"))
// {
// playerInTrigger = true;
// IconScaleGrow();
// }
//}
//private void OnTriggerExit2D(Collider2D collision)
//{
// if (collision.gameObject.CompareTag("Player"))
// {
// playerInTrigger = false;
// IconScaleShrink();
// }
//}
private void OnDestroy()
{
GlobalData.IsUsingControllerChanged -= IsUsingControllerChanged;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment