76 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			76 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			C#
		
	
	
	
| using System.Collections;
 | |
| using System.Collections.Generic;
 | |
| using UnityEngine;
 | |
| 
 | |
| namespace MMO
 | |
| {
 | |
|     public class MMOStorageBase<T> where T : MMOStorageBase<T>, new()
 | |
|     {
 | |
|         private static T mInstance;
 | |
|         public static T Instance
 | |
|         {
 | |
|             get
 | |
|             {
 | |
|                 if (mInstance == null)
 | |
|                 {
 | |
|                     Load();
 | |
|                 }
 | |
| 
 | |
|                 return mInstance;
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         public long DataVersion
 | |
|         {
 | |
|             get => mDataVersion;
 | |
|             private set => mDataVersion = value;
 | |
|         }
 | |
|         [SerializeField] private long mDataVersion = 0;
 | |
| 
 | |
|         protected virtual void InitFirstDefault()
 | |
|         {
 | |
| 
 | |
|         }
 | |
| 
 | |
|         protected virtual void InitBuildDefault()
 | |
|         {
 | |
| 
 | |
|         }
 | |
| 
 | |
|         public static void Load()
 | |
|         {
 | |
|             if (ES3.FileExists() && ES3.KeyExists(typeof(T).Name))
 | |
|             {
 | |
|                 mInstance = ES3.Load<T>(typeof(T).Name);
 | |
|             }
 | |
|             else
 | |
|             {
 | |
|                 mInstance = new T();
 | |
| 
 | |
|                 string tFirstDefaultKey = "FirstDefault_" + typeof(T).Name;
 | |
| 
 | |
|                 if (PlayerPrefs.GetInt(tFirstDefaultKey, 0) != 1)
 | |
|                 {
 | |
|                     PlayerPrefs.SetInt(tFirstDefaultKey, 1);
 | |
|                     mInstance.InitFirstDefault();
 | |
|                 }
 | |
|             }
 | |
| 
 | |
|             string tBuildDefaultKey = "BuildDefault_" + typeof(T).Name + Application.buildGUID;
 | |
| 
 | |
|             if (PlayerPrefs.GetInt(tBuildDefaultKey, 0) != 1)
 | |
|             {
 | |
|                 PlayerPrefs.SetInt(tBuildDefaultKey, 1);
 | |
|                 mInstance.InitBuildDefault();
 | |
|             }
 | |
| 
 | |
|             Save();
 | |
|         }
 | |
| 
 | |
|         public static void Save()
 | |
|         {
 | |
|             mInstance.DataVersion++;
 | |
|             ES3.Save<T>(typeof(T).Name, mInstance);
 | |
|         }
 | |
|     }
 | |
| } |