Skip to content

Instantly share code, notes, and snippets.

@kankikuchi
Created December 21, 2014 06:59
Show Gist options
  • Save kankikuchi/16f29e9bd8b1beb4658b to your computer and use it in GitHub Desktop.
Save kankikuchi/16f29e9bd8b1beb4658b to your computer and use it in GitHub Desktop.
インスペクターからモンスターの能力を設定するやつ【Unity】
using UnityEngine;
using System.Collections;
public class SetMonsterStatus : MonoBehaviour {
//モンスターの種類
public enum MonsterType{
Warrior, Witch, Dragon
}
public MonsterType Type;
//ステータス
public float HP, MP, Power;
//入力値の制限
private void OnValidate()
{
HP = Mathf.Max (1, HP);
MP = Mathf.Max (0, MP);
Power = Mathf.Max (1, Power);
if(Type == MonsterType.Warrior){
MP = 0;
}
else if(Type == MonsterType.Witch){
MP = Mathf.Max (1, MP);
HP = Mathf.Max (MP, HP);
}
else if(Type == MonsterType.Dragon){
HP = Mathf.Max (100, HP);
Power = 9999;
}
}
private void Awake(){
//起動時に各タイプのスクリプトをアタッチ
if(Type == MonsterType.Warrior){
gameObject.AddComponent<Warrior> ();
}
else if(Type == MonsterType.Witch){
gameObject.AddComponent<Witch> ();
}
else if(Type == MonsterType.Dragon){
gameObject.AddComponent<Dragon> ();
}
//設定された各能力値をスクリプトに設定
gameObject.GetComponent<Monster> ().HP = HP;
gameObject.GetComponent<Monster> ().MP = MP;
gameObject.GetComponent<Monster> ().Power = Power;
//確認
Debug.Log (gameObject.GetComponent<Monster> ().ToString () +
" HP : " + gameObject.GetComponent<Monster> ().HP +
" MP : " + gameObject.GetComponent<Monster> ().MP +
" Power : " + gameObject.GetComponent<Monster> ().Power
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment