code ソーシャル

Unityアプリcodetips(24)

スポンサーリンク

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

人気の記事

1

皆さん、ついに、エアラインでも、サブスクリプションが始まったのはご存じですか? まだ実験段階ですが、ANAが、定額全国住み放題サービスを提供する「ADDress」と組んで、国内線を4回まで定額利用可能 ...

2

無料でネットショップを開けるアプリとして多くの人に驚きを与えたBASE株式会社が、2019年10月25日東証マザーズに上場しました。2020年2月時点で90万店を超えるショップを抱えるまでに成長してい ...

3

2011年にサービスを開始してから圧倒的な成長率を誇るインテリア通販サイト 【FLYMEe/フライミー】を皆さんご存じでしょうか。 「自分のイメージするインテリア、本当に欲しいインテリアがどこにあるの ...

4

ついに、noteの月間アクティブユーザー数が4400万人(2020年3月時点)に到達しました。 そもそも、「note」とは、クリエイターが、文章やマンガ、写真、音声を投稿することができ、ユーザーはその ...

5

ボードゲームカフェが1日2回転で儲かるという記事をみつけたので興味を持ち、調べてみました。 まずは、需要がどれくらいあるのか、市場のようすからみていきましょう。 世界最大のボードゲーム市場はドイツで、 ...

-code, ソーシャル
-,

Copyright© BUSINESS HUNTER , 2023 All Rights Reserved Powered by AFFINGER5.