扩展cocos creator 中的 localstorage

本文介绍如何扩展Cocos Creator的LocalStorage,解决只能保存字符串的问题,增加了对string、bool和number的强类型支持。通过设置默认值和缓存管理,使得在typescript开发中更易使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

问题:cc中的localstorage仅能保存字符,用typescript开发时不便于各场景的应用

扩展内容如下:

1.支付默认值

2.增加string bool number强类型

3.当set时,写入localstorage,当get时,取缓存里里的数据


export class LocalStorageValue {

    //声音音量
    private static _soundVolume: number = null;
    public static get SoundVolume() {
        if (this._soundVolume == null)
            this._soundVolume = LocalStorage.GetNumber('SoundVolume', 1)
        return this._soundVolume;
    }
    public static set SoundVolume(value) {
        this._soundVolume = value
        LocalStorage.SetNumber('SoundVolume', this._soundVolume)
    }

    //音乐音量
    private static _musicVolume: number = null;
    public static get MusicVolume() {
        if (this._musicVolume == null)
            this._musicVolume = LocalStorage.GetNumber('MusicVolume', 1)
        return this._musicVolume;
    }
    public static set MusicVolume(value) {
        this._musicVolume = value
        LocalStorage.SetNumber('MusicVolume', this._musicVolume)
    }

  

    //手机登录账号
    private static _phoneLoginAccount: string = null;
    public static get PhoneLoginAccount() {
        if (this._phoneLoginAccount == null)
            this._phoneLoginAccount = LocalStorage.GetString('PhoneLoginAccount', '')
        return this._phoneLoginAccount;
    }
    public static set PhoneLoginAccount(value) {
        this._phoneLoginAccount = value
        LocalStorage.SetString('PhoneLoginAccount', this._phoneLoginAccount)
    }

    //手机登录密码
    private static _phoneLoginPassword: string = null;
    public static get PhoneLoginPassword() {
        if (this._phoneLoginPassword == null)
            this._phoneLoginPassword = LocalStorage.GetString('PhoneLoginPassword', '')
        return this._phoneLoginPassword;
    }
    public static set PhoneLoginPassword(value) {
        this._phoneLoginPassword = value
        LocalStorage.SetString('PhoneLoginPassword', this._phoneLoginPassword)
    }

    //游客登录账号
    private static _guestLoginAccount: string = null;
    public static get GuestLoginAccount() {
        if (this._guestLoginAccount == null)
            this._guestLoginAccount = LocalStorage.GetString('GuestLoginAccount', 'guest' + RandomNumber(1, 10000))
        return this._guestLoginAccount;
    }
    public static set GuestLoginAccount(value) {
        this._guestLoginAccount = value
        LocalStorage.SetString('GuestLoginAccount', this._guestLoginAccount)
    }

    //是否自动登录
    private static _IsAutoLogin: boolean = null;
    public static get IsAutoLogin() {
        if (this._IsAutoLogin == null)
            this._IsAutoLogin = LocalStorage.GetBool('IsAutoLogin', false)
        return this._IsAutoLogin;
    }
    public static set IsAutoLogin(value) {
        this._IsAutoLogin = value
        LocalStorage.SetBool('IsAutoLogin', this._IsAutoLogin)
    }
}


class LocalStorage {

    public static SetString(_key: string, _value: string) {
        cc.sys.localStorage.setItem(_key, _value)
    }

    public static GetString(_key: string, _default?: string) {
        var str = cc.sys.localStorage.getItem(_key)
        if (str != null) {
            return str;
        }
        if (str == null && _default != null) {
            return _default;
        }
        return null;
    }

    public static SetBool(_key: string, _bool: boolean) {
        if (_bool) {
            cc.sys.localStorage.setItem(_key, 'true')
        }
        else {
            cc.sys.localStorage.setItem(_key, 'false')
        }
    }

    public static GetBool(_key: string, _default?: boolean): boolean {
        var str = cc.sys.localStorage.getItem(_key)
        if (str == 'true') {
            return true;
        }
        if (str == 'false') {
            return false;
        }
        if (IsNullOrEmpty(str) && _default != null) {
            return _default;
        }
        if (IsNullOrEmpty(str) && _default == null) {
            Log.Orange(Log.Key.System, 'LocalStorage get bool is null ,defult is null, return false!')
            return false;
        }
    }

    public static SetNumber(_key: string, _value: number) {
        if (_value != null) {
            cc.sys.localStorage.setItem(_key, _value)
        }
    }

    public static GetNumber(_key: string, _default?: number): number {
        var _value = cc.sys.localStorage.getItem(_key)
        if (_value == null && _default != null) {
            Log.Orange(Log.Key.System, 'LocalStorage get Number is null or empty ,return defult false!')
            return _default;
        }
        if (_value == null && _default == null) {
            Log.Orange(Log.Key.System, 'LocalStorage get Number is null ,defult is null, return -1!')
            return -1;
        }
        let v = Number(_value);
        return isNaN(v) ? _default : v;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值