それではお作法の確認を
今回はただの終了報告なのでポエムはなしです。なしだけど終わらせたので早速やっていきたいと思います。
22. クラス
前回どこまで進んでたか分かりませんが、クラスの使い方についてのお話からはじめていきます。
using UnityEngine;
using System.Collections;
public class SingleCharacterScript : MonoBehaviour
{
public class Stuff
{
public int bullets;
public int grenades;
public int rockets;
public Stuff(int bul, int gre, int roc)
{
bullets = bul;
grenades = gre;
rockets = roc;
}
}
public Stuff myStuff = new Stuff(10, 7, 25);
public float speed;
public float turnSpeed;
public Rigidbody bulletPrefab;
public Transform firePosition;
public float bulletSpeed;
void Update ()
{
Movement();
Shoot();
}
void Movement ()
{
float forwardMovement = Input.GetAxis("Vertical") * speed * Time.deltaTime;
float turnMovement = Input.GetAxis("Horizontal") * turnSpeed * Time.deltaTime;
transform.Translate(Vector3.forward * forwardMovement);
transform.Rotate(Vector3.up * turnMovement);
}
void Shoot ()
{
if(Input.GetButtonDown("Fire1") && myStuff.bullets > 0)
{
Rigidbody bulletInstance = Instantiate(bulletPrefab, firePosition.position, firePosition.rotation) as Rigidbody;
bulletInstance.AddForce(firePosition.forward * bulletSpeed);
myStuff.bullets--;
}
}
}
using UnityEngine;
using System.Collections;
public class Inventory : MonoBehaviour
{
public class Stuff
{
public int bullets;
public int grenades;
public int rockets;
public float fuel;
public Stuff(int bul, int gre, int roc)
{
bullets = bul;
grenades = gre;
rockets = roc;
}
public Stuff(int bul, float fu)
{
bullets = bul;
fuel = fu;
}
public Stuff ()
{
bullets = 1;
grenades = 1;
rockets = 1;
}
}
public Stuff myStuff = new Stuff(50, 5, 5);
public Stuff myOtherStuff = new Stuff(50, 1.5f);
void Start()
{
Debug.Log(myStuff.bullets);
}
}
using UnityEngine;
using System.Collections;
public class MovementControls : MonoBehaviour
{
public float speed;
public float turnSpeed;
void Update ()
{
Movement();
}
void Movement ()
{
float forwardMovement = Input.GetAxis("Vertical") * speed * Time.deltaTime;
float turnMovement = Input.GetAxis("Horizontal") * turnSpeed * Time.deltaTime;
transform.Translate(Vector3.forward * forwardMovement);
transform.Rotate(Vector3.up * turnMovement);
}
}
using UnityEngine;
using System.Collections;
public class Shooting : MonoBehaviour
{
public Rigidbody bulletPrefab;
public Transform firePosition;
public float bulletSpeed;
private Inventory inventory;
void Awake ()
{
inventory = GetComponent<Inventory>();
}
void Update ()
{
Shoot();
}
void Shoot ()
{
if(Input.GetButtonDown("Fire1") && inventory.myStuff.bullets > 0)
{
Rigidbody bulletInstance = Instantiate(bulletPrefab, firePosition.position, firePosition.rotation) as Rigidbody;
bulletInstance.AddForce(firePosition.forward * bulletSpeed);
inventory.myStuff.bullets--;
}
}
}
雑にクラス図をつくってみる。多分こんな感じ、つながりがない……。Unity特有のStart()
とかRigidbody
とかそういうのを加えた方がいいのだろうか……。それをやるとごちゃごちゃしてきて何をしたい設計なのか分からなくなる気がする……。まあ、大規模なもの作ったことないからいらぬ心配なんですけどね(笑)。
23. Instantiate
オブジェクトの生成とかにかかわるInstantiate
な話。
using UnityEngine;
using System.Collections;
public class UsingInstantiate : MonoBehaviour
{
public Rigidbody rocketPrefab;
public Transform barrelEnd;
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
Rigidbody rocketInstance;
rocketInstance = Instantiate(rocketPrefab, barrelEnd.position, barrelEnd.rotation) as Rigidbody;
rocketInstance.AddForce(barrelEnd.forward * 5000);
}
}
}
using UnityEngine;
using System.Collections;
public class RocketDestruction : MonoBehaviour
{
void Start()
{
Destroy(gameObject, 1.5f);
}
}
―と、おまけのデストロイ。
24. Array
なんで後半で配列の話なんだろう。
using UnityEngine;
using System.Collections;
public class Arrays : MonoBehaviour
{
public GameObject[] players;
void Start ()
{
players = GameObject.FindGameObjectsWithTag("Player");
for(int i = 0; i < players.Length; i++)
{
Debug.Log("Player Number "+i+" is named "+players[i].name);
}
}
}
複雑なことをやろうとすると事故るやつだけど、この例だと特に話すことは多分ないです。
25. Invoke
僕の中ではJavaScriptのsetTimeout
みたいな認識のやつ。個人的にInvokeって単語はPowerShellの方でよく見かける。あっちはこういう機能なかった気がするけど……。
using UnityEngine;
using System.Collections;
public class InvokeScript : MonoBehaviour
{
public GameObject target;
void Start()
{
Invoke("SpawnObject", 2);
}
void SpawnObject()
{
Instantiate(target, new Vector3(0, 2, 0), Quaternion.identity);
}
}
using UnityEngine;
using System.Collections;
public class InvokeRepeating : MonoBehaviour
{
public GameObject target;
void Start()
{
InvokeRepeating("SpawnObject", 2, 1);
}
void SpawnObject()
{
float x = Random.Range(-2.0f, 2.0f);
float z = Random.Range(-2.0f, 2.0f);
Instantiate(target, new Vector3(x, 2, z), Quaternion.identity);
}
}
とりあえず該当部分にコメントを入れてはみたものの語ることがない。細かい仕様はリファレンス見ておけばいいような気がする。
MonoBehaviour-Invoke - Unity スクリプトリファレンス
MonoBehaviour-InvokeRepeating - Unity スクリプトリファレンス
26. Enumerations
僕の中ではIDEに任意の文字列を紐づけて補間させるためのタグみたいな認識。次に出てくるSwitch文と併用して使用すると良い感じに使えるって思ってる。IDE使わなければ不要な存在なのでとも思ってたりする……、補間しないし。
using UnityEngine;
using System.Collections;
public class EnumScript : MonoBehaviour
{
enum Direction {North, East, South, West};
void Start ()
{
Direction myDirection;
myDirection = Direction.North;
}
Direction ReverseDirection (Direction dir)
{
if(dir == Direction.North)
dir = Direction.South;
else if(dir == Direction.South)
dir = Direction.North;
else if(dir == Direction.East)
dir = Direction.West;
else if(dir == Direction.West)
dir = Direction.East;
return dir;
}
}
27. Switch Statements
なぜか最後はSwitch文。僕の中ではラベル付きif文。
using UnityEngine;
using System.Collections;
public class ConversationScript : MonoBehaviour
{
public int intelligence = 5;
void Greet()
{
switch (intelligence)
{
case 5:
print ("Why hello there good sir! Let me teach you about Trigonometry!");
break;
case 4:
print ("Hello and good day!");
break;
case 3:
print ("Whadya want?");
break;
case 2:
print ("Grog SMASH!");
break;
case 1:
print ("Ulg, glib, Pblblblblb");
break;
default:
print ("Incorrect intelligence level.");
break;
}
}
}
例文のintelligence分岐、文章内容酷すぎるんだけど元ネタあったりするんですかね?
てことで終了です。
今回はポエムはなしのでこれで終了です。おそまつさまでした!