纳金网

标题: Unity3D 文档保存 [打印本页]

作者: 王者再临    时间: 2014-8-30 01:58
标题: Unity3D 文档保存
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;

  4. /// <summary>
  5. /// 游戏配置类
  6. /// </summary>
  7. public class GameSetting : MonoBehaviour {
  8.     /// <summary>
  9.     /// 玩家的复活点游戏对象的名称
  10.     /// </summary>
  11.     public const string PLAYER_SPWAN_POINT = "Player Spwan Point";

  12.     void Awake()
  13.     {
  14.         //载入时不删除
  15.         DontDestroyOnLoad(this);
  16.     }

  17.     #region 持久化保存角色的数据
  18.     /// <summary>
  19.     /// 持久化保存角色的数据
  20.     /// </summary>
  21.     public void SaveCharacterData()
  22.     {
  23.         PlayerCharacter playerCharacter = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerCharacter>();
  24.         if (playerCharacter != null)
  25.         {
  26.             PlayerPrefs.SetString("Player Name", playerCharacter.Name);
  27.             //保存基础属性
  28.             for (int i = 0; i < Enum.GetValues(typeof(AttributeName)).Length; i++)
  29.             {
  30.                 PlayerPrefs.SetInt(((AttributeName)i).ToString() + "-Base Value", playerCharacter.GetPrimaryAttribute(i).BaseValue);
  31.                 PlayerPrefs.SetInt(((AttributeName)i).ToString() + "-Exp To Level", playerCharacter.GetPrimaryAttribute(i).ExpToLevel);
  32.             }
  33.             //保存生命属性
  34.             for (int i = 0; i < Enum.GetValues(typeof(VitalName)).Length; i++)
  35.             {
  36.                 PlayerPrefs.SetInt(((VitalName)i).ToString() + "-Base Value", playerCharacter.GetVital(i).BaseValue);
  37.                 PlayerPrefs.SetInt(((VitalName)i).ToString() + "-Exp To Level", playerCharacter.GetVital(i).ExpToLevel);
  38.                 PlayerPrefs.SetInt(((VitalName)i).ToString() + "-Cur Value", playerCharacter.GetVital(i).CurrentValue);
  39.             }
  40.             //保存技能属性
  41.             for (int i = 0; i < Enum.GetValues(typeof(SkillName)).Length; i++)
  42.             {
  43.                 PlayerPrefs.SetInt(((SkillName)i).ToString() + "-Base Value", playerCharacter.Getskill(i).BaseValue);
  44.                 PlayerPrefs.SetInt(((SkillName)i).ToString() + "-Exp To Level", playerCharacter.Getskill(i).ExpToLevel);
  45.             }
  46.         }
  47.         else
  48.             Debug.LogWarning("不好意思,没有找到Player游戏对象");
  49.     }
  50.     #endregion

  51.     #region 加载角色的数据
  52.     /// <summary>
  53.     /// 加载角色的数据
  54.     /// </summary>
  55.     public void LoadCharacterData()
  56.     {
  57.         PlayerCharacter playerCharacter = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerCharacter>();
  58.         //如果找到了该游戏对象
  59.         if (playerCharacter != null)
  60.         {
  61.             //获取角色的名称
  62.             playerCharacter.name = PlayerPrefs.GetString("Player Name", "凌天");
  63.             //获取角色的基础属性
  64.             for (int i = 0; i < System.Enum.GetValues(typeof(AttributeName)).Length; i++)
  65.             {
  66.                 playerCharacter.GetPrimaryAttribute(i).BaseValue = PlayerPrefs.GetInt(((AttributeName)i).ToString() + "-Base Value", 0);
  67.                 playerCharacter.GetPrimaryAttribute(i).ExpToLevel = PlayerPrefs.GetInt(((AttributeName)i).ToString() + "-Exp To Level", 0);
  68.             }
  69.             //获取角色的生命属性
  70.             for (int i = 0; i < System.Enum.GetValues(typeof(VitalName)).Length; i++)
  71.             {
  72.                 playerCharacter.GetVital(i).BaseValue = PlayerPrefs.GetInt(((VitalName)i).ToString() + "-Base Value", 0);
  73.                 playerCharacter.GetVital(i).ExpToLevel = PlayerPrefs.GetInt(((VitalName)i).ToString() + "-Exp To Level", 0);
  74.                 playerCharacter.GetVital(i).Update();
  75.                 playerCharacter.GetVital(i).CurrentValue = PlayerPrefs.GetInt(((VitalName)i).ToString() + "- Cur Value", 1);
  76.             }
  77.             //获取角色的技能属性
  78.             for (int i = 0; i < System.Enum.GetValues(typeof(SkillName)).Length; i++)
  79.             {
  80.                 playerCharacter.Getskill(i).BaseValue = PlayerPrefs.GetInt(((SkillName)i).ToString() + "-Base Value", 0);
  81.                 playerCharacter.Getskill(i).ExpToLevel = PlayerPrefs.GetInt(((SkillName)i).ToString() + "-Exp To Level", 0);
  82.                 playerCharacter.Getskill(i).Update();
  83.             }
  84.         }
  85.         else
  86.             Debug.LogWarning("真的不好意思哎,我没有找到名叫Player的游戏对象哎");
  87.     }
  88.     #endregion

  89. }
复制代码

作者: heise    时间: 2014-8-30 02:15
谢谢分享
作者: hyui    时间: 2014-8-30 03:16
Great stuff !




欢迎光临 纳金网 (http://wwww.narkii.com/club/) Powered by Discuz! X2.5