❤❤Fungus新課程即將發布,快寫問卷拿優惠❤❤

2019年5月20日 星期一

Unity 小遊戲 教學 猜數字


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour
{
    public InputField playerAnswerUI;
    public int playerAnswer;
    public int correctAnswer;
    public Text hintMessage;
    public Button restartButton;

    void Start()
    {        
        NewQuestion();
    }

    void UpdateHintMessage(string message) {
        hintMessage.text = message;
    }

    void NewQuestion() {
        UpdateHintMessage("請輸入0到99之間的數字");
        correctAnswer = Random.Range(0, 100);
        restartButton.gameObject.SetActive(false);
    }

    bool CanGetInputNumber() {
        return int.TryParse(playerAnswerUI.text, out playerAnswer);
    }

    public void CompareNumbers() {
        if (!CanGetInputNumber())
        {
            UpdateHintMessage("只能輸入數字喔");
            return;
        }

        if (playerAnswer==correctAnswer)
        {
            UpdateHintMessage("恭喜你答對了");
            restartButton.gameObject.SetActive(true);
        }

        if (playerAnswer > correctAnswer)
        {
            UpdateHintMessage("答案還要再小一點");
        }

        if (playerAnswer < correctAnswer)
        {
            UpdateHintMessage("答案還要再大一點");
        }
        FocusPlayerAnswerUI();
    }

    public void ClearHintMessage() {
        UpdateHintMessage("");
    }

    public void ReloadScene() {
        Scene scene = SceneManager.GetActiveScene();
        SceneManager.LoadScene(scene.name);
    }

    void FocusPlayerAnswerUI() {
        playerAnswerUI.ActivateInputField();
    }
    void Update()
    {
        
    }
}


2 則留言:

  1. 陳閒時光您好 想請問一下 fungus可以偵測 我的object到一個位置時 再去做其他的行動嗎 謝謝

    回覆刪除
    回覆
    1. http://www.morningfungame.com/2016/05/unityfungus.html
      看一下常見問題的Q15

      刪除

留言給作者加油打氣