コード例 #1
0
    private void Update()
    {
        //Checks whether the mouse left button is pressed
        if (Input.GetMouseButtonDown(0))
        {
            youHaveCollectedGameObject.SetActive(false);

            if (EventSystem.current.currentSelectedGameObject.name == "UselessButton")
            {
                itemCanvas.alpha = 0f;
                isItemHolderOpen = false;
                giftCanvas.alpha = 0f;
                isGiftHolderOpen = false;
            }

            print("Mouse clicked!");

            RaycastHit hit;
            Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            //Checks whether the mouse click hits a mesh collider
            if (Physics.Raycast(ray, out hit, 100.0f))
            {
                if (hit.transform != null)
                {
                    GameObject touchedObj = hit.transform.gameObject;

                    print(touchedObj.name);

                    fadeOutAnim = (Animator)touchedObj.GetComponent(typeof(Animator));
                    if (fadeOutAnim)
                    {
                        BinaryFormatter bf = new BinaryFormatter();

                        //scoreDataFile = File.Open(Application.persistentDataPath + "/scoreData.dat", FileMode.Open);
                        //scoreData = (ScoreData)bf.Deserialize(scoreDataFile);

                        Debug.Log("Touched bronzeCoinCount: " + scoreData.bronzeCoinCount);
                        Debug.Log("Touched silverCoinCount: " + scoreData.silverCoinCount);
                        Debug.Log("Touched goldCoinCount: " + scoreData.goldCoinCount);

                        scoreDataFile.Close();

                        if (scoreData.Contains(touchedObj.name, touchedObj.tag))
                        {
                            print("Item already collected!");
                            //fadeOutAnim.enabled = false;
                            scoreDataFile.Close();
                        }
                        else
                        {
                            scoreDataFile = File.Open(Application.persistentDataPath + "/scoreData.dat", FileMode.Open);
                            print("Item available now");

                            scoreData.CollectCoin(touchedObj.name, touchedObj.tag);

                            UpdateItemsHolderPanel(touchedObj.tag);

                            bf.Serialize(scoreDataFile, scoreData);

                            //fadeOutAnim.enabled = true;

                            fadeOutAnim.SetTrigger("fadeOutTrigger");

                            scoreDataFile.Close();

                            youHaveCollectedGameObject.SetActive(true);

                            if (touchedObj.tag == "goldCoin")
                            {
                                youHaveCollectedText.text = "You have collected a gold coin";
                            }
                            else if (touchedObj.tag == "silverCoin")
                            {
                                youHaveCollectedText.text = "You have collected a silver coin";
                            }
                            else if (touchedObj.tag == "bronzeCoin")
                            {
                                youHaveCollectedText.text = "You have collected a bronze coin";
                            }
                        }

                        Debug.Log("Touched 2 bronzeCoinCount: " + scoreData.bronzeCoinCount);
                        Debug.Log("Touched 2 silverCoinCount: " + scoreData.silverCoinCount);
                        Debug.Log("Touched 2 goldCoinCount: " + scoreData.goldCoinCount);

                        scoreDataFile = File.Open(Application.persistentDataPath + "/scoreData.dat", FileMode.Open);
                        scoreData     = (ScoreData)bf.Deserialize(scoreDataFile);

                        scoreDataFile.Close();
                    }
                    else
                    {
                        print("No animator defined for this object!");
                    }
                }
            }
        }

        if (Input.GetKey(KeyCode.Escape))
        {
            SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex - 1);
            Debug.Log(Application.persistentDataPath);
        }
    }