UNITY3D学习笔记9



using UnityEngine;
using System.Collections;

public class TestB1 : MonoBehaviour {

	public int items;
	private Npc npc = null;

	// Use this for initialization
	void Start () {
		items = PlayerPrefs.GetInt("itemCount",0);
		npc = GameObject.Find("NPC").GetComponent<Npc>();
	}
	
	// Update is called once per frame
	void Update () {
		if(npc.gameState == Npc.TASK_STATE_GAME){
			if(Input.GetMouseButtonDown(0)){
				Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
				RaycastHit hit;

				if(Physics.Raycast(ray,out hit)){
					GameObject obj = hit.collider.gameObject;
					if(obj.name == "Cube"){
						items++;
						if(items>=10){
							npc.gameState = Npc.TASK_STATE_COMPLETE;
						}
						Destroy(obj);
						PlayerPrefs.SetInt("itemCount",items);
						PlayerPrefs.SetInt("gameState",npc.gameState);
					}
				}
			}
		}
	}
}



using UnityEngine;
using System.Collections;

public class Npc : MonoBehaviour {

	public const int TASK_STATE_DEFAULT = 0;
	public const int TASK_STATE_OVER = 1;
	public const int TASK_STATE_OPEN = 2;
	public const int TASK_STATE_GAME = 3;
	public const int TASK_STATE_COMPLETE = 4;
	public const int TASK_STATE_FINSH = 5;

	public int gameState = 0;

	TestB1 script = null;

	Rect windowRect = new Rect(100,0,200,100);
	// Use this for initialization
	void Start () {
		script = GameObject.Find("Main Camera").GetComponent<TestB1>();
		gameState = PlayerPrefs.GetInt("gameState",TASK_STATE_DEFAULT);
	}

	void OnMouseDown(){
		if(gameState == TASK_STATE_OVER){
			gameState = TASK_STATE_OPEN;
		}else if(gameState == TASK_STATE_COMPLETE){
			gameState = TASK_STATE_FINSH;
		}
	}

	void OnMouseOver(){
		if(gameState == TASK_STATE_DEFAULT){
			gameState = TASK_STATE_OVER;
		}
	}
	
	// Update is called once per frame
	void Update () { }

	void OnGUI(){
		switch(gameState){
		case TASK_STATE_OVER:
			GUILayout.Box("click NPC ");
			break;
		case TASK_STATE_OPEN:
			windowRect = GUILayout.Window(0,windowRect,AddWindow,"Accept Mission");
			break;
		case TASK_STATE_GAME:
			GUILayout.Box("current ruby number:"+script.items);
			break;
		case TASK_STATE_COMPLETE:
			GUILayout.Box("enought");
			break;
		case TASK_STATE_FINSH:
			GUILayout.Box("mission complete");
			if(GUILayout.Button("restart")){
				gameState = TASK_STATE_DEFAULT;
				script.items = 0;
				PlayerPrefs.SetInt("itemCount",script.items);
				PlayerPrefs.SetInt("gameState",gameState);
			}
			break;
		}
	}

	void AddWindow(int windowID){
		GUILayout.Label("find 20 ruby");
		GUILayout.BeginHorizontal();
		if(GUILayout.Button("accpt mission")){
			gameState = TASK_STATE_GAME;
		}
		if(GUILayout.Button("no mission")){
			gameState = TASK_STATE_DEFAULT;
		}
		GUILayout.EndHorizontal();
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值