Advertisement
mew_fi

spawn virtual keyboard in unity

Mar 21st, 2023
919
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.99 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5.  
  6. public class KeyboardPlacement : MonoBehaviour {
  7.     public GameObject centerObject; // The game object around which the keys will be placed
  8.     public float radius = 2.0f; // The radius of the hemisphere
  9.     public float height = 1.0f; // The height of the hemisphere
  10.     public float keySpacing = 0.1f; // The spacing between each key
  11.     public float horizontalDegrees = 80.0f; // The angle in degrees of the left and right edges of the keyboard
  12.     public float verticalDegrees = 60.0f; // The angle in degrees of the top and bottom edges of the keyboard
  13.  
  14.     private List<Vector3> positions = new List<Vector3>();
  15.     private string[] keys = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "a", "s", "d", "f", "g", "h", "j", "k", "l", ";", "z", "x", "c", "v", "b", "n", "m", ",", ".", "-" };
  16.     private bool keysCreated = false;
  17.  
  18.     //void OnDrawGizmosSelected() {
  19.     //    if (GUILayout.Button("Create Keyboard")) {
  20.     //        CreateKeys();
  21.     //    }
  22.     //}
  23.  
  24.     private void Start() {
  25.         CreateKeys();
  26.     }
  27.  
  28.     void CalculatePositions() {
  29.         // Calculate the number of rows and columns of keys
  30.         int numRows = 4;
  31.         int[] numColumns = { 10, 10, 10, 10 };
  32.  
  33.         // Calculate the angle between each key
  34.         float[] horizontalAngle = new float[] {
  35.         horizontalDegrees / (numColumns[0] - 1),
  36.         horizontalDegrees / (numColumns[1] - 1),
  37.         horizontalDegrees / (numColumns[2] - 1),
  38.         horizontalDegrees / (numColumns[3] - 1)
  39.     };
  40.         float verticalAngle = verticalDegrees / (numRows - 1);
  41.  
  42.         // Calculate the position of each key
  43.         int keyIndex = 0;
  44.         for (int row = numRows - 1; row >= 0; row--) {
  45.             float rowOffset = 0f;
  46.             if (row == 1) {
  47.                 rowOffset = horizontalAngle[0] / 2;
  48.             } else if (row == 2) {
  49.                 rowOffset = horizontalAngle[1] / 2;
  50.             } else if (row == 3) {
  51.                 rowOffset = horizontalAngle[2] / 2;
  52.             }
  53.  
  54.             // Align the middle column to be between G and H
  55.             float centerColumnOffset = 0.5f * horizontalAngle[1];
  56.  
  57.             for (int col = 0; col < numColumns[row]; col++) {
  58.                 float x = radius * Mathf.Cos(Mathf.Deg2Rad * verticalAngle * (row - (numRows - 1) / 2)) * Mathf.Sin(Mathf.Deg2Rad * (horizontalAngle[row] * col + rowOffset - (numColumns[row] - 1) / 2 * horizontalAngle[row] - centerColumnOffset));
  59.                 float y = radius * Mathf.Sin(Mathf.Deg2Rad * verticalAngle * (row - (numRows - 1) / 2));
  60.                 float z = radius * Mathf.Cos(Mathf.Deg2Rad * verticalAngle * (row - (numRows - 1) / 2)) * Mathf.Cos(Mathf.Deg2Rad * (horizontalAngle[row] * col + rowOffset - (numColumns[row] - 1) / 2 * horizontalAngle[row] - centerColumnOffset));
  61.  
  62.                 Vector3 position = new Vector3(x, y, z);
  63.  
  64.                 // Rotate the position around the center object's forward vector
  65.                 position = Quaternion.LookRotation(centerObject.transform.forward) * position;
  66.  
  67.                 position += centerObject.transform.position;
  68.                 positions.Add(position);
  69.                 keyIndex++;
  70.             }
  71.         }
  72.     }
  73.  
  74.  
  75.  
  76.  
  77.  
  78.     void InstantiateKeys() {
  79.         GameObject cubePrefab = GameObject.CreatePrimitive(PrimitiveType.Cube);
  80.         cubePrefab.transform.localScale = Vector3.one * 0.2f;
  81.  
  82.         for (int i = 0; i < keys.Length; i++) {
  83.             GameObject key = new GameObject(keys[i]);
  84.             key.transform.position = positions[i];
  85.             key.transform.parent = transform;
  86.  
  87.             GameObject cube = Instantiate(cubePrefab, key.transform.position, Quaternion.identity);
  88.             cube.transform.parent = key.transform;
  89.         }
  90.     }
  91.  
  92.  
  93.     void CreateKeys() {
  94.         CalculatePositions();
  95.         InstantiateKeys();
  96.         keysCreated = true;
  97.     }
  98. }
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement