Unity上でPlayボタンを押すとオブジェクトの色が変わるスクリプト
やりたいこと
Unity上でPlayボタンを押すと、立方体の色が青からオレンジに変わるスクリプトを作成します。
Unityの画面構成、マウス操作、C#の基本などは以下の投稿を参考にしてください。
Unityとc#の超入門 ~基礎編~
流れ
- オブジェクトを作成
- マテリアルを作成
- オブジェクトにマテリアルを追加
- スクリプトを追加
- スクリプトを編集
- 実行ボタンを押すと、オブジェクトの色が変わる
詳細
1. オブジェクトを作成
1. オブジェクトを作成
HierarchyウィンドウのCreate -> 3D Object -> Cube の順にクリックして、立方体のオブジェクトを作成します。
2. マテリアルを作成
ProjectウィンドウのAssetsフォルダ内で右クリックし、Create -> Materialを選択します。名称をtest_materialに変更します。
3. オブジェクトにマテリアルを追加
test_materialを、HierarchyウィンドウのCubeにドラッグ&ドロップします。これで、マテリアルをオブジェクトに結び付ける(アタッチする)ことができました。
4. スクリプトを追加
HierarchyウィンドウのCubeをクリックし、InspectorウィンドウのAdd Component -> New Scriptを選択します。名称を変更し、Create & addをクリックします。スクリプトが生成されました。
5. スクリプトを編集
生成されたスクリプトcolor_changeをダブルクリックし、Visual Studioを起動、以下のソースに変更し、保存します。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class color_change : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
// // 赤色に変更する
gameObject.GetComponent<Renderer>().material.color = Color.red;
}
// Update is called once per frame
void Update()
{
}
}
6. 実行ボタンを押すと、オブジェクトの色が変わる
Author And Source
この問題について(Unity上でPlayボタンを押すとオブジェクトの色が変わるスクリプト), 我々は、より多くの情報をここで見つけました https://qiita.com/harufuji/items/4d1b4c4f25e2b668e3e3著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .