more ctrlpnl

- moved panel for easier access\
- gave third-person toggle its own code
- refactor panel button code
- instead of lock all buttons, hide non-game buttons
This commit is contained in:
msk
2022-05-23 18:25:05 -07:00
parent 4ef7cd0bbf
commit 65a8ee2770
5 changed files with 676 additions and 54 deletions
+30 -15
View File
@@ -15,6 +15,8 @@ public class PanelButton : MonoBehaviour
public VirtualKeyCode key2;
public bool isToggle;
public bool doesBeep;
public bool isOn;
private int _insideColliderCount = 0;
@@ -30,24 +32,41 @@ public class PanelButton : MonoBehaviour
audioSrc = GetComponent<AudioSource>();
audioSrc.playOnAwake = false;
audioSrc.Stop();
audioSrc.clip = btnSound;
Debug.Log(cr.material.color);
if (isToggle)
{
// initialize toggle state
OnTriggerEnter(new Collider());
OnTriggerExit(new Collider());
ButtonPress();
ButtonRelease();
}
}
private void OnTriggerEnter(Collider other)
{
_insideColliderCount += 1;
audioSrc.Play();
if (doesBeep)
audioSrc.Play();
ButtonPress();
}
private void OnTriggerExit(Collider other)
{
_insideColliderCount = Mathf.Clamp(_insideColliderCount - 1, 0, _insideColliderCount);
if (_insideColliderCount == 0)
{
ButtonRelease();
}
}
private void ButtonPress()
{
if (isToggle)
{
if(!isOn)
if (!isOn)
{
cr.material.color = Color.green;
keybd_event(System.Convert.ToByte(key2), (byte)MapVirtualKey((uint)key2, 0), 2, UIntPtr.Zero);
@@ -65,23 +84,19 @@ public class PanelButton : MonoBehaviour
}
else
{
cr.material.color = Color.white;
keybd_event(System.Convert.ToByte(key), (byte)MapVirtualKey((uint)key, 0), 0, UIntPtr.Zero);
}
}
private void OnTriggerExit(Collider other)
private void ButtonRelease()
{
_insideColliderCount = Mathf.Clamp(_insideColliderCount - 1, 0, _insideColliderCount);
if (_insideColliderCount == 0)
{
keybd_event(System.Convert.ToByte(key), (byte)MapVirtualKey((uint)key, 0), 2, UIntPtr.Zero);
keybd_event(System.Convert.ToByte(key2), (byte)MapVirtualKey((uint)key2, 0), 2, UIntPtr.Zero);
}
keybd_event(System.Convert.ToByte(key), (byte)MapVirtualKey((uint)key, 0), 2, UIntPtr.Zero);
keybd_event(System.Convert.ToByte(key2), (byte)MapVirtualKey((uint)key2, 0), 2, UIntPtr.Zero);
if (!isToggle)
cr.material.color = Color.gray;
}
// Update is called once per frame
void Update()
{
+3 -4
View File
@@ -21,7 +21,7 @@ public class PanelLocker : MonoBehaviour
[SerializeField]
private AudioSource audioSrc;
[SerializeField]
private List<PanelButton> panelButtons;
private List<GameObject> panelButtons;
[Header("Assets")]
[SerializeField]
@@ -67,11 +67,10 @@ public class PanelLocker : MonoBehaviour
timerRing.color = Color.cyan;
statusImg.texture = isLocked ? lockImg : unlockImg;
// set buttons' state
foreach (var btn in panelButtons)
{
btn.GetComponent<Collider>().enabled = !isLocked;
btn.SetActive(!isLocked);
}
actionTaken = true;
+41
View File
@@ -0,0 +1,41 @@
using System;
using System.Runtime.InteropServices;
using UnityEngine;
using WindowsInput.Native;
[RequireComponent(typeof(AudioSource))]
public class PanelThirdPersonButton : MonoBehaviour
{
public bool isTP;
private Renderer cr;
public GameObject tpCamera;
private AudioSource audioSrc;
private static AudioClip btnSound;
void Start()
{
btnSound = Resources.Load<AudioClip>("Audio/button press");
audioSrc = GetComponent<AudioSource>();
audioSrc.playOnAwake = false;
audioSrc.clip = btnSound;
cr = GetComponent<Renderer>();
SetTP(isTP);
}
private void OnTriggerEnter(Collider other)
{
audioSrc.Play();
isTP = !isTP;
SetTP(isTP);
}
private void SetTP(bool state)
{
isTP = state;
cr.material.color = state ? Color.green : Color.red;
tpCamera?.SetActive(state);
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 447b0e47a465da145b212861d2c70c27
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: