15
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ピアノをつくる

Last updated at Posted at 2017-10-03

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

スクリーンショット 2017-10-04 2.33.40.png

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;
                }
            }
		}
    }

}

完成イメージ
スクリーンショット 2017-10-04 2.32.11.png

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

15
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?