《组合变身小宠物游戏》DataRecord(修改更新中)【初学者】
来源:互联网 发布:有线电视和网络机顶盒 编辑:程序博客网 时间:2024/05/13 08:29
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using System;
public class DataRecord : MonoBehaviour {
public static DataRecord self;
private static Hashtable table;
// Use this for initialization
void Awake()
{
self = this;
}
public static void AddPet(PetXmlData data)
{
Hashtable tmp = new Hashtable();
tmp.Add("id", data.id);
tmp.Add("name", data.name);
tmp.Add("level", data.level);
tmp.Add("hp", data.hp);
tmp.Add("attack", data.attack);
tmp.Add("catchcost", data.catchcost);
tmp.Add("worth", data.worth);
tmp.Add("probability", data.probability);
tmp.Add("headImage", data.headImage);
tmp.Add("curHp", data.hp); //当前血量
tmp.Add("isFight", 0); //是否出战
table.Add(table.Count.ToString(), tmp);
Save();
}
public static Hashtable GetPet(int id)
{
Hashtable tmp = table[id.ToString()] as Hashtable;
return tmp;
}
public static void SetFight(int id,int pos, bool Fight = true)
{ //(拥有所有宠物列表中索引id,出战队伍索引位置pos,是否出战Fight)
Hashtable tmp = table[id.ToString()] as Hashtable;
tmp["isFight"] = Fight? 1 : 0;
table[id.ToString()] = tmp;
if (!table.ContainsKey("FightId" + pos))
table.Add("FightId" + pos, id);
else
table["FightId" + pos] = id;
Save();
}
public static Hashtable GetFightData(int pos)//获取当前战斗的宠物
{
if(table.ContainsKey("FightId" + pos))
{
int i = int.Parse(table["FightId" + pos].ToString());
return GetPet(i);
}
else
return null;
}
public static bool GetFight(int id)
{
Hashtable tmp = table[id.ToString()] as Hashtable;
return int.Parse(tmp["isFight"].ToString()) == 1 ;
}
public static void AddPetHp(int id,int add)//当前血量的变化
{
Hashtable tmp = table[id.ToString()] as Hashtable;
int hp = int.Parse(tmp["curHp"].ToString());
hp += add;
tmp["curHp"] = hp;
table[id.ToString()] = tmp;
Save();
}
public static void Load()
{
if (PlayerPrefs.HasKey("data"))
{
string json = Decrypt(PlayerPrefs.GetString("data"));
if (!string.IsNullOrEmpty(json))
{
table = json.hashtableFromJson();
#if UNITY_ANDROID
string identity = SystemInfo.deviceUniqueIdentifier;
if (string.IsNullOrEmpty(identity))
{
identity = "datadata";
}
Debug.Log("device uniqueIdentifier = " + identity);
if (table.ContainsKey("secret key"))
{
if ((string)table["secret key"] != identity)
{
ClearData();
}
}
else
{
table.Add("secret key", identity);
}
#endif
return;
}
PlayerPrefs.DeleteKey("data");
}
table = new Hashtable();
}
public static void Save()
{
string json = table.toJson();
//Debug.Log("save data = " + json );
string encryptJson = Encrypt(json);
PlayerPrefs.SetString("data", encryptJson);
PlayerPrefs.Save();
}
public static void ClearData()
{
table.Clear();
Save();
}
private static string Encrypt(string toE)
{
// Monodevelop9Unity1File8Edit2View
byte[] keyArray = UTF8Encoding.UTF8.GetBytes("Monodevelop9Unity1File8Edit2View");
RijndaelManaged rDel = new RijndaelManaged();
rDel.Key = keyArray;
rDel.Mode = CipherMode.ECB;
rDel.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = rDel.CreateEncryptor();
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toE);
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
private static string Decrypt(string toD)
{
byte[] keyArray = UTF8Encoding.UTF8.GetBytes("Monodevelop9Unity1File8Edit2View");
RijndaelManaged rDel = new RijndaelManaged();
rDel.Key = keyArray;
rDel.Mode = CipherMode.ECB;
rDel.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = rDel.CreateDecryptor();
byte[] toEncryptArray = Convert.FromBase64String(toD);
try
{
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return UTF8Encoding.UTF8.GetString(resultArray);
}
catch (Exception ex)
{
Debug.Log("Decrypt data failed, reset data. " + ex.Message);
return null;
}
}
}
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using System;
public class DataRecord : MonoBehaviour {
public static DataRecord self;
private static Hashtable table;
// Use this for initialization
void Awake()
{
self = this;
}
public static void AddPet(PetXmlData data)
{
Hashtable tmp = new Hashtable();
tmp.Add("id", data.id);
tmp.Add("name", data.name);
tmp.Add("level", data.level);
tmp.Add("hp", data.hp);
tmp.Add("attack", data.attack);
tmp.Add("catchcost", data.catchcost);
tmp.Add("worth", data.worth);
tmp.Add("probability", data.probability);
tmp.Add("headImage", data.headImage);
tmp.Add("curHp", data.hp); //当前血量
tmp.Add("isFight", 0); //是否出战
table.Add(table.Count.ToString(), tmp);
Save();
}
public static Hashtable GetPet(int id)
{
Hashtable tmp = table[id.ToString()] as Hashtable;
return tmp;
}
public static void SetFight(int id,int pos, bool Fight = true)
{ //(拥有所有宠物列表中索引id,出战队伍索引位置pos,是否出战Fight)
Hashtable tmp = table[id.ToString()] as Hashtable;
tmp["isFight"] = Fight? 1 : 0;
table[id.ToString()] = tmp;
if (!table.ContainsKey("FightId" + pos))
table.Add("FightId" + pos, id);
else
table["FightId" + pos] = id;
Save();
}
public static Hashtable GetFightData(int pos)//获取当前战斗的宠物
{
if(table.ContainsKey("FightId" + pos))
{
int i = int.Parse(table["FightId" + pos].ToString());
return GetPet(i);
}
else
return null;
}
public static bool GetFight(int id)
{
Hashtable tmp = table[id.ToString()] as Hashtable;
return int.Parse(tmp["isFight"].ToString()) == 1 ;
}
public static void AddPetHp(int id,int add)//当前血量的变化
{
Hashtable tmp = table[id.ToString()] as Hashtable;
int hp = int.Parse(tmp["curHp"].ToString());
hp += add;
tmp["curHp"] = hp;
table[id.ToString()] = tmp;
Save();
}
public static void Load()
{
if (PlayerPrefs.HasKey("data"))
{
string json = Decrypt(PlayerPrefs.GetString("data"));
if (!string.IsNullOrEmpty(json))
{
table = json.hashtableFromJson();
#if UNITY_ANDROID
string identity = SystemInfo.deviceUniqueIdentifier;
if (string.IsNullOrEmpty(identity))
{
identity = "datadata";
}
Debug.Log("device uniqueIdentifier = " + identity);
if (table.ContainsKey("secret key"))
{
if ((string)table["secret key"] != identity)
{
ClearData();
}
}
else
{
table.Add("secret key", identity);
}
#endif
return;
}
PlayerPrefs.DeleteKey("data");
}
table = new Hashtable();
}
public static void Save()
{
string json = table.toJson();
//Debug.Log("save data = " + json );
string encryptJson = Encrypt(json);
PlayerPrefs.SetString("data", encryptJson);
PlayerPrefs.Save();
}
public static void ClearData()
{
table.Clear();
Save();
}
private static string Encrypt(string toE)
{
// Monodevelop9Unity1File8Edit2View
byte[] keyArray = UTF8Encoding.UTF8.GetBytes("Monodevelop9Unity1File8Edit2View");
RijndaelManaged rDel = new RijndaelManaged();
rDel.Key = keyArray;
rDel.Mode = CipherMode.ECB;
rDel.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = rDel.CreateEncryptor();
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toE);
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
private static string Decrypt(string toD)
{
byte[] keyArray = UTF8Encoding.UTF8.GetBytes("Monodevelop9Unity1File8Edit2View");
RijndaelManaged rDel = new RijndaelManaged();
rDel.Key = keyArray;
rDel.Mode = CipherMode.ECB;
rDel.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = rDel.CreateDecryptor();
byte[] toEncryptArray = Convert.FromBase64String(toD);
try
{
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return UTF8Encoding.UTF8.GetString(resultArray);
}
catch (Exception ex)
{
Debug.Log("Decrypt data failed, reset data. " + ex.Message);
return null;
}
}
}
0 0
- 《组合变身小宠物游戏》DataRecord(修改更新中)【初学者】
- 《组合变身小宠物游戏》UIManager(修改更新中)【初学者】
- 《组合变身小宠物游戏》GameFSM(修改更新中)【初学者】
- 《组合变身小宠物游戏》ConfigManager(修改更新中)【初学者】
- 《组合变身小宠物游戏》PetCatch(修改更新中)【初学者】
- 《组合变身小宠物游戏》PlayerPetItem(修改更新中)【初学者】
- 《组合变身小宠物游戏》MonsterPetItem(修改更新中)【初学者】
- 《组合变身小宠物游戏》PetCombine宠物组合(修改更新中)【初学者】
- 《组合变身小宠物游戏》Fighting战斗UI(修改更新中)【初学者】
- 《组合变身小宠物游戏》TeamEditor出战队伍选择(修改更新中)【初学者】
- 《组合变身小宠物游戏》PetItemMove物品在格子中拖拽效果(修改更新中)【初学者】
- DataRecord
- 多猫宠物游戏
- 免费游戏引擎列表(更新中)
- 组合游戏(博弈)
- 游戏中常用英文术语(动态更新)
- 电子宠物!(简单)
- 宠物
- python3使用cookie
- maven 打包,打包依赖,并只打某个包下的依赖
- java i/o流-----内存流
- Unity Shader Example 16 (水面反射)
- hdoj1596find the safest road(floyd算法)
- 《组合变身小宠物游戏》DataRecord(修改更新中)【初学者】
- JavaScript中call()、apply()和 bind()方法
- C++实现二叉树所有操作 -- 创建递归遍历迭代遍历拷贝清空查找
- lightoj1370——Bi-shoe and Phi-shoe(欧拉函数应用)
- (转)android:fastScrollEnabled和android:drawSelectorOnTop的用法
- Handling Runtime Changes
- ubuntu16.04安装opencv3.1.0问题
- CostFunctionModeling Non-linear Least Squares2
- JavaScript自学笔记之匆匆一眼