试图在c#unity中保存并加载一个值,但遇到了麻烦

时间:2018-01-21 17:29:54

标签: c# unity3d unity5

我正试图让我的游戏部分工作,但无法解决这个问题!

如果用户有足够的游戏币可供使用,我可以删除游戏部分。它在一定程度上起作用,一旦使用了助推器,硬币就会减少但是在退出并重新加载游戏时它不会更新正确的值!

例如,助推器可以以50个硬币的成本使用,如果我有200个硬币并且使用4个助推器,则值更新为0并且这有效。但是,如果我重新启动,那么200的值仍然存在!

我有一个使用playerprefs设置的硬币管理器脚本,在每个级别之后为用户提供50个硬币,这与助推器脚本相关联。

助推器课程:

public class Booster : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler {


    Image m_image;
    RectTransform m_rectXform;
    Vector3 m_startPosition;
    Board m_board;
    Tile m_tileTarget;

    public static GameObject ActiveBooster;
    public Text instructionsText;
    public string instructions = "drag over game piece to remove";

    public bool isEnabled = false;
    public bool isDraggable = false;
    public bool isLocked = false;

    public List<CanvasGroup> canvasGroups;
    public UnityEvent boostEvent;
    public int boostTime = 15;

    //coin test
    CoinManager coinManager;

    private void Awake()
    {
        m_image = GetComponent<Image> ();
        m_rectXform = GetComponent<RectTransform> ();
        m_board = Object.FindObjectOfType<Board> ().GetComponent<Board> ();
        coinManager = GameObject.Find ("CoinManager").GetComponent<CoinManager> ();

    }

    void Start () 
    {
        EnableBooster (false);
    }

    public void EnableBooster(bool state)
    {
        isEnabled = state;

        if (state)
        {
            DisableOtherBoosters ();
            Booster.ActiveBooster = gameObject;
        }
        else if(gameObject == Booster.ActiveBooster)
        {
            Booster.ActiveBooster = null;
        }

        m_image.color = (state) ? Color.white : Color.gray;

        if (instructionsText != null)
        {
            instructionsText.gameObject.SetActive (Booster.ActiveBooster != null);

            if (gameObject == Booster.ActiveBooster)
            {
                instructionsText.text = instructions;
            }
        }
    }

    void DisableOtherBoosters()
    {
        Booster[] allBoosters = Object.FindObjectsOfType<Booster> ();

        foreach (Booster b in allBoosters)
        {
            if (b != this)
            {
                b.EnableBooster (false);
            }
        }
    }

    public void ToggleBooster()
    {
            EnableBooster (!isEnabled);
    }


    public void OnBeginDrag (PointerEventData eventData)
    {
        if (isEnabled && isDraggable && !isLocked)
        {
            m_startPosition = gameObject.transform.position;
            EnableCanvasGroups (false);
        }
    }

    public void OnDrag (PointerEventData eventData)
    {
        if (isEnabled && isDraggable && !isLocked && Camera.main != null)
        {
            Vector3 onscreenPosition;
            RectTransformUtility.ScreenPointToWorldPointInRectangle (m_rectXform, eventData.position,
                Camera.main, out onscreenPosition);

            gameObject.transform.position = onscreenPosition;

            RaycastHit2D hit2D = Physics2D.Raycast (onscreenPosition, Vector3.forward, Mathf.Infinity);

            if (hit2D.collider != null)
            {
                m_tileTarget = hit2D.collider.GetComponent<Tile> ();
            }
            else
            {
                m_tileTarget = null;
            }
        }
    }


    public void OnEndDrag (PointerEventData eventData)
    {
        if (isEnabled && isDraggable && !isLocked)
        {
            gameObject.transform.position = m_startPosition;
            EnableCanvasGroups (true);
            if (m_board != null && m_board.isRefilling)
            {
                return;
            }

            if (m_tileTarget != null)
            {
                if (boostEvent != null)
                {
                    boostEvent.Invoke ();   //can do things here like play a sound effect
                }

                EnableBooster (false);

                m_tileTarget = null;
                Booster.ActiveBooster = null;
            }
        }
    }

    void EnableCanvasGroups(bool state)
    {
        if (canvasGroups != null && canvasGroups.Count > 0)
        {
            foreach (CanvasGroup cGroup in canvasGroups)
            {
                if (cGroup != null)
                {
                    cGroup.blocksRaycasts = state;
                }
            }
        }
    }

    public void RemoveOneGamePiece()
    {
        if (m_board != null && m_tileTarget != null)
        {
            if (coinManager.currentCoinCount >= 50)
            {
                m_board.ClearAndRefillBoard (m_tileTarget.xIndex, m_tileTarget.yIndex);
                coinManager.AddCoins (-50);

                coinManager.SetCoinCount();
                coinManager.ShowCoinCount ();
            }
        }
    }

    public void AddTime()
    {
        if (GameManager.Instance != null)
        {
            if (coinManager.currentCoinCount >= 50){
                GameManager.Instance.AddTime (boostTime);
                coinManager.AddCoins (-50);

                coinManager.SetCoinCount();
                coinManager.ShowCoinCount ();
            }
        }
    }

    public void DropColorBomb()   //big bomb
    {
        if (m_board != null && m_tileTarget != null)
        {
            if (coinManager.currentCoinCount >= 50)
            {
                m_board.MakeColorBombBooster (m_tileTarget.xIndex, m_tileTarget.yIndex);
                coinManager.AddCoins (-50);

                coinManager.SetCoinCount();
                coinManager.ShowCoinCount ();
            }
        }
    }
}

CoinManager类:

public class CoinManager : Singleton<CoinManager>{
    // reference to the UI Text element
    public Text coinText;

    // current number of coins earned during gameplay
    public int currentCoinCount = 0;

    // because we don't have a key set, we should create it and initialize its value to 0
    public void InitCoinCount()
    {
        if (!PlayerPrefs.HasKey("TotalCoins"))
        {
            PlayerPrefs.SetInt("TotalCoins",0);
        }
    }

    // returns the number of coins stored in PlayerPrefs
    public int GetCoinCount()
    {
        if (PlayerPrefs.HasKey("TotalCoins"))
        {
            return PlayerPrefs.GetInt("TotalCoins");
        }

        // if we don't have the key set, so return 0
        return 0;
    }

    // sets a number of coins into PlayerPrefs if the current coin count is greater
    public void SetCoinCount()
    {
        if (PlayerPrefs.HasKey("TotalCoins"))
        {
            int oldCoins = PlayerPrefs.GetInt("TotalCoins"); 
            if (currentCoinCount > oldCoins)
            {
                PlayerPrefs.SetInt("TotalCoins", currentCoinCount);
            }
        }
    }

    // display the coin count as a TextUI
    public void ShowCoinCount()
    {

        if (coinText != null)
        {
            coinText.text = currentCoinCount.ToString();
        }
    }

    //new method for oldcoin count
    public void AddCoins(int coins)
    {
        currentCoinCount = currentCoinCount + coins;
    }
}

1 个答案:

答案 0 :(得分:4)

// sets a number of coins into PlayerPrefs if the current coin count is greater

好吧,这个评论基本上告诉你这个问题。如果硬币计数不大于之前保存的硬币数,则不会保存!

改变这个:

// sets a number of coins into PlayerPrefs if the current coin count is greater
public void SetCoinCount()
{
    if (PlayerPrefs.HasKey("TotalCoins"))
    {
        int oldCoins = PlayerPrefs.GetInt("TotalCoins"); 
        if (currentCoinCount > oldCoins)
        {
            PlayerPrefs.SetInt("TotalCoins", currentCoinCount);
        }
    }
}

对此:

// sets a number of coins into PlayerPrefs
public void SetCoinCount()
{
    PlayerPrefs.SetInt("TotalCoins", currentCoinCount);
}