CR开发笔记-6用户数据存储

仍然是贴代码

using UnityEngine;
using System.Collections;

public class DateControl : MonoBehaviour {

    public static string Name;
    public static int Num;
    bool JBstart;
    float fullWidth;
    float fullHeight;
    Rect fullScreen;
    //窗口
    Rect NameText;
    Rect GoBtn;
    Rect pointOut;
    bool JBlb;//用来显示提示文字
    // Use this for initialization
    //静态方法
    public static int getNum()
    {
        return PlayerPrefs.GetInt("_Num");
    }
    public static string getName()
    {
        return PlayerPrefs.GetString("_NAME");
    }
    void Start () {
        JBlb = true;
        fullHeight = Screen.height;
        fullWidth = Screen.width;
        JBstart = false;
        NameText = new Rect((fullWidth / 10) * 3, (fullHeight / 10) * 4, (fullWidth / 10) * 4, (fullHeight / 10) * 2);
        GoBtn = new Rect((fullWidth / 10) * 3, (fullHeight / 10) * 6, (fullWidth / 10) * 4, (fullHeight / 10) * 2);
        fullScreen = new Rect(0, 0, Screen.width, Screen.height);
        pointOut = new Rect((fullWidth / 10) * 3, (fullHeight / 10) * 3, (fullWidth / 10) * 4, (fullHeight / 10) * 1);
        if (PlayerPrefs.HasKey("_NAME"))//根据是否有姓名来判断
        {
            //载入数据
            Name = PlayerPrefs.GetString("_NAME");
            Num = PlayerPrefs.GetInt("_Num");
            this.gameObject.AddComponent("Startmenu");
            //
        }
        else
        {
            //初始化数据
            Name = "";
            Num=0;
            JBstart = true;
            PlayerPrefs.SetString("_NAME", Name);
            PlayerPrefs.SetInt("_Num", Num);
        }
    }
    
    // Update is called once per frame
    void Update () {
    }
    void WinStart(int WinID)
    {
        if (JBlb)
        {
            GUI.Label(pointOut, "请输入你的名字");
        }
        else
        {
            GUI.Label(pointOut, "名字不合规范");
        }
        Name = GUI.TextField(NameText, Name,20);
        if(GUI.Button(GoBtn,"确认"))
        {
            if (!(Name.Equals("")))
            {
                PlayerPrefs.SetString("_NAME", Name);
                JBstart = false;
                this.gameObject.AddComponent("Startmenu");
            }
            else
            {
                JBlb = false;
            }
        }
    }
    void OnGUI()
    {
        if (JBstart)
        {
            GUI.Window(0, fullScreen, WinStart, "出行");
        }
    }
}

今天把数据的读取读入弄了一下

其实储存数据的位置在哪里呢

On Mac OS X PlayerPrefs are stored in ~/Library/Preferences folder, in a file named unity.[company name].[product name].plist, where company and product names are the names set up in Project Settings. The same .plist file is used for both Projects run in the Editor and standalone players.

在Mac OS X上PlayerPrefs存储在~/Library/PlayerPrefs文件夹,名为unity.[company name].[product name].plist,这里company和product名是在Project Setting中设置的,相同的plist用于在编辑器中运行的工程和独立模式.

On Windows standalone players, PlayerPrefs are stored in the registry under HKCUSoftware[company name][product name] key, where company and product names are the names set up in Project Settings.

在Windows独立模式下,PlayerPrefs被存储在注册表的 HKCUSoftware[company name][product name]键下,这里company和product名是在Project Setting中设置的.

On Web players, PlayerPrefs are stored in binary files under ~/Library/Preferences/Unity/WebPlayerPrefs on Mac OS X and %APPDATA%UnityWebPlayerPrefs on Windows. There is one preference file per Web player URL and the file size is limited to 1 megabyte. If this limit would be exceeded, SetInt, SetFloat and SetString will not store the value and throw a PlayerPrefsException.

在Web模式,PlayerPrefs存储在Mac OS X的二进制文件 ~/Library/Preferences/Unity/WebPlayerPrefs中和Windows的 %APPDATA%UnityWebPlayerPrefs中,一个游戏存档文件对应一个web播放器URL并且文件大小被限制为1MB。如果超出这个限制,SetInt、SetFloat和SetString将不会存储值并抛出一个PlayerPrefsException。

摘自unity圣典

效果图

下一步是GUIskin和飞机控制

原文地址:https://www.cnblogs.com/zuoguangxing/p/3852540.html