查看: 1077|回复: 3
打印 上一主题 下一主题

[其他] Unity3D 文档保存

[复制链接]

2317

主题

54

听众

2万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
20645
精华
62

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2014-8-30 01:58:00 |只看该作者 |倒序浏览
  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. }
复制代码
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

heise    

4

主题

3

听众

4249

积分

中级设计师

Rank: 5Rank: 5

纳金币
105
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2014-8-30 02:15:26 |只看该作者
谢谢分享
回复

使用道具 举报

hyui    

1

主题

2

听众

6671

积分

高级设计师

Rank: 6Rank: 6

纳金币
2715
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

板凳
发表于 2014-8-30 03:16:29 |只看该作者
Great stuff !
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2024-9-21 00:33 , Processed in 0.126476 second(s), 32 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部