code ソーシャル

Unityアプリcodetips(31)

スポンサーリンク

//tips

//canvas下のimageの位置移動

canvas下にあるimageなどを動かすためにはrect transformの値を変更するためにtransform.localPositionを操作する必要がある。

そのためImage型の変数を宣言、変数にimageのアクセスを与え、transform.localPosition でcanvas下の座標を与える。

Image clickImage;

void Start(){
clickImage = GetComponent<Image>();
transform.localPosition = new Vector3(-300,100,0);
}

ボタンを押した場合に反応させる場合はpublic void PushButton(){}を使用する。

下記のようにすればボタンを押した際にtransform.localPositionが実行される。

public void PushButton(){
transform.localPosition = new Vector3(-300,100,0);

}

//配列の親戚であるListの考え方

Listは配列と似ているが、配列の長さの決め方が異なる。

配列は、要素の数を最初に決めなければならないが使用できないが、Listは要素の数を決めなくても、使用することができ、どのタイミングでも新たな要素を追加したりすることができる。

下記のような例で見るとわかりやすい。

<配列>
Transform[] simpleArray = new Transform[3];

Transform[] array = new Transform[]
{
transform1,
transform2,
transform3
};

<List>
List<Transform> list = new List<Transform>();
list.Add(transform1);
list.Add(transform2);
list.Add(transform3);

Transform target = list[1]; // = transform2

list.Count; // = 3

<List>の方は後からどんどん追加しても一塊の配列として認識されることがわかる。

ただ、using System.Collections.Generic;を最初に宣言しておかないと使用できない。

新たにListを作成する際はList<Vector3> 変数 = new List<Vector3>(){};で表される。

List<Vector3> position = new List<Vector3>(){
new Vector3(-300,400,0),
new Vector3(0,400,0),
new Vector3(300,400,0),
};

//public void PushButton(){}とListを使用してクリック回数に応じてクリック結果を変更する

public void PushButton(){}とListを組み合わせて、クリック回数によってクリック結果が変わるようにする。

これは、クリックするたびに加算するものを用意して、その加算するものとListにある配列の番号とを連結させれば良い。

カウントする変数をphaseとおき、int phase;で宣言。
List<Vector3> positionをクリックするたびに順に呼び出すとすると、public void PushButton(){}とListは下記のようになる。

List<Vector3> positionList = new List<Vector3>(){
new Vector3(-300,400,0),
new Vector3(0,400,0),
new Vector3(300,400,0),
};

void Start(){
phase=0;
tarnsform.localPosition = positionList[phase];

}

public void PushButton(){
phase++;
tarnsform.localPosition = positionList[phase];
}

Listの変数の中身は変数名[数字]で抜き出すことができる。
今回は数字の代わりにphaseが入っている。

また、phaseをリストの数以上に増やさないように、Listの要素数を数えるlist.Count;をうまく活用して、Listの変数positionListの要素数でphaseの増加に制限をかける。

public void PushButton(){
phase++;
if(phase>=positionList.Count){
phase = 0;
}
tarnsform.localPosition = positionList[phase];
}

これはListの要素数以上になったら0に戻すということをやっている。

//returnとは

簡単にいうとvoid型では使えず、メソッドの()内の要素を{}の処理で調理し、答えを出し、その答えを送り出す仕組みだといえる。

さらに、それは他の変数に代入されることで効果が出る。

上記のreturnの流れを理解するにはメソッドから理解しなければならないので、段階を踏みながら例をもとに考える。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Return : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Debug.Log("1");
aaa();
Debug.Log("2");
}

void aaa()
{
Debug.Log("aaa");
}
}

これを実行するとコンソールに
1
aaa
2
と表示される。

次に以下を実行すると

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Return : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
aaa(1);

}

void aaa(int b)
{
Debug.Log(b);
}
}
コンソールに1と表示される。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Return : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
int b = 0;
aaa(b);
aaa(b);
aaa(b);
Debug.Log(b);

}

void aaa(int b)
{
++b;
}
}

これはどうだろうか。

これはaaa(b);が3回続いているのでコンソールに3が表示されそうであるが、答えは0となる。

その理由は、bという変数の効果範囲が違うことにある。

Start()の中でのbはb=0のみ影響され、void aaa()の答えは帰る場所がない。

さらにint b=0;を両方の外に出すと範囲領域が最初の一瞬しかないStart()の方にb=0が適用され、下記の場合もコンソールには0と記される。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Return : MonoBehaviour
{
int b = 0;
// Start is called before the first frame update
void Start()
{

aaa(b);
aaa(b);
aaa(b);
Debug.Log(b);

}

void aaa(int b)
{
++b;
}
}

今度は複数の要素で考える。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Return : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{

aaa(1,2,3);

}

void aaa(int a, int b, int c)
{
Debug.Log(a+b+c);
}
}

コンソールに出てくる答えは6。

今度はreturnを使用して同じ答えを導く。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Return : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
int d = aaa(1,2,3);
Debug.Log(d);

}

int aaa(int a, int b, int c)
{
return a+b+c;
}
}

ここで変数dが呼べていることに注意したい。というのも、voidではintという型を持つ変数に代入することができず、メソッドそのままのaaa(1,2,3);と書くしかなかったからだ。

ちなみに、このreturnを記載するとその分で処理が終了したとみなされ、それ以降の処理内容は扱われない。

これまでの有効範囲とreturn代入技の最終確認を下記2つで行う。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Return : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
int b = 0;
aaa(b);
aaa(b);
aaa(b);
Debug.Log(b);

}

int aaa(int b)
{
return ++b;
}
}
これの実行結果はなんだろうか。

答えは0となる。せっかくintで格納できる形で答えを返しても、格納先がないので、void Start内のb=0が優先される。

これを格納できる形で返すと下記になる。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Return : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
int b = 0;
b = aaa(b);
b = aaa(b);
b = aaa(b);
Debug.Log(b);

}

int aaa(int b)
{
return ++b;
}
}

この答えは3であり、理屈はb=0であるbに対して、int型であるaaa()の答えを代入できるため、上から順に流れるプログラムでは

int b = 0;
b = aaa(b); //1
b = aaa(b); //2
b = aaa(b); //3

と増やすことができのである。

人気の記事

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.