LoginSignup
25
25

More than 5 years have passed since last update.

Unity GameObjectの色変更(C#)

Last updated at Posted at 2014-08-07

ソースからGameObjectの色を変更する。

GameObjectにMaterial追加

GameObject作成

Hierarchy > Create > Cube
SceneにCubeができる。

Material作成

Project > Create > Material
作ったMaterialの丸いとこクリックするとColorやShaderを変更できる。

GameObjectにMaterial追加

作ったMaterialをSceneのCubeにドラッグ&ドロップ
これでcube(GameObject)にMaterialが追加され、CubeのInspectorから確認できる。

GameObjectのMaterialの色をコードで変更

cubeのInspectorからAdd Component > New script

changeColorScript.cs
using UnityEngine;
using System.Collections;

public class ChangeColorScript : MonoBehaviour {

    public GameObject myCube;

    // Use this for initialization
    void Start () {

        //gameObject取得 
        myCube = GameObject.Find("CubeName");

        //今の色コンソールに出力
        Debug.Log(myCube.renderer.material.color);

        //青色に変更
        myCube.renderer.material.color = Color.blue;

        //変更後の色コンソールに出力
        Debug.Log(myCube.renderer.material.color);
    }

    // Update is called once per frame
    void Update () {

    }
}

シーンを再生したらキューブの色が青に変わる。

Unityでの色の設定方法

いくつか方法がある。
http://docs.unity3d.com/ScriptReference/Color.html

  • Unityが用意したカラーネームを使う
    Color.red
  • RGBAで設定
    new Color(1f, 0.92f, 0.016f, 1f);
  • ランダムな色 Color randomColor = new Color( Random.value, Random.value, Random.value, 1.0f );

参照)
http://docs-jp.unity3d.com/Documentation/ScriptReference/GameObject-renderer.html

25
25
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
25
25