LoginSignup
0
0

More than 1 year has passed since last update.

Unity C# 基礎【ゲームプログラミング入門】

Last updated at Posted at 2022-09-21

YouTube

YouTubeでも解説しています。
前編:https://youtu.be/pPU46GzNaO0
後編:https://youtu.be/LYC-cPtW5Yw

はじめに!

Unity Hub と Visual Studio をダウンロード

Unity Hub と Visual Studio をご用意ください。

Unity Hub : https://unity3d.com/get-unity/download
Visual Studio : https://visualstudio.microsoft.com/ja/

Unity Hub から Unity をダウンロードしてください。バージョンは、なんでも大丈夫ですので、適当に最新のバージョンでLTSのものを選んでください。

Unity 立ち上げ

Unity Hub の New Project から、3D(2Dでも可)を選択して、「プロジェクト作成」を押してください。少し(3分くらい)待つと、Unityが立ち上がります。

C# Script のファイル作成

立ち上がったら、Projectのカラムから新しく C# Script のファイル作成してください。
※スクリプトの名前を変更したい場合は、スクリプト作成時に名前を変更してください。

C# Script をオブジェクトに貼り付け

Hierarchy に3Dオブジェクトを適当に作成して、先ほどの C# Script をドラッグ&ドロップで貼り付けておいてください。
Inspector のカラムで、スクリプトと紐づいているか確認できます。

C# Script を Visual Studio で開く

作成した C# Script を ダブルクリックすると、 Visual Studio が立ち上がると思います。
立ち上がらない場合は、Unity画面の設定(Preferences) から、エディタの立ち上がり方の変更をしてください。(Visual Studio でなくても問題ございません)

C# 基礎知識

コメントアウト

///* */ の2パターンあります。

  • //

// より右側の処理が実行されず、コメントを書くことができます。

  • /* */

/* から */ に挟まれた部分全ての処理が実行されず、コメントを書くことができます。

sample.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SampleScript : MonoBehaviour
{
    // ここはコメントアウト部分
    void Start()
    {
        /* 
         * ここがコメントアウト部分
         * ここもコメントアウト部分
         */
    }

    // ここはコメントアウト部分
    void Update()
    {
        
    }
}

デバッグ

Debug.Log()()の中がデバッグとして確認できます。
確認方法は、Unity画面で実行を押すと、プログラムが走り、下記のプログラムだと void start() が実行され、Debug.Log() の中身が Unity画面の console に表示されます。

sample.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SampleScript : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("Hello YouTube!"); // Unity画面のconsoleで確認
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

変数・定数

変数と定数の違いは、constをつけるかつけないか。
変数で定義したものは、代入したものを書き換えることができます(再代入可能)。

sample.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SampleScript : MonoBehaviour
{
    void Start()
    {
        // Debug.Log("Hello YouTube!");

        // 変数
        string firstMessage = "Hello";
        firstMessage = "Hello World"; // 再代入可
        Debug.Log(firstMessage);

        // 定数
        const string secondMessage = "Hello";
        // secondMessage = "Hello World2"; // 再代入不可
        Debug.Log(secondMessage);
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

とりあえず、これだけ知っておけば大丈夫だろうと思った「数字」、「文字列」、「浮動小数点」、「論理値」を取り上げてみました。文字列の足し算と format

Kata.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class KataScript : MonoBehaviour
{
    // 数字
    int MP = 100;
    int HP = 200;
    int AP = 34;

    // 文字
    string monsterName = "なかじょ";
    string heroName = "ピカチュー";

    // 浮動小数点
    float jumpPower = 3.4f;
    float speed = 2.6f;

    // 論理値
    bool attackFlag = true;

    // Start is called before the first frame update
    void Start()
    {
        Debug.Log(string.Format("{0}のHP:{1}", monsterName, HP));
        Debug.Log(string.Format("{0}のMP:{1}", monsterName, MP));

        Debug.Log(monsterName);
        Debug.Log(heroName);

        Debug.Log(jumpPower);
        Debug.Log(speed);

        Debug.Log(attackFlag);
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

アルゴリズム

if文で条件分岐、update関数でマイフレームごとの実行、論理演算子、for文のテストが、下記のプログラムでできます。適当に値を変えてみて、試してみてください。動画内(25分経過後以降)で、全てゼロから解説しています。

AlgorithmScript.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AlgorithmScript : MonoBehaviour
{
    int HP = 500;

    // Start is called before the first frame update
    void Start()
    {
        for (int i = 0; i < HP; i++) // i = i + 1;
        {
            Debug.Log(i);
        }
    }

    // Update is called once per frame
    void Update()
    {
        HP--; // HP = HP - 1;

        /*
         * or -> ||
         * and -> &&
         */
        //
        if (HP >= 400 && HP % 10 == 0)
        {
            Debug.Log("体力が十分にあります");
        }
        else if (HP >= 200 && HP % 10 == 0)
        {
            Debug.Log("体力が減ってきました");
        }
        else if (HP >= 100 && HP % 10 == 0)
        {
            Debug.Log("体力を回復させてください");
        }
        else if (HP / 10 == 0)
        {
            Debug.Log("もうすぐ体力がなくなります");
        }
    }
}

配列・リスト

続きの動画で、配列・リストについても解説していますので、ご参考までに。
YouTube動画

ArrayScript.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ArrayScript : MonoBehaviour
{
    // 配列
    int[] characterHP = new int[10] { 0, 10, 100, 300, 200, -20, 30, 444, 345, 40 };

    // List
    List<int> enemyHP = new List<int>() { 50, 60, 399 };

    // Start is called before the first frame update
    void Start()
    {
        // 配列のfor文
        for (int i = 0; i < characterHP.Length; i++)
        {
            if (characterHP[i] >= 200) {
                Debug.Log(string.Format("キャラクターのHP:{0}", characterHP[i]));
            }
        }

        enemyHP.Remove(399); // 削除
        enemyHP.Add(250); // 追加

        // Listのfor文
        for (int i = 0; i < enemyHP.Count; i++)
        {
            if (enemyHP[i] >= 200)
            {
                Debug.Log(string.Format("相手のHP:{0}", enemyHP[i]));
            }
        }

    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

回転・移動

最後に、回転や移動についても解説していますので、ご参考までに。

Script.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PositionScript : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        transform.Rotate(new Vector3(2, 3, 1));
        transform.position += new Vector3(0.08f, 0.04f, 0.01f);
    }
}

筆者について

2022年中にゲームをリリースしようと頑張っています。
実は、ゲームなんて作ったことないですが、暖かく見守ってくれると嬉しいです!

Twitter

お気軽にフォローしてください

YouTube

チャンネル登録と動画の高評価もよろしくお願いいたします。

0
0
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
0
0