예제 #1
0
    public static void Save(LocalSaveType saveType, bool value)
    {
        int intValue = value ? 1 : 0;

        PlayerPrefs.SetInt(saveType.ToString(), intValue);
        PlayerPrefs.Save();
    }
예제 #2
0
    public static bool LoadBool(LocalSaveType saveType)
    {
        int intValue = PlayerPrefs.GetInt(saveType.ToString());

        return(intValue > 0 ? true : false);
    }
예제 #3
0
 public static void Save(LocalSaveType saveType, string value)
 {
     PlayerPrefs.SetString(saveType.ToString(), value);
     PlayerPrefs.Save();
 }
예제 #4
0
 public static string LoadString(LocalSaveType saveType)
 {
     return(PlayerPrefs.GetString(saveType.ToString()));
 }
예제 #5
0
 public static float LoadFloat(LocalSaveType saveType)
 {
     return(PlayerPrefs.GetFloat(saveType.ToString()));
 }
예제 #6
0
 public static void Save(LocalSaveType saveType, float value)
 {
     PlayerPrefs.SetFloat(saveType.ToString(), value);
     PlayerPrefs.Save();
 }
예제 #7
0
 public static int LoadInt(LocalSaveType saveType)
 {
     return(PlayerPrefs.GetInt(saveType.ToString()));
 }
예제 #8
0
    public static T Load <T>(LocalSaveType saveType)
    {
        Type type = typeof(T);
        T    obj  = (T)Activator.CreateInstance(type);

        FieldInfo[] fieldInfos = type.GetFields();

        for (int i = 0; i < fieldInfos.Length; ++i)
        {
            string fieldName = saveType.ToString() + "." + fieldInfos[i].Name;

            try
            {
                switch (fieldInfos[i].FieldType.ToString())
                {
                case "System.Int32":
                    var intValue = PlayerPrefs.GetInt(fieldName);
                    fieldInfos[i].SetValue(obj, intValue);
                    break;

                case "System.Boolean":
                    var  boolValue = PlayerPrefs.GetInt(fieldName);
                    bool boolInt   = boolValue > 0 ? true : false;
                    fieldInfos[i].SetValue(obj, boolInt);
                    break;

                case "System.Single":
                    var floatValue = PlayerPrefs.GetFloat(fieldName);
                    fieldInfos[i].SetValue(obj, floatValue);
                    break;

                case "System.String":
                    var stringValue = PlayerPrefs.GetString(fieldName);
                    fieldInfos[i].SetValue(obj, stringValue);
                    break;

                case "System.Collections.Generic.List`1[System.Int32]":
                    var        listIntStr = PlayerPrefs.GetString(fieldName);
                    List <int> listInt    = GetIntListFromStr(listIntStr);
                    fieldInfos[i].SetValue(obj, listInt);
                    break;

                case "System.Collections.Generic.List`1[System.Single]":
                    var          listFloatStr = PlayerPrefs.GetString(fieldName);
                    List <float> listFloat    = GetFloatListFromStr(listFloatStr);
                    fieldInfos[i].SetValue(obj, listFloat);
                    break;

                case "System.Collections.Generic.List`1[System.String]":
                    var           listStrStr = PlayerPrefs.GetString(fieldName);
                    List <string> listStr    = GetStrListFromStr(listStrStr);
                    fieldInfos[i].SetValue(obj, listStr);
                    break;

                default:
                    Debug.LogError("LocalSave type error type" + type.ToString() +
                                   "name:" + fieldInfos[i].Name +
                                   " ,type:" + fieldInfos[i].FieldType.ToString());
                    break;
                }
            }
            catch (Exception e)
            {
                Debug.Log(e.ToString() + " type:" + fieldInfos[i].FieldType.ToString());
            }
        }
        return(obj);
    }
예제 #9
0
    public static void Save(LocalSaveType saveType, System.Object obj)
    {
        Type type = obj.GetType();

        FieldInfo[] fieldInfos = type.GetFields();

        for (int i = 0; i < fieldInfos.Length; ++i)
        {
            string fieldName = saveType.ToString() + "." + fieldInfos[i].Name;

            try
            {
                switch (fieldInfos[i].FieldType.ToString())
                {
                case "System.Int32":
                    var intValue = (int)fieldInfos[i].GetValue(obj);
                    PlayerPrefs.SetInt(fieldName, intValue);
                    break;

                case "System.Boolean":
                    var boolValue = (bool)fieldInfos[i].GetValue(obj);
                    int boolInt   = boolValue ? 1 : 0;
                    PlayerPrefs.SetInt(fieldName, boolInt);
                    break;

                case "System.Single":
                    var floatValue = (float)fieldInfos[i].GetValue(obj);
                    PlayerPrefs.SetFloat(fieldName, floatValue);
                    break;

                case "System.String":
                    var stringValue = (string)fieldInfos[i].GetValue(obj);
                    PlayerPrefs.SetString(fieldName, stringValue);
                    break;

                case "System.Collections.Generic.List`1[System.Int32]":
                    var listIntValue = fieldInfos[i].GetValue(obj) as List <int>;
                    PlayerPrefs.SetString(fieldName, GetStrFromList(listIntValue));
                    break;

                case "System.Collections.Generic.List`1[System.Single]":
                    var listFloatValue = fieldInfos[i].GetValue(obj) as List <float>;
                    PlayerPrefs.SetString(fieldName, GetStrFromList(listFloatValue));
                    break;

                case "System.Collections.Generic.List`1[System.String]":
                    var listStringValue = fieldInfos[i].GetValue(obj) as List <string>;
                    PlayerPrefs.SetString(fieldName, GetStrFromList(listStringValue));
                    break;

                default:
                    Debug.LogError("LocalSave type error type" + type.ToString() +
                                   "name:" + fieldInfos[i].Name +
                                   " ,type:" + fieldInfos[i].FieldType.ToString());
                    break;
                }
            }
            catch (Exception e)
            {
                Debug.Log(e.ToString() + " type:" + fieldInfos[i].FieldType.ToString());
            }
        }
        PlayerPrefs.Save();
    }