Last active
August 29, 2015 13:56
-
-
Save Buravo46/8848507 to your computer and use it in GitHub Desktop.
【Unity】セットしたGUITextureをライフの増減によって表示したり非表示したりするスクリプト。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
/// <summary> | |
/// Lifeを生成し管理するクラス | |
/// </summary> | |
public class LifeManager : MonoBehaviour { | |
/// <summary> | |
/// 現在のLife数 | |
/// </summary> | |
private int currentLife = 3; | |
/// <summary> | |
/// 初期のLife数 | |
/// </summary> | |
private int initLife = 3; | |
/// <summary> | |
/// Lifeのプレハブ | |
/// </summary> | |
public GameObject lifePrefab; | |
/// <summary> | |
/// 格納するLifeオブジェクト配列 | |
/// </summary> | |
private GameObject[] lifesObject; | |
/// <summary> | |
/// Lifeの初期座標 | |
/// </summary> | |
private Vector3 lifePosition = new Vector3(0.5f, 0.5f, 0); | |
/// <summary> | |
/// ターゲットである暗転処理をするオブジェクト | |
/// </summary> | |
public GameObject targetFadeObject; | |
/// <summary> | |
/// 現在のLife数 | |
/// </summary> | |
/// <remarks> | |
/// 現在のLife数を代入して、全てのLifeを無効化し、現在のLife数分有効化する | |
/// </remarks> | |
/// <value>現在のLife数</value> | |
public int Life{ | |
set{ | |
// 現在のLife数を代入 | |
currentLife = value; | |
// 全てのLifeを無効化 | |
for(int i = 0; i < initLife; i++){ | |
lifesObject[i].SetActive(false); | |
} | |
// 現在のLife数分有効化 | |
for(int i = 0; i < currentLife; i++){ | |
lifesObject[i].SetActive(true); | |
} | |
} | |
get{ | |
return currentLife; | |
} | |
} | |
/// <summary> | |
/// 初期化 | |
/// </summary> | |
/// <remarks> | |
/// 初期値分のLifeを生成し、このコンポーネントを付けているオブジェクトの子供にする。 | |
/// </remarks> | |
void Start(){ | |
// ゲームオブジェクトの配列を生成 | |
lifesObject = new GameObject[initLife]; | |
// 初期値分のLife生成 | |
for(int i = 0; i < initLife; i++){ | |
// インスタンス生成 | |
lifesObject[i] = Instantiate(lifePrefab, lifePosition, Quaternion.identity) as GameObject; | |
// 子供にする | |
lifesObject[i].transform.parent = gameObject.transform; | |
// 位置座標と画像の大きさを設定 | |
lifesObject[i].guiTexture.pixelInset = new Rect(-Screen.width/2 + 32*i, Screen.height/2 - 96, 32, 32); | |
} | |
} | |
/// <summary> | |
/// Life減算関数 | |
/// </summary> | |
/// <remarks> | |
/// 引数のdamage分、Lifeを減らす。現在のLifeが0になればゲームオーバー処理を行う。 | |
/// </remarks> | |
/// <param name="damage">現在のLifeを減らす値</param> | |
/// <example> | |
/// 使用例 | |
/// <code> | |
/// GameObject.Find("このスクリプトが付いているオブジェクト名").SendMessage("ApplyDamage", 1); | |
/// </code> | |
/// </example> | |
void ApplyDamage(int damage){ | |
// もしも現在のLifeが0を超えるならば | |
if(currentLife > 0){ | |
// 減算 | |
currentLife -= damage; | |
// 現在のLifeを代入 | |
this.Life = currentLife; | |
} | |
// もしも現在のLifeが0以下ならば | |
if(currentLife <= 0){ | |
// ゲームオーバー処理 | |
Debug.Log("GameOver"); | |
} | |
} | |
/// <summary> | |
/// Life加算関数 | |
/// </summary> | |
/// <remarks> | |
/// 引数のrepair分、Lifeを増やす。 | |
/// </remarks> | |
/// <param name="repair">現在のLifeを増やす値</param> | |
/// <example> | |
/// 使用例 | |
/// <code> | |
/// GameObject.Find("このスクリプトが付いているオブジェクト名").SendMessage("RecoveryLife", 1); | |
/// </code> | |
/// </example> | |
void RecoveryLife(int repair){ | |
// もしも現在のLifeが初期Life未満ならば | |
if(currentLife < initLife){ | |
// 加算 | |
currentLife += repair; | |
// 現在のLifeを代入 | |
this.Life = currentLife; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment