Last active
November 21, 2022 09:44
-
-
Save asus4/9b60fe6bf2fae85db7484fe8d351c649 to your computer and use it in GitHub Desktop.
USB BarcodeScanner input on Unity3D
This file contains hidden or 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 System; | |
using System.Collections.Generic; | |
using System.Text; | |
using UnityEngine; | |
namespace AppKit | |
{ | |
/// <summary> | |
/// Barcode Scanner Input | |
/// | |
/// Tested on http://amzn.asia/6JDu51P | |
/// </summary> | |
public class BarcodeScanner : MonoBehaviour | |
{ | |
public event Action<string> OnScanned; | |
StringBuilder builder; | |
void Start() | |
{ | |
builder = new StringBuilder(); | |
} | |
void Update() | |
{ | |
string str = Input.inputString; | |
if (!string.IsNullOrEmpty(str)) | |
{ | |
builder.Append(str); | |
} | |
if (Input.GetKeyDown(KeyCode.Return)) | |
{ | |
if (OnScanned != null) | |
{ | |
OnScanned(builder.ToString().TrimEnd('\r', '\n')); | |
} | |
builder.Clear(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment