Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Modify 2DGameKit for DS4 Input?

Discussion in 'Scripting' started by KatyPIllman, Feb 21, 2018.

  1. KatyPIllman

    KatyPIllman

    Joined:
    Dec 24, 2016
    Posts:
    9
    The default script is for PC/Xbox controlling, and trying to get Dualshock 4 input. How can the PlayerInput.cs be modified to achieve this?

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. namespace Gamekit2D
    4. {
    5.     public class PlayerInput : InputComponent, IDataPersister
    6.     {
    7.         public static PlayerInput Instance
    8.         {
    9.             get { return s_Instance; }
    10.         }
    11.  
    12.         protected static PlayerInput s_Instance;
    13.    
    14.    
    15.         public bool HaveControl { get { return m_HaveControl; } }
    16.        
    17.         public InputButton Pause = new InputButton(KeyCode.Escape, XboxControllerButtons.Menu);
    18.         public InputButton Interact = new InputButton(KeyCode.E, XboxControllerButtons.Y);
    19.         public InputButton MeleeAttack = new InputButton(KeyCode.K, XboxControllerButtons.X);
    20.         public InputButton RangedAttack = new InputButton(KeyCode.O, XboxControllerButtons.B);
    21.         public InputButton Jump = new InputButton(KeyCode.Space, XboxControllerButtons.A);
    22.         public InputAxis Horizontal = new InputAxis(KeyCode.D, KeyCode.A, XboxControllerAxes.LeftstickHorizontal);
    23.         public InputAxis Vertical = new InputAxis(KeyCode.W, KeyCode.S, XboxControllerAxes.LeftstickVertical);
    24.        
    25.  
    26.         [HideInInspector]
    27.         public DataSettings dataSettings;
    28.        
    29.         protected bool m_HaveControl = true;
    30.  
    31.         protected bool m_DebugMenuIsOpen = false;
    32.  
    33.         void Awake ()
    34.         {
    35.             if (s_Instance == null)
    36.                 s_Instance = this;
    37.             else
    38.                 throw new UnityException("There cannot be more than one PlayerInput script.  The instances are " + s_Instance.name + " and " + name + ".");
    39.         }
    40.  
    41.         void OnEnable()
    42.         {
    43.             if (s_Instance == null)
    44.                 s_Instance = this;
    45.             else if(s_Instance != this)
    46.                 throw new UnityException("There cannot be more than one PlayerInput script.  The instances are " + s_Instance.name + " and " + name + ".");
    47.        
    48.             PersistentDataManager.RegisterPersister(this);
    49.         }
    50.  
    51.         void OnDisable()
    52.         {
    53.             PersistentDataManager.UnregisterPersister(this);
    54.  
    55.             s_Instance = null;
    56.         }
    57.  
    58.         protected override void GetInputs(bool fixedUpdateHappened)
    59.         {
    60.             Pause.Get(fixedUpdateHappened, inputType);
    61.             Interact.Get(fixedUpdateHappened, inputType);
    62.             MeleeAttack.Get(fixedUpdateHappened, inputType);
    63.             RangedAttack.Get(fixedUpdateHappened, inputType);
    64.             Jump.Get(fixedUpdateHappened, inputType);
    65.             Horizontal.Get(inputType);
    66.             Vertical.Get(inputType);
    67.  
    68.             if (Input.GetKeyDown(KeyCode.F12))
    69.             {
    70.                 m_DebugMenuIsOpen = !m_DebugMenuIsOpen;
    71.             }
    72.         }
    73.  
    74.         public override void GainControl()
    75.         {
    76.             m_HaveControl = true;
    77.  
    78.             GainControl(Pause);
    79.             GainControl(Interact);
    80.             GainControl(MeleeAttack);
    81.             GainControl(RangedAttack);
    82.             GainControl(Jump);
    83.             GainControl(Horizontal);
    84.             GainControl(Vertical);
    85.         }
    86.  
    87.         public override void ReleaseControl(bool resetValues = true)
    88.         {
    89.             m_HaveControl = false;
    90.  
    91.             ReleaseControl(Pause, resetValues);
    92.             ReleaseControl(Interact, resetValues);
    93.             ReleaseControl(MeleeAttack, resetValues);
    94.             ReleaseControl(RangedAttack, resetValues);
    95.             ReleaseControl(Jump, resetValues);
    96.             ReleaseControl(Horizontal, resetValues);
    97.             ReleaseControl(Vertical, resetValues);
    98.         }
    99.  
    100.         public void DisableMeleeAttacking()
    101.         {
    102.             MeleeAttack.Disable();
    103.         }
    104.  
    105.         public void EnableMeleeAttacking()
    106.         {
    107.             MeleeAttack.Enable();
    108.         }
    109.  
    110.         public void DisableRangedAttacking()
    111.         {
    112.             RangedAttack.Disable();
    113.         }
    114.  
    115.         public void EnableRangedAttacking()
    116.         {
    117.             RangedAttack.Enable();
    118.         }
    119.  
    120.         public DataSettings GetDataSettings()
    121.         {
    122.             return dataSettings;
    123.         }
    124.  
    125.         public void SetDataSettings(string dataTag, DataSettings.PersistenceType persistenceType)
    126.         {
    127.             dataSettings.dataTag = dataTag;
    128.             dataSettings.persistenceType = persistenceType;
    129.         }
    130.  
    131.         public Data SaveData()
    132.         {
    133.             return new Data<bool, bool>(MeleeAttack.Enabled, RangedAttack.Enabled);
    134.         }
    135.  
    136.         public void LoadData(Data data)
    137.         {
    138.             Data<bool, bool> playerInputData = (Data<bool, bool>)data;
    139.  
    140.             if (playerInputData.value0)
    141.                 MeleeAttack.Enable();
    142.             else
    143.                 MeleeAttack.Disable();
    144.  
    145.             if (playerInputData.value1)
    146.                 RangedAttack.Enable();
    147.             else
    148.                 RangedAttack.Disable();
    149.         }
    150.  
    151.         void OnGUI()
    152.         {
    153.             if (m_DebugMenuIsOpen)
    154.             {
    155.                 const float height = 100;
    156.  
    157.                 GUILayout.BeginArea(new Rect(30, Screen.height - height, 200, height));
    158.  
    159.                 GUILayout.BeginVertical("box");
    160.                 GUILayout.Label("Press F12 to close");
    161.  
    162.                 bool meleeAttackEnabled = GUILayout.Toggle(MeleeAttack.Enabled, "Enable Melee Attack");
    163.                 bool rangeAttackEnabled = GUILayout.Toggle(RangedAttack.Enabled, "Enable Range Attack");
    164.  
    165.                 if (meleeAttackEnabled != MeleeAttack.Enabled)
    166.                 {
    167.                     if (meleeAttackEnabled)
    168.                         MeleeAttack.Enable();
    169.                     else
    170.                         MeleeAttack.Disable();
    171.                 }
    172.  
    173.                 if (rangeAttackEnabled != RangedAttack.Enabled)
    174.                 {
    175.                     if (rangeAttackEnabled)
    176.                         RangedAttack.Enable();
    177.                     else
    178.                         RangedAttack.Disable();
    179.                 }
    180.                 GUILayout.EndVertical();
    181.                 GUILayout.EndArea();
    182.             }
    183.         }
    184.     }
    185. }
    186.  
    Full Source: http://u3d.as/12xR
     
  2. safegame

    safegame

    Joined:
    Jun 16, 2021
    Posts:
    1
    Hi did you found the solution?