LoginSignup
0
2

More than 5 years have passed since last update.

[備忘録]Unity で始める C# その6 仕掛けの設置

Posted at

public GameObject 変数名;という風にパブリック変数を用意しておくことで、プログラムでメッセージを操作できるようにしておくと便利。
SetActiveメソッドにfalseを引数として渡すと、GOを非アクティブにできる
[復習]

GetComponentメソッド
Componentの派生クラス.GetComponent<コンポーネントの型>()
全体的流れの1例
public class GameManager : MonoBehaviour {

    //定数定義:壁方向  数字では人間にわかりづらいのでわかり易い名前の定数を定義する。
    public const int WALL_FRONT = 1; //前
    public const int WALL_RIGHT = 2; //右
    public const int WALL_BACK  = 3; //後
    public const int WALL_LEFT  = 4; //左

    //定数定義 ボタンカラー
    public const int COLOR_GREEN = 0; //green
    public const int COLOR_RED = 1; //red 
    public const int COLOR_BLUE = 2; //blue
    public const int COLOR_WHITE = 3; //white


    public GameObject panelWalls; //壁全体

    public GameObject buttonHammer; //buttom hammer

    public GameObject imageHammerIcon; //icon hammer

    public GameObject buttonMessage; //ボタン:メッセージ
    public GameObject buttonMessageText; //メッセージテキスト

    public GameObject[] buttomLamp = new GameObject[3]; //buttom safebox 配列変数で管理

    public Sprite[] buttonPictue =new Sprite[4]; //buttom picture

    public Sprite hammmerPicture; //picture of hammmer



    private int wallNo;  //現在の向いている方向

    // Use this for initialization
    void Start () {
        wallNo = WALL_FRONT;  //スタートでは前を向く

    }



        //右ボタンを押した
    public void PushButtonRight(){
        wallNo++; //方向を1つ右に int a= 5; int b = ++a; //aもbも6になる。
            //左の1つ右は前
        if(wallNo>WALL_LEFT){
            wallNo=WALL_FRONT;
            }
        DisplayWall(); //壁画面表示
        }

        //左ボタンを押した
    public void PushLeftButton(){
        wallNo--; //方向を1つ左に int a= 5; int b = ++a; //aもbも6になる。
            //前の1つ左は後ろ
        if(wallNo<WALL_FRONT){
            wallNo=WALL_LEFT;
            }
        DisplayWall(); //壁画面表示
        }


        //向いている方向の壁を表示


    void DisplayWall(){
        switch (wallNo) {
        case WALL_FRONT:
            panelWalls.transform.localPosition = new Vector3 (0.0f, 0.0f, 0.0f);
            break;
        case WALL_RIGHT:
            panelWalls.transform.localPosition = new Vector3 (-1000.0f, 0.0f, 0.0f);
            break;
        case WALL_BACK:
            panelWalls.transform.localPosition = new Vector3 (-2000.0f, 0.0f, 0.0f);
            break;
        case WALL_LEFT:
            panelWalls.transform.localPosition = new Vector3 (-3000.0f, 0.0f, 0.0f);
            break;
        }
    }

     //メッセージを表示
    void DisplayMessage(string mes){
        buttonMessage.SetActive (true);
        buttonMessageText.GetComponent<Text> ().text = mes;
    }

    public void PushButtonMemo(){
        DisplayMessage ("エッフェル塔と書いてある。");
    }

    //メッセージをタップ
    public void PushButtonMessage(){
        buttonMessage.SetActive (false);  //メッセージを消す
    }


    }

書き忘れていて躓いたこと
if文で条件式の=(いこーる)を一個しか書かなくて想定の動作をしなかったこと。
=1個は、変数に代入する場合で、=2個は数学的ないこーるの意味。if文などで使う。

false: if (doseHaveKey = false) {
true: if (doseHaveKey == false) {

・シーンを新しく作ったときは、ビルド設定に登録する必要がある。
ファイルメニューから「BuildSettings」「プロジェクト」ウィンドウの順。
・ボタンにonclick機能をつけるときは、スクリプトを作り、それを空のGOに付与して、ボタンOnclickにGOをつけて選択する。

0
2
2

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
2