2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Unity C# 超初歩1

Last updated at Posted at 2018-05-13

すごーい簡単なUnityC#スクリプト講座。というか自分用の覚書。

やり方

1.左上の隅っこにある「helloworld」とかいう名前をコピって、Unity上でC#スクリプト作る
2.スクリプトをコピペ。あるいは見ながら自分で書いていく。
3.ヒエラルキーでCubeでも作って、さっき作ったスクリプトを貼っ付けるか「Add Component」でくっつける。

ただのHello Worldですな

helloworld.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class helloworld : MonoBehaviour {
 
    // Use this for initialization
    void Start () {
        //単純にコンソールで文字を表示させるだけ
        print("ハローワールド");
    }
 
// Update is called once per frame
    void Update () {        
    }
}

いわゆるハローワールド。コンソール(出力)にちょこっと「ハローワールド」って表示されるっていうだけの簡単なはじめて用スクリプト。

ただのハローワールドをDebug.Logで出力

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

public class helloworld_d : MonoBehaviour {

	// Use this for initialization
	void Start () {
        //単純に今度はコンソールでDebug Logで出力する
        Debug.Log("ハローワールドDebug Log");
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}
}

今度はDebug.Logで出力したってだけ。どっちがいいのかよく分からん。

簡単な計算もできる

keisan.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class keisan : MonoBehaviour {
    int x=3;
    int y=4;
 
    // Use this for initialization
    void Start () {
        int kotae = x + y;
        print ("答えは" + kotae);
 
    }
 
    // Update is called once per frame
    void Update () {
 
    }
}

すごい簡単なif文

ifbun.cs
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ifbun : MonoBehaviour {
     int x=500;
     public int testx = 300;
 
     // Use this for initialization
     void Start () {
         if (x == testx) {
             print ("合ってる");
         } else {
             print ("間違っている");
         }
     }
 
     // Update is called once per frame
     void Update () {
 
     }
 }

すごい簡単なelse if文

ifelse.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class ifelse : MonoBehaviour {
 
    public int fuga = 3;
 
    // Use this for initialization
    void Start () {
        if (fuga == 3) {
            print ("3で合ってるよ");
        } else if (fuga == 4) {
            print ("おしい。それ4だから");
        } else {
            print ("3じゃないよ");
        }
    }
 
    // Update is called once per frame
    void Update () {
 
    }
}

頻出するであろうwhile構文

whilebb.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class whilebb : MonoBehaviour {
    int sum = 0;
    int cont = 1;
    public int tasukazu = 100;
 
    // Use this for initialization
    void Start () {
 
    }
 
    // Update is called once per frame
    void Update () {
        while (cont <= tasukazu) {
            sum += cont;
            cont++;
        }
        print (sum);
    }
}

よく使うであろうfor文

forloop.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class forloop : MonoBehaviour {
    int sum = 0;
 
    // Use this for initialization
    void Start () {
        for (int cont = 1; cont <= 100; cont++) {
            sum += cont;
        }
        print(sum);
 
    }
 
    // Update is called once per frame
    void Update () {
 
    }
}
2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?