Unity5学習の際の覚え書き。

Rendererとは

そのオブジェクトが実際にカメラに表示される、表面の描画に関する機能。
rendererプロパティに保管されている。

スペースを押している間だけオブジェクトを消すスクリプト

public class myscript : MonoBehaviour {

	void Start () {
	}

	void Update () {
		transform.Rotate (1f, 1f, 0.1f);
		if (Input.GetKeyDown (KeyCode.Space)) {
			GetComponent<Renderer>().enabled = false;
		}
		if (Input.GetKeyUp (KeyCode.Space)) {
			GetComponent<Renderer>().enabled = true;
		}
	}
}

Material

Materialは、Rendererの「material」プロパティに保管される。

Materialクラスのプロパティ

color
マテリアルのカラーに関するもの。
Color構造体のインスタンスとして保管されている。

mainTexture
メインテクスチャに関するもの。
マテリアルのインスペクターで「_MainTex」として設定されているもの。
Textureクラスのインスタンスが保管される。

mainTextureOffset
メインテクスチャのオフセットを示すもの。
「Vector2」というクラスのインスタンス。
Vector3の2次元版で、xとyの値だけからなる。

mainTextureScale
メインテクスチャの大きさを示すもの。
Vector2インスタンスで保管されている。

passCount
マテリアルのパスの数を示すもの。
値はint。
主にGLクラスで使用される。

renderQueue
マテリアルのレンダーキューの値。
マテリアルのインスペクターに表示される「Render queue」の値。
int値。

shader
マテリアルで使用するシェーダーを示すもの。
Shaderクラスのインスタンスとして保管される。

shaderKeywords
シェーダーに設定されている来ワード(定数の値)を示すもの。
string配列として保管される。

レンダラーの色を操作するスクリプト

public class myscript : MonoBehaviour {

	void Start () {
	}

	void Update () {
		transform.Rotate (1f, 1f, 0.1f);
		Color c = GetComponent<Renderer>().material.color;
		if (Input.GetKey (KeyCode.RightArrow)) {
			c.r += 0.01f;
			if (c.r > 1.0f) {
				c.r = 1.0f;
			}
			c.b -= -0.01f;
			if (c.b < 0.0f) {
				c.b = 0.0f;
			}
			GetComponent<Renderer>().material.color = c;
		}
		if (Input.GetKey (KeyCode.LeftArrow)) {
			c.r -= 0.01f;
			if (c.r < 0.0f) {
				c.r = 0.0f;
			}
			c.b += 0.01f;
			if (c.b > 1.0f) {
				c.b = 1.0f;
			}
			GetComponent<Renderer>().material.color = c;
		}
	}
}

Color構造体のプロパティについて


Color c = renderer.material.color;

// c.r ... 赤の輝度。0.0〜1.0までの実数(float値)で設定されている。
// c.g ... 緑の輝度。0.0〜1.0までの実数(float値)で設定されている。
// c.b ... 青の輝度。0.0〜1.0までの実数(float値)で設定されている。
// c.a ... 透明度 (アルファチャンネル)。0.0〜1.0までの実数(float値)で設定されている。

Colorインスタンス

Colorインスタンスの作成
変数 = new Color ( 赤, 緑, 青 );
変数 = new Color ( 赤, 緑, 青, アルファチャンネル );
Colorに用意されている色名プロパティ
red ... 赤のColorインスタンス
blue ... 青のColorインスタンス
green ... 緑のColorインスタンス
black ... 黒のColorインスタンス
white ... 白のColorインスタンス
cyan ... シアンのColorインスタンス
magenta ... マゼンタのColorインスタンス
yellow ... 黄色のColorインスタンス
grey (gray) ... グレー(灰色)のColorインスタンス
clear ... RGBAの値すべてをゼロにしたColorインスタンス

Color操作の注意点

  • CubeやSphereは上記の操作で可能
  • GuiTextは、GuiTextクラス内にcolorプロパティが用意されているのでそれを操作する

GuiTextの色を変えるスクリプト

public class myscript : MonoBehaviour {

	void Start () {
		GameObject.Find("GUI Text").GetComponent<GUIText>().text = "This is Sample!";
	}

	void Update () {
		transform.Rotate (1f, 1f, 0.1f);
		GameObject obj = GameObject.Find ("GUI Text");
		Color c = obj.GetComponent<GUIText>().color;
		if (Input.GetKey (KeyCode.RightArrow)) {
			c.a += 0.01f;
			if (c.a > 1.0f) {
				c.a = 1.0f;
			}
			obj.GetComponent<GUIText>().color = c;
		}
		if (Input.GetKey (KeyCode.LeftArrow)) {
			c.a -= 0.01f;
			if (c.a < 0.0f) {
				c.a = 0.0f;
			}
			obj.GetComponent<GUIText>().color = c;
		}
	}
}

コメントをかく


「http://」を含む投稿は禁止されています。

利用規約をご確認のうえご記入下さい

Menu

【メニュー編集】
Wiki記法ガイド

リンク

ゲームバー大阪心斎橋

ゲームバー大阪梅田

ゲームバー大阪心斎橋

ダーツ&ダイニングバー大阪梅田

メニュー

Unity


スマホ操作

Unity - GameObject

Unity - 3DCG

メンバーのみ編集できます