本文介绍了使用 PlayerPrefs (unity)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在用教程制作游戏.不幸的是,他们没有涵盖保存得分功能.感谢另一位用户,我能够弄清楚我需要使用 playerprefs.我在网上看了教程,但没有一个有帮助.如果可以,请帮帮我!

I have recently been creating a game with tutorials. Unfortunately, they didn't cover a save score feature. Thanks to another user, I was able to figure out that I needed to use playerprefs. I watched tutorials online, but none of them were helpful. If you can, please help me!

黄金每秒脚本:

 using UnityEngine;
 using System.Collections;

 public class GoldPerSec : MonoBehaviour {

     public UnityEngine.UI.Text gpsDisplay;
     public Click click;
     public ItemManager[] items;

     void Start () {
         StartCoroutine(AutoTick ());
     }

     void Update () {
         gpsDisplay.text = GetGoldPerSec() + " Money Per Sec";
     }

     public float GetGoldPerSec() {
         float tick = 0;
         foreach (ItemManager item in items) {
             tick += item.count * item.tickValue;
         }
         return tick;   
     }

     public void AutoGoldPerSec() {
         click.gold += GetGoldPerSec() / 10;
     }

     IEnumerator AutoTick() {
         while (true) {
             AutoGoldPerSec();
             yield return new WaitForSeconds(0.10f);
         }
     }
 }

Gold Per Click 脚本:

Gold Per Click script:

 using UnityEngine;
 using System.Collections;

 public class Click : MonoBehaviour {

     public UnityEngine.UI.Text gpc;
     public UnityEngine.UI.Text goldDisplay;
     public float gold = 0.00f;
     public int goldperclick = 1;

     void Update () {
         goldDisplay.text = "" + gold.ToString("F0");
         gpc.text = "Money Per Click: " + goldperclick;
     }

     public void Clicked(){
         gold += goldperclick;
     }

 }

我的想法是在游戏退出时保存游戏,并在您重新加载游戏时立即加载.我是一个完整的初学者,如果有人能告诉我如何做到这一点,请告诉我!谢谢!:D

My idea was for the game to save when the game is quit, and load as soon as you load the game back up. I am a complete beginner, if anyone can tell me how to do this, please tell me! Thanks! :D

推荐答案

请注意,PlayerPrefs 是一种保存数据的简单方法,但也是一种非常不安全的方法.玩家可以轻松操纵他的goldValue",因为它只是作为整数存储在他设备上的某个文件中.PlayerPrefs 通常只用于玩家可以在游戏中以任何方式更改的值,例如音量设置等.

Please note that PlayerPrefs is an easy way to save data but also an very unsafe way. The player can easily manipulate his "goldValue" since it's just stored as an integer in some file on his device. PlayerPrefs should usually just be used for values the player can changed any way within in game, like volume setting etc.

示例代码

  void Save()
 {
     string filename = "/filename.dat";
     BinaryFormatter bf = new BinaryFormatter();
     FileStream file = File.Create(Application.persistentDataPath+filename);
     bf.Serialize(file, goldValue); //Use can easily use e.g. a List if you want to store more date
     file.Close();
 }

  bool Load()
 {
     string filename = "/filename.dat";
     if (File.Exists(Application.persistentDataPath + filename))
     {
         BinaryFormatter bf = new BinaryFormatter();
         FileStream file = File.Open(Application.persistentDataPath + filename, FileMode.Open);
         goldValue=(int) bf.Deserialize(file); 
         file.Close();
         return true;
     }
     return false;
 }

这篇关于使用 PlayerPrefs (unity)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 20:23