技術めも

Unity2D 個人的メモ

便利リンク

Unity使いそうな関数まとめメモ 1.1

GameObject 基底クラス

2Dゲームを作るときのMonoBehaviourを使いやすくするクラスの設計

変更点

GetPrefab を廃止し、CreateInstance のみでプレハブ生成(プレハブ名の指定)。
SetObject、 Object で 名前での GameObject 取得。

using UnityEngine;
using System.Collections;

/// キャラクター基底クラス.
/// SpriteRendererが必要.
[RequireComponent (typeof(SpriteRenderer))]
public class Token : MonoBehaviour
{
	private static Hashtable _gameHash = new Hashtable();

	// ゲームオブジェクト名での登録.
	public static void SetObject (string objName)
	{
		if (_gameHash.ContainsKey(objName)) {
			return;
		}
		GameObject targetObj = new GameObject();
		targetObj = GameObject.Find (objName);
		_gameHash.Add(objName, targetObj);
	}

	// ゲームオブジェクト名での取得.
	public static GameObject Object (string objName)
	{
		if (!_gameHash.ContainsKey(objName)) {
			return null;
		}
		return (GameObject)(_gameHash[objName]);
	}

	/// インスタンスを生成してスクリプトを返す.
	public static Type CreateInstance<Type> (string name, Vector3 p, float direction = 0.0f, float speed = 0.0f) where Type : Token
	{
		/// プレハブ取得.必ず"Resources/Prefabs/"に配置すること.
		if (!_gameHash.ContainsKey ("Prefabs/" + name)) {
			_gameHash.Add("Prefabs/" + name, (Resources.Load ("Prefabs/" + name) as GameObject));
		}
		GameObject g = Instantiate ((GameObject)(_gameHash["Prefabs/" + name]), p, Quaternion.identity) as GameObject;
		Type obj = g.GetComponent<Type> ();
		obj.SetVelocity (direction, speed);
		return obj;
	}

	public static Type CreateInstance2<Type> (string name, float x, float y, float direction = 0.0f, float speed = 0.0f) where Type : Token
	{
		Vector3 pos = new Vector3 (x, y, 0);
		return CreateInstance<Type> (name, pos, direction, speed);
	}
……

Sprite 自作 基底クラス

using System;
using UnityEngine;
using System.Collections;

public class MonoBase : MonoBehaviour
{
	private Hashtable objHash = new Hashtable();

	protected void setObj (string objName)
	{
		GameObject targetObj = new GameObject();
		targetObj = GameObject.Find (objName);
		objHash.Add(objName, targetObj);
	}

	protected GameObject obj (string objName)
	{
		return (GameObject)(objHash[objName]);
	}

	protected bool isTap ()
	{
		if (! Input.GetMouseButton (0)) return false;

		Vector2 tapPoint = Camera.main.ScreenToWorldPoint (Input.mousePosition);
		Collider2D collition2d = Physics2D.OverlapPoint (tapPoint);
		if (collition2d && collition2d.gameObject == gameObject) return true;

		return false;
	}

	protected bool isUnTap ()
	{
		if (! Input.GetMouseButton (0)) return true;

		return false;
	}

	protected bool hit(Collision2D col, string objName)
	{
		if (col.gameObject.name == objName) return true;

		return false;
	}

	protected String getKey()
	{
		float y = Input.GetAxisRaw ("Vertical");
		float x = Input.GetAxisRaw ("Horizontal");
		if (x > 0) return "R";
		if (x < 0) return "L";
		if (y > 0) return "U";
		if (y < 0) return "D";
		return "";
	}
}
  • 起動時に操作対象 Object を Objact名 で取得
	void Start ()
        {
		setObj("Tank");
		setObj("Hoge");
	}
  • いつでも Object名で操作可能
  obj("Tank").GetComponent<Rigidbody2D>().AddForce (new Vector2 (+300.0f, 500.0f));
  • 名前で当たり判定
	void OnCollisionEnter2D(Collision2D col)
	{
		if (hit(col, "Tank_Shell")) {
			……
		}
	}