Interactable按钮UNITY

时间:2016-05-10 00:00:30

标签: c# unity3d unityscript unity5 unity3d-2dtools

我正试图制作一个简单的条件:

如果我的价值低于物品的价格,则按钮被禁用。

如果我拥有的价值大于或等于物品的价格,则按钮已启用,我可以购买。

但是当我测试时,我遇到了一些问题。

首先,如果我的项目成本低于项目成本,则启用该按钮,并且只有当我点击它时它才会被禁用。

其次,如果我的物品成本低于物品成本且我点击它就会禁用,但如果我有足够的购买物品,则该按钮不会再次启用。

如何一直检查这些变量?如果我有足够的按钮,如果你没有它,则启用按钮。

Bellow my scrip:

    using UnityEngine;
    using System.Collections;
    using UnityEngine.UI;

    public class BuySkin : MonoBehaviour {

        public int price;
        public Button buyBee1;

        void OnEnable ()
        {
            //Register Button Events
            buyBee1.onClick.AddListener (() => buySkin (buyBee1));

        }


        public void buySkin(Button button)
        { 
            if (BeeCoinScore.coin >= price) {
                BeeCoinScore.coin -= price;
                buyBee1.interactable = false;

            }

            if (BeeCoinScore.coin < price) {
                buyBee1.interactable = false;
            }
        }

        void OnDisable ()
        {
            //Un-Register Button Events
            buyBee1.onClick.RemoveAllListeners ();

        }
    }

2 个答案:

答案 0 :(得分:1)

尝试使用一些预制件!

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

public class GameController : MonoBehaviour
{

    public int coins;
    private int spherePrice = 100, cubePrice = 50;

    public GameObject player;
    public GameObject[] availablePrefabs;
    public List<GameObject> mySkins;

    public Button btnSphere, btnCube;
    public Text txtSphere, txtCube;

    void Start ()
    {
        string serializedMySkins = PlayerPrefs.GetString ("skins", "");
        string serializedPlayer = PlayerPrefs.GetString ("player", "");

        // skins desserialization
        if (serializedMySkins == "")
            mySkins = new List<GameObject> ();
        else {
            var a = serializedMySkins.Split (',');
            for (int i = 0; i < a.Length; i++) {
                if (a [i] == "Sphere") {
                    mySkins.Add (availablePrefabs [0]);
                } 

                if (a [i] == "Cube") {
                    mySkins.Add (availablePrefabs [1]);
                } 
            }
        }

        // player desserialization
        if (serializedPlayer != "") {
            if (serializedPlayer == "Sphere") {
                player = availablePrefabs [0];
            } 

            if (serializedPlayer == "Cube") {
                player = availablePrefabs [1];
            } 
        } else {
            player = mySkins [0];
        }

        coins = PlayerPrefs.GetInt ("coins", 0);
        coins = 1000;
    }

    void Update ()
    {
        if (mySkins.Contains (availablePrefabs [0])) { 
            txtSphere.text = "Usar esfera";
        } else {
            btnSphere.interactable = coins >= spherePrice;
        }

        if (mySkins.Contains (availablePrefabs [1])) {
            txtCube.text = "Usar cubo";
        } else {
            btnCube.interactable = coins >= cubePrice;
        }
    }

    public void play ()
    {
        player = (GameObject)Instantiate (player, new Vector2 (0, 0), Quaternion.identity);
    }

    public void verifySkin (GameObject skinPrefab)
    {
        if (mySkins.Contains (skinPrefab)) {
            useSkin (skinPrefab);
        } else if (coins >= priceOf (skinPrefab)) {
            buySkin (skinPrefab, priceOf (skinPrefab));
        }
    }

    public void buySkin (GameObject skinPrefab, int price)
    {
        mySkins.Add (skinPrefab);
        coins -= price;

        string skinsHash = "";
        for (int i = 0; i < mySkins.Count; i++) {
            skinsHash += mySkins [i].name + ",";
        }

        Debug.Log (skinsHash);

        PlayerPrefs.SetInt ("coins", coins);
        PlayerPrefs.SetString ("skins", skinsHash);

        PlayerPrefs.Save ();
    }

    public void useSkin (GameObject skinPrefab)
    {
        player = skinPrefab;
        PlayerPrefs.SetString ("player", player.name);
        PlayerPrefs.Save ();
    }

    private int priceOf (GameObject skinPrefab)
    {
        if (skinPrefab == availablePrefabs [0])
            return spherePrice;
        else if (skinPrefab == availablePrefabs [1])
            return cubePrice;
        else
            return 0;
    }
}

答案 1 :(得分:0)

当对象变为启用并处于活动状态时,将调用OnEnable()。 你需要Update(),因为每次调用它都会检查你的值是否小于或大于item的价格。你也可以尝试这样。

// I think that you are making an buymenu, so you can disable and enable your menu with ui button and check money you have


     using System.Collections;
        using UnityEngine.UI;

        public class BuySkin : MonoBehaviour 
    {

            public int price;
            public static int money;// money you have
            public Button thisbuyBee1;
    public void buychkr()
    {
    if(price>= money)
    {
    thisbuyBee1.interactable = false;
    }
    else
    {
    thisbuyBee1.interactable = true;
    }
    }
    void Update()
    {
    buychkr();
    }
}