public void Update() { #if UNITY_EDITOR bool bKeyUpdate = false; int i; bKeyUpdate = keyRows == null ? true : keyRows.Length != Rows; if (!bKeyUpdate) { bKeyUpdate = keyRows[0].Length != Columns; } if (bKeyUpdate) { keyRows = new KeyRow[Rows]; for (i = 0; i < Rows; i++) { keyRows[i] = new KeyRow(); keyRows[i].stringArray = new string[Columns]; } } #endif // To Do: Add a method for getting screenspace size between keys // Use our two test keys to determine our overall size. if (TestKey != null && TestKeyTwo != null && Veil.Instance != null && Veil.Instance.HeadTransform != null) { Vector3 headPos = Veil.Instance.HeadTransform.position; Vector3 keyOneVec = TestKey.transform.position - headPos; Vector3 keyTwoVec = TestKeyTwo.transform.position - headPos; float keyAngle = Vector3.Angle(keyOneVec, keyTwoVec); if (DebugText != null) { DebugText.text = "KeyAngle: " + keyAngle; } } }
protected override void OnStart() { base.OnStart(); // Setup the keys - load YAML asset var loader = new StringReader(layout.ToString()); var yaml = new YamlStream(); yaml.Load(loader); var mapping = (YamlMappingNode)yaml.Documents[0].RootNode; var rows = (YamlSequenceNode)mapping.Children[new YamlScalarNode("rows")]; foreach (YamlMappingNode row in rows) { var savedRow = new KeyRow(); var keys = (YamlSequenceNode)row.Children[new YamlScalarNode("keys")]; foreach (YamlMappingNode key in keys) { var savedKey = new PhysicsKey(); savedKey.normal = ((YamlScalarNode)key.Children[new YamlScalarNode("std")]).Value; savedKey.shifted = ((YamlScalarNode)key.Children[new YamlScalarNode("shift")]).Value; savedKey.width = float.Parse(((YamlScalarNode)key.Children[new YamlScalarNode("width")]).Value); //Debug.LogFormat("{0} {1} w:{2}", savedKey.normal, savedKey.shifted, savedKey.width); savedRow.keys.Add(savedKey); } this.keyRows.Add(savedRow); } // Build the keyboard keyContainer = new GameObject("KeyContainer"); keyContainer.transform.SetParent(this.transform, false); keyContainer.transform.localEulerAngles = new Vector3(0.0f, 180.0f, 0.0f); float size = keySize / 100; float gap = keyGap / 100; float xLim = 0.0f; float yLim = 0.0f; float y = 0.0f; foreach (var row in this.keyRows) { float x = 0.0f; foreach (var key in row.keys) { // Instantiate key GameObject newKey = Instantiate(keyPrefab); newKey.transform.SetParent(keyContainer.transform, false); var pos = new Vector3(); pos.x = x + size * key.width * 0.5f; pos.y = y; newKey.transform.localPosition = pos; // Scale newKey.transform.localScale = new Vector3(size, size, size); // Set the Visible label string and the width var dim = newKey.GetComponentInChildren <RectTransform>().sizeDelta; dim.x *= key.width; newKey.GetComponentInChildren <RectTransform>().sizeDelta = dim; newKey.GetComponentInChildren <Text>().text = labelKey(false, key); // Save key properties key.x = x; key.y = y; key.dim = new Vector2(size * key.width, size); key.obj = newKey; x += size * key.width + gap; } if (x > xLim) { xLim = x; } yLim = y; y -= size + gap; } // Place key container to center keyboard keyContainer.transform.localPosition = new Vector3(xLim / 2 + gap, -yLim / 2 - gap); // Resize background panel if (panel) { var scale = panel.transform.localScale; panel.transform.localScale = new Vector3(xLim + 2 * gap, -y + 2 * gap, scale.z); } }