1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Unity放置ゲームmemo

Posted at
C# GameManager.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using TMPro;
using System;

public class GameManager : MonoBehaviour
{
    private const int MaxOrb=10;
    private const int RespanTime=1;
    //const:後から内容を変えられない定数
    
    //オブジェクトの参照

    public GameObject orbPrefab;
    public GameObject leveltext;
    public GameObject canvasGame;
    public GameObject textScore; //ScoreTextのゲームオブジェクトを格納するpublic変数
    //public変数にするとインスペクター上でアッタチして変更可能
    //GameObject型にしておくとゲームオブジェクトやプレハブがアタッチ可能

    //メンバ変数
    private int score=0;
    private int nextScrore=100;
    private int currentOrb=0;
    private int nowlevel=0;
    private int nextexp;
    private int nowexp;
    private int[] exptable =new int[]{5,10,20,30};

    private DateTime lastDateTime;
    /// <summary>
    /// DateTime型の変数は時刻を扱うのに便利な構造体である
    /// </summary>
    void Start()
    {
        //初期オーブ生成
        for(int i=0; i<MaxOrb; i++){
            CreateOrb();
        }
        RefleshText();
    }

    // Update is called once per frame
    void Update()
    {
        if (currentOrb < MaxOrb){
            TimeSpan timeSpan=DateTime.UtcNow - lastDateTime;
            if (timeSpan >=TimeSpan.FromSeconds (RespanTime)){    ///TimeSpan.Fromsconds (RespanTime)でint型をTimeSpan型に変更している
                while (timeSpan >=TimeSpan.FromSeconds (RespanTime)){
                    CreateNewOrb();
                    timeSpan-=TimeSpan.FromSeconds(RespanTime);
                }

            }
        }
    }

    //オーブ生成の関数
    public void CreateOrb(){
        GameObject orb=(GameObject)Instantiate(orbPrefab);
        //↑GamaObject型の変数  ↑Instantiateでプレハブから新しいインスタンスを生成し、ゲームオブジェクト型に変換
        orb.transform.SetParent (canvasGame.transform,false);
        //Setparentは親オブジェクトの設定
        orb.transform.localPosition=new Vector3(
        UnityEngine.Random.Range(-300.0f,300.0f),
        UnityEngine.Random.Range(-140.0f,500.0f),
        0f);
    }

    
    public void GetOrb(){
        score+=1;
        RefleshText();
        currentOrb--;
        nowexp++;
        LevelUp();

    }
    void RefleshText(){
        textScore.GetComponent<TextMeshProUGUI>().text="徳:"+ score+"/"+nextScrore;
    }
    public void CreateNewOrb(){
            lastDateTime=DateTime.UtcNow;
            ///UtcNow:その時点での時刻を表す
            if (currentOrb >= MaxOrb){
                return;
            }
            CreateOrb();
            currentOrb++;
        }
    
    public void LevelUp(){
        nextexp=exptable[nowlevel];
        if (nowexp  < nextexp){
            return;
        }
        nowlevel++;
        nowexp=nowexp-nextexp;
        nextexp=exptable[nowlevel];
        leveltext.GetComponent<TextMeshProUGUI>().text="Lv."+ nowlevel;

    }
}
C# orbManager.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using TMPro;

public class orbmanager : MonoBehaviour
{
    // Start is called before the first frame update
    private GameObject gameManager;
    void Start()
    {
        gameManager=GameObject.Find("GameManager");
    }

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

    public void TouchOrb(){
        if(Input.GetMouseButton (0) ==false){
            return;
        }
        //マウスボタンが押されていなかったらreturnで関数から脱出する
        gameManager.GetComponent<GameManager> ().GetOrb();
        //GameManagerコンポーネントはここではアタッチしたスクリプトで、その中のGetOrbメゾットを呼び出している。
        Destroy(this.gameObject);
    }
}
1
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?