FC2ブログ
    05 «1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.» 07

    ハルシオンシステムの気ままBlog

    株式会社ハルシオンシステムのメンバーが送る、UnityやらJavaやらの技術的話題から、自社開発のアプリの宣伝とかとかのブログです。ほんと気ままにいきたいと思います。更新日は毎週 月 木でっす!

     

    【ハルシオンブログ】みなさんUnityで構造体って使ってますか?私全然使ってません。構造体のベースを紹介します。 

    寒かったり暖かかったり温度差が激しい今日この頃。
    皆様いかがお過ごしでしょうか?坂内っす。

    はい、ポケガ3でございます。
    まだ遊んでないそこのあなた、遊んでくださいね。

    URL(Android):https://play.google.com/store/apps/details?id=com.halcyon.pocketgirl3
    URL(iOS):https://itunes.apple.com/jp/app/id1539553611

    PV:



    みなさんUnityで構造体って使ってますか?

    いくつかの変装をまとめて管理する際につかうやつです。

    [Blog0327.cs]

    using UnityEngine;

    public class Blog0327 : MonoBehaviour
    {
    private void Start() {
    EnemyInfo enemy = new EnemyInfo(1, "モンスター", 100, 10, 8);
    Debug.Log(enemy.name + " " + enemy.hp);
    }

    struct EnemyInfo {
    public int id;
    public string name;
    public int hp;
    public int atk;
    public int def;
    public EnemyInfo(int id, string name, int hp, int atk, int def) {
    this.id = id;
    this.name = name;
    this.hp = hp;
    this.atk = atk;
    this.def = def;
    }
    }
    }



    このコードだとstruct EnemyInfo~ ってところが構造体になります。

    EnemyInfoにてそれぞれ、ID 名前 HP atk defという変数を管理しています。

    Startで構造体にデータを入れて、Debug,Logで出してます。

    これって・・・・クラスでもいいのでは?と思ってしまいますが

    [Blog0327.cs]

    using UnityEngine;

    public class Blog0327 : MonoBehaviour
    {
    private void Start() {
    EnemyInfo enemy = new EnemyInfo(1, "モンスター", 100, 10, 8);
    Debug.Log(enemy.name + " " + enemy.hp);

    EnemyInfo2 enemy2 = new EnemyInfo2(1, "モンスター", 100, 10, 8);
    Debug.Log(enemy2.name + " " + enemy2.hp);
    }

    struct EnemyInfo {
    public int id;
    public string name;
    public int hp;
    public int atk;
    public int def;
    public EnemyInfo(int id, string name, int hp, int atk, int def) {
    this.id = id;
    this.name = name;
    this.hp = hp;
    this.atk = atk;
    this.def = def;
    }
    }

    public class EnemyInfo2 {
    public int id;
    public string name;
    public int hp;
    public int atk;
    public int def;
    public EnemyInfo2(int id, string name, int hp, int atk, int def) {
    this.id = id;
    this.name = name;
    this.hp = hp;
    this.atk = atk;
    this.def = def;
    }
    }
    }



    もちろんクラスでEnemyInfo2をつくってやっても同じ結果になります。

    結局構造体って使わないのでは?

    Microsoftさんがクラスを使うか、構造体を使うかの判断基準を書いてくれています。
    https://learn.microsoft.com/ja-jp/dotnet/standard/design-guidelines/choosing-between-class-and-struct

    なんかいまいちわかりにくいですが、有効期間が短いやつとか、他のオブジェクトに埋め込まれることが多い場合は構造体を使うのがいいらしいです。

    ちなみにUnityのVector3とかintとかって構造体らしいです。

    構造体の簡単な紹介でした。

    では、あでゅ~ノシ

    Category: 開発日記(Unity)

    tb 0 : cm 0   

    コメント

    コメントの投稿

    Secret

    トラックバック

    トラックバックURL
    →http://halcyonsystemblog.jp/tb.php/965-5161a198
    この記事にトラックバックする(FC2ブログユーザー)