public PianoBuilder(MIDIPlayer midiPlayer, PianoKeyObjectSettings whiteKeyObjectSettings, PianoKeyObjectSettings blackKeyObjectSettings) { this.midiPlayer = midiPlayer; this.whiteKeyObjectSettings = whiteKeyObjectSettings; this.blackKeyObjectSettings = blackKeyObjectSettings; whiteKeyBounds = GetBounds(whiteKeyObjectSettings.prefab); Bounds blackKeyBounds = GetBounds(blackKeyObjectSettings.prefab); blackKeyOffset = new Vector3(0, blackKeyBounds.size.y * 0.5f, 0); }
private void CreatePianoKey(Transform parent, PianoKeyColor pianoKeyColor, int midiNote, float distanceBetweenWhiteKeys) { PianoKeyObjectSettings pianoKeyObjectSettings = GetPianoKeyObjectSettings(pianoKeyColor); Vector3 position = GetPosition(pianoKeyColor, pianoKeyObjectSettings, distanceBetweenWhiteKeys); GameObject pianoKeyObject = PrefabUtility.InstantiatePrefab(pianoKeyObjectSettings.prefab) as GameObject; pianoKeyObject.transform.parent = parent; pianoKeyObject.transform.localPosition = position; pianoKeyObject.transform.localRotation = Quaternion.identity; //PianoKey pianoKey = pianoKeyObject.GetComponent<PianoKey>(); PianoKey pianoKey = pianoKeyObject.GetComponentInChildren <PianoKey>(); pianoKey.SetMidiPlayer(midiPlayer); pianoKey.SetMidiNote(midiNote); }
private Vector3 GetPosition(PianoKeyColor pianoKeyColor, PianoKeyObjectSettings pianoKeyObjectSettings, float distanceBetweenWhiteKeys) { Vector3 position = nextWhiteKeyPosition; Vector3 baseOffset = pianoKeyObjectSettings.baseOffset; if (pianoKeyColor == PianoKeyColor.WHITE) { nextWhiteKeyPosition += Vector3.right * (whiteKeyBounds.size.x + distanceBetweenWhiteKeys); } else if (pianoKeyColor == PianoKeyColor.BLACK) { position += Vector3.left * (whiteKeyBounds.size.x + distanceBetweenWhiteKeys) * 0.5f + blackKeyOffset; } return(position + baseOffset); }
private void OnGUI() { parent = (Transform)EditorGUILayout.ObjectField("Parent: ", parent, typeof(Transform), true); GUILayout.Space(10); // Button: Clear parent EditorGUI.BeginDisabledGroup(parent == null); if (GUILayout.Button("Clear parent")) { PianoBuilder.ClearParentOfChildren(parent); } EditorGUI.EndDisabledGroup(); GUILayout.Space(10); midiPlayer = (MIDIPlayer)EditorGUILayout.ObjectField("MidiPlayer: ", midiPlayer, typeof(MIDIPlayer), true); whiteKeyObjectSettings = (PianoKeyObjectSettings)EditorGUILayout.ObjectField("WhiteKey Object Settings: ", whiteKeyObjectSettings, typeof(PianoKeyObjectSettings), false); blackKeyObjectSettings = (PianoKeyObjectSettings)EditorGUILayout.ObjectField("BlackKey Object Settings: ", blackKeyObjectSettings, typeof(PianoKeyObjectSettings), false); GUILayout.Space(20); if (IsRequiredValuesSet()) { PianoBuilder pianoBuilder = new PianoBuilder(midiPlayer, whiteKeyObjectSettings, blackKeyObjectSettings); octaves = EditorGUILayout.IntField("Octaves: ", octaves); skippedOctaves = EditorGUILayout.IntField("Skipped Octaves: ", skippedOctaves); GUILayout.Space(20); distanceBetweenWhiteKeys = EditorGUILayout.FloatField("Distance Between WhiteKeys: ", distanceBetweenWhiteKeys); if (GUILayout.Button("Build")) { pianoBuilder.BuildPiano(parent, octaves, skippedOctaves, distanceBetweenWhiteKeys); } } }