Last active
April 26, 2017 22:35
-
-
Save SeeringPhil/8c9611a7dfd8048511074474dc07ca7c to your computer and use it in GitHub Desktop.
This is the class we're using for being able to modify the first player's controls in game and for the assigned controls to persist across scenes ( thus the keycodes being static.)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.UI; | |
using UnityEngine.EventSystems; | |
public class Player1CtrlRemap : MonoBehaviour | |
{ | |
// Initialisation of player's controls. | |
public GameObject upBtn, downBtn, lftBtn, rightBtn; | |
public btnInit bI; | |
public string buttonName; | |
public KeyCode changeKeyCode; | |
public KeyCode bufferKC; | |
public static KeyCode kcForward = KeyCode.W; | |
public static KeyCode kcDownward = KeyCode.S; | |
public static KeyCode kcRight = KeyCode.D; | |
public static KeyCode kcLeft = KeyCode.A; | |
public AudioClip changeSound; | |
public AudioClip wrongSound; | |
float timeLeft; | |
public void OnDestroy() | |
{ | |
} | |
public void Start() | |
{ | |
if (PlayerPrefs.HasKey("player1ctrl")) | |
{ | |
} | |
else | |
{ | |
} | |
} | |
/// <summary> | |
/// OnGUI n'accepte pas de paramètre mais on en a besoin pour savoir quel bouton a été cliqué --> cette fonction | |
/// </summary> | |
/// On doit utiliser cette fonction au lieu de passer directement par OnGUI parce que OnGUI est la seule fonction | |
/// qui peut modifier le texte des boutons de l'interface graphique mais que pour pouvoir reconnaître quel bouton est cliqué, | |
/// on le fait envoyer une string à cette fonction, alors que OnGUI n'accepte pas de paramètres. C'est dans controlRemap que l'on initialise le | |
/// compteur de temps de l'assignation, et que l'on assigne le keycode de basse de change keycode avant de lancer OnGUI. | |
/// <param name="btnName"></param> | |
public void controlRemap(string btnName) | |
{ | |
buttonName = btnName; | |
timeLeft = 5.0f; | |
switch (btnName) | |
{ | |
case "upBtn": | |
changeKeyCode = kcForward; | |
//bufferKC = kcForward; | |
OnGUI(); | |
kcForward = changeKeyCode; | |
break; | |
case "downBtn": | |
changeKeyCode = kcDownward; | |
bufferKC = kcDownward; | |
OnGUI(); | |
kcDownward = changeKeyCode; | |
break; | |
case "leftBtn": | |
changeKeyCode = kcLeft; | |
bufferKC = kcLeft; | |
OnGUI(); | |
kcLeft = changeKeyCode; | |
break; | |
case "rightBtn": | |
changeKeyCode = kcRight; | |
bufferKC = kcRight; | |
OnGUI(); | |
kcRight = changeKeyCode; | |
break; | |
} | |
} | |
/// <summary> | |
/// Fonction qui est utilisée pour gérer la touche appuyée par l'utilisateur après avoir appuyé sur l'un des boutons d'assignation. | |
/// </summary> | |
public void OnGUI() | |
{ | |
Event e = Event.current; | |
if (timeLeft > 1) | |
{ | |
if (e.isKey) | |
{ | |
changeKeyCode = e.keyCode; | |
if (buttonName == "rightBtn") | |
{ | |
if (isValidKey()) | |
{ | |
kcRight = changeKeyCode; | |
rightBtn.GetComponentInChildren<Text>().text = e.keyCode.ToString(); | |
timeLeft = 0; | |
if (GetComponent<AudioSource>()) | |
GetComponent<AudioSource>().PlayOneShot(changeSound, 0.5F); | |
} | |
else | |
{ | |
GetComponent<AudioSource>().PlayOneShot(wrongSound, 0.5F); | |
bI.Invoke("Start", 1); | |
} | |
} | |
else if (buttonName == "leftBtn") | |
{ | |
if (isValidKey()) | |
{ | |
kcLeft = changeKeyCode; | |
lftBtn.GetComponentInChildren<Text>().text = e.keyCode.ToString(); | |
timeLeft = 0; | |
if (GetComponent<AudioSource>()) | |
GetComponent<AudioSource>().PlayOneShot(changeSound, 0.5F); | |
} | |
else | |
GetComponent<AudioSource>().PlayOneShot(wrongSound, 0.5F); | |
} | |
else if (buttonName == "upBtn") | |
{ | |
if (isValidKey()) | |
{ | |
kcForward = changeKeyCode; | |
upBtn.GetComponentInChildren<Text>().text = e.keyCode.ToString(); | |
timeLeft = 0; | |
if (GetComponent<AudioSource>()) | |
GetComponent<AudioSource>().PlayOneShot(changeSound, 0.5F); | |
} | |
else | |
GetComponent<AudioSource>().PlayOneShot(wrongSound, 0.5F); | |
} | |
else if (buttonName == "downBtn") | |
{ | |
if (isValidKey()) | |
{ | |
kcDownward = changeKeyCode; | |
downBtn.GetComponentInChildren<Text>().text = e.keyCode.ToString(); | |
timeLeft = 0; | |
if (GetComponent<AudioSource>()) | |
GetComponent<AudioSource>().PlayOneShot(changeSound, 0.5F); | |
} | |
else | |
GetComponent<AudioSource>().PlayOneShot(wrongSound, 0.5F); | |
} | |
} | |
timeLeft = timeLeft - Time.deltaTime; | |
} | |
} | |
/// <summary> | |
/// Vérifie que la touche n'est pas déjà utilisée. Permet de réduire la vérification dans le code. | |
/// </summary> | |
/// <returns>vrai si le changeKeyCode est différent de ceux précédemment assignés </returns> | |
public bool isValidKey() | |
{ | |
return (changeKeyCode != Player2CtrlRemap.kcRight && changeKeyCode != Player2CtrlRemap.kcLeft && changeKeyCode != Player2CtrlRemap.kcForward && changeKeyCode != Player2CtrlRemap.kcDownward && changeKeyCode != kcRight && changeKeyCode != kcLeft && changeKeyCode != kcForward && changeKeyCode != kcDownward); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment