2022-05-18 06:38:11 +02:00
|
|
|
using System;
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
using UnityEngine;
|
|
|
|
using WindowsInput.Native;
|
|
|
|
|
2022-05-24 01:59:50 +02:00
|
|
|
[RequireComponent(typeof(AudioSource))]
|
|
|
|
public class PanelButton : MonoBehaviour
|
2022-05-18 06:38:11 +02:00
|
|
|
{
|
|
|
|
[DllImport("user32.dll")]
|
|
|
|
public static extern uint MapVirtualKey(uint uCode, uint uMapType);
|
|
|
|
[DllImport("user32.dll")]
|
|
|
|
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);
|
2022-05-24 01:59:50 +02:00
|
|
|
|
2022-05-18 06:38:11 +02:00
|
|
|
public VirtualKeyCode key;
|
2022-05-23 07:00:35 +02:00
|
|
|
public VirtualKeyCode key2;
|
2022-05-24 01:59:50 +02:00
|
|
|
|
2022-05-23 07:00:35 +02:00
|
|
|
public bool isToggle;
|
2022-05-24 03:25:05 +02:00
|
|
|
public bool doesBeep;
|
|
|
|
|
2022-05-23 07:00:35 +02:00
|
|
|
public bool isOn;
|
2022-05-24 01:59:50 +02:00
|
|
|
private int _insideColliderCount = 0;
|
|
|
|
|
2022-05-23 07:00:35 +02:00
|
|
|
private Renderer cr;
|
2022-05-18 06:38:11 +02:00
|
|
|
public GameObject camera;
|
2022-05-24 01:59:50 +02:00
|
|
|
private AudioSource audioSrc;
|
2022-05-24 02:14:40 +02:00
|
|
|
private static AudioClip btnSound;
|
2022-05-23 07:00:35 +02:00
|
|
|
|
|
|
|
void Start()
|
2022-05-24 01:59:50 +02:00
|
|
|
{
|
|
|
|
btnSound = Resources.Load<AudioClip>("Audio/button press");
|
|
|
|
cr = GetComponent<Renderer>();
|
2022-05-23 07:00:35 +02:00
|
|
|
|
2022-05-24 01:59:50 +02:00
|
|
|
audioSrc = GetComponent<AudioSource>();
|
|
|
|
audioSrc.playOnAwake = false;
|
|
|
|
audioSrc.clip = btnSound;
|
2022-05-23 07:00:35 +02:00
|
|
|
|
2022-05-24 01:59:50 +02:00
|
|
|
if (isToggle)
|
|
|
|
{
|
|
|
|
// initialize toggle state
|
2022-05-24 03:25:05 +02:00
|
|
|
ButtonPress();
|
|
|
|
ButtonRelease();
|
2022-05-24 01:59:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-18 06:38:11 +02:00
|
|
|
private void OnTriggerEnter(Collider other)
|
|
|
|
{
|
2022-05-24 01:59:50 +02:00
|
|
|
_insideColliderCount += 1;
|
2022-05-24 09:21:47 +02:00
|
|
|
ButtonPress();
|
2022-05-24 03:25:05 +02:00
|
|
|
if (doesBeep)
|
|
|
|
audioSrc.Play();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnTriggerExit(Collider other)
|
|
|
|
{
|
|
|
|
_insideColliderCount = Mathf.Clamp(_insideColliderCount - 1, 0, _insideColliderCount);
|
|
|
|
|
|
|
|
if (_insideColliderCount == 0)
|
|
|
|
{
|
|
|
|
ButtonRelease();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void ButtonPress()
|
|
|
|
{
|
2022-05-24 01:59:50 +02:00
|
|
|
if (isToggle)
|
2022-05-23 07:00:35 +02:00
|
|
|
{
|
2022-05-24 03:25:05 +02:00
|
|
|
if (!isOn)
|
2022-05-23 07:00:35 +02:00
|
|
|
{
|
2022-05-24 01:59:50 +02:00
|
|
|
cr.material.color = Color.green;
|
|
|
|
keybd_event(System.Convert.ToByte(key2), (byte)MapVirtualKey((uint)key2, 0), 2, UIntPtr.Zero);
|
2022-05-23 07:00:35 +02:00
|
|
|
keybd_event(System.Convert.ToByte(key), (byte)MapVirtualKey((uint)key, 0), 0, UIntPtr.Zero);
|
2022-05-24 01:59:50 +02:00
|
|
|
isOn = true;
|
|
|
|
}
|
|
|
|
else
|
2022-05-23 07:00:35 +02:00
|
|
|
{
|
2022-05-24 01:59:50 +02:00
|
|
|
cr.material.color = Color.red;
|
|
|
|
keybd_event(System.Convert.ToByte(key), (byte)MapVirtualKey((uint)key, 0), 2, UIntPtr.Zero);
|
2022-05-23 07:00:35 +02:00
|
|
|
keybd_event(System.Convert.ToByte(key2), (byte)MapVirtualKey((uint)key2, 0), 0, UIntPtr.Zero);
|
2022-05-24 01:59:50 +02:00
|
|
|
isOn = false;
|
2022-05-23 07:00:35 +02:00
|
|
|
}
|
|
|
|
|
2022-05-24 01:59:50 +02:00
|
|
|
}
|
|
|
|
else
|
2022-05-23 07:00:35 +02:00
|
|
|
{
|
2022-05-24 03:25:05 +02:00
|
|
|
cr.material.color = Color.white;
|
2022-05-23 07:00:35 +02:00
|
|
|
keybd_event(System.Convert.ToByte(key), (byte)MapVirtualKey((uint)key, 0), 0, UIntPtr.Zero);
|
|
|
|
}
|
2022-05-18 06:38:11 +02:00
|
|
|
}
|
|
|
|
|
2022-05-24 03:25:05 +02:00
|
|
|
private void ButtonRelease()
|
2022-05-18 06:38:11 +02:00
|
|
|
{
|
2022-05-24 03:25:05 +02:00
|
|
|
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;
|
2022-05-18 06:38:11 +02:00
|
|
|
}
|
|
|
|
}
|