LoginSignup
13
15

More than 3 years have passed since last update.

[Unityで学ぶC#] 11 クラスとメソッド

Last updated at Posted at 2017-03-04
1 / 15

クラスとメソッドが複数ある場合の書き方


新しくスクリプト作ってこの状態にしました.

スクリーンショット 2017-03-05 2.36.42.png


注意

ここら辺で起きそうなエラー

  1. クラス名 Monsterが2つある.
    古いほう消してください.

  2. ファイル名とClass名が違う.
    Unityではファイル名と, : MonoBehaviourって右にかいてあるクラスの名前が違うとエラー出ます.


サンプル

まずはデータを増やします.
,(カンマ)を使うことで,引数の数を増やすことができます.

class Monster
{
    public string name;
    public int hp; // HP
    public int attack; // 攻撃力


    public Monster(string name, int hp, int attack){
        this.name = name;
        this.hp = hp;
        this.attack = attack;
    }
}

public class Battle : MonoBehaviour {

    void Start () {
        Monster monster1 = new Monster ("スライム", 10, 3);
    }
}

続いて,Monsterクラスにステータスを表示する関数を追加します.

// ステータスを表示
public void ShowStatus(){
    Debug.Log ("名前: " + name);
    Debug.Log ("HP: " + hp);
    Debug.Log ("攻撃力: " + attack);
}

外部のクラスの関数を呼ぶためには,
インスタンスから.(ドット)で繋げて関数を呼びます.
monster1.ShowStatus ();
ただし,関数がpublicでないとこれはできません.


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

class Monster
{
    public string name;
    public int hp; // HP
    public int attack; // 攻撃力


    public Monster(string name, int hp, int attack){
        this.name = name;
        this.hp = hp;
        this.attack = attack;
    }

    // ステータスを表示
    public void ShowStatus(){
        Debug.Log ("名前: " + name);
        Debug.Log ("HP: " + hp);
        Debug.Log ("攻撃力: " + attack);
    }
}

public class Battle : MonoBehaviour {

    void Start () {
        Monster monster1 = new Monster ("スライム", 10, 3);
        monster1.ShowStatus ();
    }
}


スクリーンショット 2017-03-05 2.47.43.png


応用

ここからは応用です.次の3つを実装します.
1. モンスター1とモンスター2を作る
2. モンスター2をモンスター1が攻撃する
3. モンスター2の残りHPを表示する


モンスターを増やす.

void Start () {
    Monster monster1 = new Monster ("スライム", 10, 3);
    Monster monster2 = new Monster ("ゴブリン", 5, 4);
    monster1.ShowStatus ();
    monster2.ShowStatus ();
}

ダメージを受けるための関数を作る

これをMonsterクラスの中に書きます.

// ダメージを受ける
public void Damaged(int damage){
    hp -= damage;
    Debug.Log(name + "に" + damage + "のダメージ");
    Debug.Log (name + "のHP: " + hp);
}

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

class Monster
{
    public string name;
    public int hp; // HP
    public int attack; // 攻撃力


    public Monster(string name, int hp, int attack){
        this.name = name;
        this.hp = hp;
        this.attack = attack;
    }

    // ステータスを表示
    public void ShowStatus(){
        Debug.Log ("名前: " + name);
        Debug.Log ("HP: " + hp);
        Debug.Log ("攻撃力: " + attack);
    }

    // ダメージを受ける
    public void Damaged(int damage){
        hp -= damage;
        Debug.Log(name + "に" + damage + "のダメージ");
        Debug.Log (name + "のHP: " + hp);
    }
}

public class Battle : MonoBehaviour {

    void Start () {
        Monster monster1 = new Monster ("スライム", 10, 3);
        Monster monster2 = new Monster ("ゴブリン", 5, 4);
        monster1.ShowStatus ();
        monster2.ShowStatus ();
    }
}

オブジェクトを引数にする.

モンスターを引数として使います.

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

class Monster
{
    public string name;
    public int hp; // HP
    public int attack; // 攻撃力


    public Monster(string name, int hp, int attack){
        this.name = name;
        this.hp = hp;
        this.attack = attack;
    }

    // ステータスを表示
    public void ShowStatus(){
        Debug.Log ("名前: " + name);
        Debug.Log ("HP: " + hp);
        Debug.Log ("攻撃力: " + attack);
    }

    // ダメージを受ける
    public void Damaged(int damage){
        hp -= damage;
        Debug.Log(name + "に" + damage + "のダメージ");
        Debug.Log (name + "のHP: " + hp);
    }
}

public class Battle : MonoBehaviour {

    void Start () {
        Monster monster1 = new Monster ("スライム", 10, 3);
        Monster monster2 = new Monster ("ゴブリン", 5, 4);
        monster1.ShowStatus ();
        monster2.ShowStatus ();

        Attack (monster1, monster2);
    }

    // 攻撃するための関数
    void Attack(Monster attacker, Monster defender){
        Debug.Log (attacker.name + "の攻撃");
        defender.Damaged (attacker.attack);
    }
}


おわり

13
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
15