首页 分享 《组合变身小宠物游戏》DataRecord(修改更新中)【初学者】

《组合变身小宠物游戏》DataRecord(修改更新中)【初学者】

来源:萌宠菠菠乐园 时间:2024-08-17 02:36

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;
        }

    }
}

相关知识

《组合变身小宠物游戏》DataRecord(修改更新中)【初学者】
进化吧小宠物 《东方仙境》宠物变身记
糖糖宠物派对游戏下载
初学者如何学习宠物美容
神武变身宠物装饰
神武变身卡与宠物装饰合成怎么弄
宠物摄影师初学者选择什么样的镜头比较合适?
糖糖宠物派对下载安装
宠物养殖游戏推荐,养殖宠物的游戏
帮家里的狗狗换个发型,分分钟变身狗中贵族~!

网址: 《组合变身小宠物游戏》DataRecord(修改更新中)【初学者】 https://www.mcbbbk.com/newsview63246.html

所属分类:萌宠日常
上一篇: 为什么我家电脑QQ游戏宠物宝宝网
下一篇: 虚拟牙齿

推荐分享