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();
}
}