可交互按钮 UNITY

Interactable button UNITY

我想做一个简单的条件:

如果我拥有的价值低于物品的价格,该按钮将被禁用。

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

但是我在测试的时候遇到了一些问题

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

其次,如果我的钱少于商品价格并且我点击它会禁用,但如果我有足够的钱购买商品,该按钮将不会再次启用。

我怎样才能一直检查这些变量?如果我有足够的按钮启用,如果你没有它禁用。

吼我的票据:

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

        }
    }

用一些预制件试试这个!

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

OnEnable() 在对象启用和活动时调用。 你需要 Update() 因为它在每一帧都会被调用,它会检查你的价值是小于还是大于 item.You 的价格也可以这样尝试。

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