StorageMgr.ts 819 B

123456789101112131415161718192021222324252627282930313233
  1. const { ccclass, property } = cc._decorator;
  2. @ccclass
  3. export default class StorageMgr {
  4. //单例
  5. private static _instance: StorageMgr = null
  6. public static get Inst() {
  7. if (this._instance == null) {
  8. this._instance = new StorageMgr()
  9. }
  10. return this._instance;
  11. }
  12. /**获取存储
  13. * @param:key key值
  14. */
  15. getStorage(key: string): any {
  16. if (cc.sys.localStorage.getItem(key)) {
  17. return JSON.parse(cc.sys.localStorage.getItem(key));
  18. }
  19. else {
  20. return null;
  21. }
  22. }
  23. /**存储
  24. * @param: key key值
  25. * @param: value 要存储的value值
  26. */
  27. setStorage(key: string, value: any) {
  28. cc.sys.localStorage.setItem(key, JSON.stringify(value));
  29. }
  30. }