//tips
//[SerializeField]の使い方
インスペクターにpublicではない値を表示したいときに使用する[SerializeField]の使い方を確認する。
主な使い道は、インスペクターウィンドウで編集できるようにすることで、スクリプトに下記のように記載することで
[SerializeField] int maxScore = 100;
[SerializeField] int minScore = 0;
maxScoreとminScoreをインスペクター上で編集することができる。
Unity画面を見ながら値を調整したい場合に便利。
Publicではなく、[SerializeField] が使われる理由としては、publicにしてしまうと異なるクラスから書き換えを行うことが可能になるからである。
例えば、
public class TestA : MonoBehaviour {
[SerializeField] int maxScore = 100;
[SerializeField] int minScore = 0;
}
に対して、
[SerializeField] TestB testB;
を追加し、別途TestBを下記のように記した場合、
public class TestB : MonoBehaviour {
public int maxLife = 500;
}
TestAのStart()から、下記のようにTestBのpublicフィールドを変更できてしまうのである。
public class TestA : MonoBehaviour {
...
void Start () {
testB.maxLife = 1000;
}
...
}
privateメンバーをインスペクターに表示させたい際、変数の前に[SerializeField]と書いて用いるのが一般的。
//unity3D配列の利用
配列の宣言は2パターンあり、
配列の要素数を先に決めてしまうint[] array = new int[3];のようなパターン
int[] array = new int[3]; // arrayを生成
array[0] = 100; // 1つ目の要素へ、整数100を代入
array[1] = 101; // 2つ目の要素へ、整数101を代入
array[2] = 102; // 3つ目の要素へ、整数102を代入
と要素の内容を決めてしまうint[] array={10,20,30};のようなパターン
shotPos = new Vector3[] {
new Vector3(Random.value, Random.value, Random.value),
new Vector3(Random.value, Random.value, Random.value),
new Vector3(Random.value, Random.value, Random.value),
},
が存在する。
実際に下記のスクリプトを新たなarrayオブジェクトにアタッチし、動作を確認した。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class array : MonoBehaviour
{
void Start()
{
// GameObjectを保持する配列の生成
GameObject[] array = new GameObject[3];
// 各要素へ検索し取得してきたGameObjectを代入
array[0] = GameObject.Find("Wall");
array[1] = GameObject.Find("Walla");
array[2] = GameObject.Find("Wallb");
// ループして、オブジェクト名をログに出力
foreach (GameObject i in array)
{
Debug.Log(i.name);
}
}
}
問題なく動作した。
Forのループを使用するスクリプトを作ると
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class array2 : MonoBehaviour
{
public Text arrayText;
private string[] stringArray = new string[10]; //配列の宣言
void Start()
{
arrayText.text = ""; //テキストの初期化
for (int i = 0; i < stringArray.Length; i++)
{
stringArray[i] = Random.Range(1, 100).ToString(); //乱数の生成
arrayText.text = arrayText.text + stringArray[i] + ","; //テキストの上書き
}
}
}
このようになり、canvasのtextに数字を羅列させることができる。
//transform.positionでの複数障害物との接触処理
障害物の配列の作成とそれのforeachで複数障害物との接触判定を行う。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController7 : MonoBehaviour
{
[SerializeField]//インスペクターにpublicではない変数を表示させる属性
Vector3[] wallPoses = new Vector3[] {
new Vector3(0, 0, 3.0f),
new Vector3(0, 0, 6.0f),
new Vector3(8.0f, 0, 3.0f),
};
// Update is called once per frame
void Update()
{
Vector3 nextPosition = transform.position;//同じ処理はまとめたほうがコンパクト
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
nextPosition.x = Mathf.Clamp(nextPosition.x - 1, 0, 10);
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
nextPosition.x = Mathf.Clamp(nextPosition.x + 1, 0, 10);
}
if (Input.GetKeyDown(KeyCode.UpArrow))
{
nextPosition.z = Mathf.Clamp(nextPosition.z + 1, 0, 10);
}
if (Input.GetKeyDown(KeyCode.DownArrow))
{
nextPosition.z = Mathf.Clamp(nextPosition.z - 1, 0, 10);
}
transform.LookAt(nextPosition);
bool hit = false;//障害物があるかどうかのフラグ
foreach (Vector3 v in wallPoses)
{
if (v == nextPosition)
{
hit = true;
break;
}
}
if (!hit)
{
transform.position = nextPosition;
}
}
}