|
你可以复制+粘贴这些代码到你的Unity中来看看效果- using UnityEngine;
- using System.Collections;
- using System .Diagnostics;
- using System ;
- public class WS : MonoBehaviour
- {
- private static int chances;
- private static int usernum;//用户输入
- private static int number;
- private static bool isgussed=false;
- private static string status;
- void Start ()
- {
- ReStar ();
- }
- void Update ()
- {
-
- }
- void OnGUI()
- {
- GUI.Label (new Rect (100,100,500,100),status);
- try
- {
- usernum =int.Parse(GUI.TextField (new Rect (100,200,50,20),Convert .ToString (usernum),5));
- }
- catch
- {
- usernum =0;
- usernum =int.Parse(GUI.TextField (new Rect (100,200,50,20),Convert .ToString (usernum),5));
- }
- if (GUI.Button (new Rect(200,300,100,30),"重置" ))
- {
- ReStar ();
- }
- if(GUI .Button (new Rect (500,300,100,30),"猜数"))
- {
- GuessNum (usernum);
- }
- }
- private static void GuessNum(int guessnum)
- {
- try
- {
- if (isgussed ==false)
- {
- if (guessnum <number)
- {
- status ="猜小了,你还剩"+Convert .ToString (chances-1)+"次机会";
- chances -=1;
- }
- else if (guessnum >number )
- {
- status ="猜大了,你还剩"+Convert .ToString (chances-1)+"次机会";
- chances -=1;
- }
- else if(guessnum ==number )
- {
- status ="恭喜你猜中了!!!";
- isgussed =true ;
- }
- }
- else
- {
- status ="你已经猜过了!";
- }
- if (chances ==0)
- {
- status ="机会已用完………………正确答案"+Convert.ToString(number);
- isgussed =true ;
- }
- }
- catch
- {
- status ="乱来的退出游戏!!!";
- isgussed =true ;
- }
- }
-
- private static void ReStar()
- {
- System.Random r=new System.Random ();
- chances = 6;
- isgussed =false ;
- number = r.Next (1,101);
- status="开始了哦!";
- }
- }
复制代码 与普通的C#有很多不一样的地方…………………………
|
|