ピアノをつくる


鍵盤用のCube、白と黒のマテリアル、
音源(https://www.assetstore.unity3d.com/jp/#!/content/27633)
をResources以下に用意

Resources
https://docs.unity3d.com/jp/540/ScriptReference/Resources.html

鍵盤が沈んで戻るアニメーション

KeyTouch.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class KeyTouch : MonoBehaviour {

    float myY;

    void Start () {
        myY = transform.position.y;
    }

    void OnMouseDown() {
        GetComponent<AudioSource>().Play();
        iTween.MoveTo(gameObject, iTween.Hash("y", myY - 0.005f, "time", 0.1f, "oncomplete", "keyBack"));
    }

    void keyBack() {
        iTween.MoveTo(gameObject, iTween.Hash("y", myY, "time", 0.4f));
    }
}

このスクリプトをアタッチしたGameObjectの直下に鍵盤を生成する

MakePiano.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MakePiano : MonoBehaviour {

    private Transform MyTr;
    private string[] Kcodes = { "C","D","E","F","G","A","B" };

    void Start () {
        MyTr = transform;
        MakeKeys();
    }

    void MakeKeys() {
        float KeySpace = 0.021f;
        for (int Octave = 0; Octave < 7; Octave++) { // 7オクターブ
            for (int sc = 0; sc < 7; sc++) { // 7音階
                float Xloc = Octave * (KeySpace * 7) + sc * KeySpace;
                GameObject theKey = Instantiate(
                    Resources.Load("Cube") as GameObject,
                    MyTr.position + new Vector3(Xloc, 0, 0),
                    MyTr.rotation
                );
                /*
                 * 白鍵
                 */
                theKey.transform.localScale = new Vector3(0.02f, 0.02f, 0.1f);
                // マテリアル
                theKey.GetComponent<Renderer>().material = Resources.Load("White") as Material;
                // 音
                theKey.name = Kcodes[sc] + (Octave + 1);
                AudioSource ac = theKey.AddComponent<AudioSource>();
                ac.clip = Resources.Load("Choir Piano (Mp3)/" + theKey.name) as AudioClip;
                // スクリプト
                theKey.AddComponent<KeyTouch>(); // KeyTouchはAssets以下のどこに置いても大丈夫
                theKey.transform.parent = MyTr; // Pianoの中に鍵盤を入れる

                /*
                 * 黒鍵
                 */
                // BかE以外に黒鍵をつける
                if(Kcodes[sc] != "B" && Kcodes[sc] != "E") {
                    Xloc += KeySpace / 2; // 真ん中に
                    GameObject theBKey = Instantiate(
                        Resources.Load("Cube") as GameObject,
                        MyTr.position + new Vector3(Xloc, 0.01f, 0.02f),
                        MyTr.rotation
                    );
                    theBKey.transform.localScale = new Vector3(0.015f, 0.02f, 0.06f);
                    theBKey.name = Kcodes[sc] + "#" + (Octave + 1);
                    theBKey.GetComponent<Renderer>().material = Resources.Load("Black") as Material;
                    AudioSource ac2 = theBKey.AddComponent<AudioSource>();
                    ac2.clip = Resources.Load("Choir Piano (Mp3)/" + theBKey.name) as AudioClip;
                    theBKey.AddComponent<KeyTouch>();
                    theBKey.transform.parent = MyTr;
                }
            }
        }
    }

}

完成イメージ

参考
http://unity.incd2.jp/