首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Unity中针对文件File的增加修改查找功能实现(包含Android移动端解决方案)小结

Unity中针对文件File的增加修改查找功能实现(包含Android移动端解决方案)小结

作者头像
bering
发布2019-12-03 15:52:48
7980
发布2019-12-03 15:52:48
举报

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/CJB_King/article/details/85840807

Unity中针对文件File的增加修改查找功能实现(包含Android移动端解决方案)小结

前段时间从上面又分配了许多的奇葩需求,可以说是小编工作以来最繁忙的一段时间了,今天趁着周末有时间,总结下工作中遇到的小问题。

首先看下策划那边出需求:注册登录功能----->玩家首次打开App不需要输入账号密码,以游客的身份登录到游戏大厅,当第二次打开APP的时候,首先要出现账号选择的一个页面,如图 ,玩家点击“游客”还是可以直接进入大厅,这个页面会显示你之前在本设备上注册过的账号,如果账号设置的是自动登录,那么点击改账号不需要输入密码直接登录进入大厅,如果设置的是手动登录,那么点击账号会在改账号下面弹出密码输入框,然后点击登录按钮进入游戏大厅。

这个需求听上去还是挺简单的,小编当时也是这么认为的,所以评估时间很短,但是里面有几个小坑,坑的小编晚上加了班不说,心里那叫一个苦啊。

首先检测设备是否是第一次登录,如果是直接进入大厅,反之则停留在账号选择页面点击帐号登录大厅,我的解决思路就是每次登录检测本地是否包含保存的账户文件并且文件中是否含有数据,如果没有则是第一次登录该设备,登录完之后将该游客账号写入文件保存(游客的账号是以设备: LogonVisitors.deviceName = SystemInfo.deviceName + " " + id作为唯一身份标识的),这样在第二次登录时检测文件含有信息,则显示登录账号的页面了;

其次是点击账号直接进入大厅,这个可以肯定的是玩家在注册或者登录的时候点选了自动登录才可以操作的,这时就需要把玩家的密码和账号对应保存到文件中了(密码加密),当点击账号的时候,在文件中取得账号和密码向服务端发送请求就可以登录了

另一个就是在大厅里有一个关于当前用户选择手动登录和自动登录的功能,这个就需要在账号文件中保存一个字段设置是否手动自动登录了,账户信息如图:

详细见代码:

 public void SaveAccountFile(AccountSave account)
        {

#if UNITY_EDITOR
            this.filePath = Application.persistentDataPath + "/AccountDic";
#elif UNITY_ANDROID
           string[] src = new string[1] { "Android" };
            string[] srcs = Application.persistentDataPath.Split(src, StringSplitOptions.None);
             this.filePath =srcs[0]+"//AccountDic";
#endif
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }
            if (IsExistInFile(account)) return;      //如果账户存在,则不保存
            string accountJson = LitJson.JsonMapper.ToJson(account);
            StreamWriter sw = null;
            FileInfo file = new FileInfo(filePath + "//" + GlobalConst.Settings.fileName);
            if (!file.Exists)
            {
                sw = File.CreateText(filePath + "//" + GlobalConst.Settings.fileName);
                
            }
            else
            {
                sw = file.AppendText();
            }
            if (sw != null)
            {
                
                sw.WriteLine(accountJson);
                sw.Close();
                sw.Dispose();
            }
        }
        public bool IsExistInFile(AccountSave account)
        {
#if UNITY_EDITOR
            this.filePath = Application.persistentDataPath + "/AccountDic";
#elif UNITY_ANDROID
           string[] src = new string[1] { "Android" };
            string[] srcs = Application.persistentDataPath.Split(src, StringSplitOptions.None);
             this.filePath =srcs[0]+"//AccountDic";
#endif
            if (File.Exists(this.filePath + "//" + GlobalConst.Settings.fileName))
            {
                string[] contents = File.ReadAllLines(this.filePath + "//" + GlobalConst.Settings.fileName);
                for (int i = 0; i < contents.Length; i++)
                {
                    AccountSave a = new AccountSave();
                    a= LitJson.JsonMapper.ToObject<AccountSave>(contents[i]);
                    if (a.account == account.account) return true;
                }
                return false;
            }
            return false;
        }
        public AccountSave GetAccountInFile(AccountSave account)
        {
#if UNITY_EDITOR
            this.filePath = Application.persistentDataPath + "/AccountDic";
#elif UNITY_ANDROID
           string[] src = new string[1] { "Android" };
            string[] srcs = Application.persistentDataPath.Split(src, StringSplitOptions.None);
             this.filePath =srcs[0]+"//AccountDic";
#endif
            if (File.Exists(this.filePath + "//" + GlobalConst.Settings.fileName))
            {
                string[] contents = File.ReadAllLines(this.filePath + "//" + GlobalConst.Settings.fileName);
                for (int i = 0; i < contents.Length; i++)
                {
                    AccountSave a = new AccountSave();
                    a = LitJson.JsonMapper.ToObject<AccountSave>(contents[i]);
                    if (a.account == account.account) return a;
                }
                return default(AccountSave);
            }
            return default(AccountSave);
        }

        public int  GetAccountCount()
        {
#if UNITY_EDITOR
            this.filePath = Application.persistentDataPath + "/AccountDic";
#elif UNITY_ANDROID
           string[] src = new string[1] { "Android" };
            string[] srcs = Application.persistentDataPath.Split(src, StringSplitOptions.None);
             this.filePath =srcs[0]+"//AccountDic";
#endif
            if (File.Exists(this.filePath + "//" + GlobalConst.Settings.fileName))
            {
                string[] contents = File.ReadAllLines(this.filePath + "//" + GlobalConst.Settings.fileName);
                return contents.Length;
            }
            return 0;
        }
        public bool UpdateAccountInfo(AccountSave account,bool isSavePass=true)
        {
           AccountSave destAccountSave= GetAccountInFile(account);
           if (!destAccountSave.Equals(default(AccountSave)))
            {
                string accountJson = LitJson.JsonMapper.ToJson(destAccountSave);
                string strContent = File.ReadAllText(this.filePath + "/" + GlobalConst.Settings.fileName);
                AccountSave modifyAccount = new AccountSave();
                modifyAccount.account = destAccountSave.account;
                modifyAccount.pwd = destAccountSave.pwd;
                modifyAccount.SavePassword = isSavePass;
                string modifyJson = LitJson.JsonMapper.ToJson(modifyAccount);
                strContent = Regex.Replace(strContent, accountJson, modifyJson);
                File.WriteAllText(this.filePath + "/" + GlobalConst.Settings.fileName, strContent);
                return true;
            }
            return false;
        }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-01-05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Unity中针对文件File的增加修改查找功能实现(包含Android移动端解决方案)小结
相关产品与服务
访问管理
访问管理(Cloud Access Management,CAM)可以帮助您安全、便捷地管理对腾讯云服务和资源的访问。您可以使用CAM创建子用户、用户组和角色,并通过策略控制其访问范围。CAM支持用户和角色SSO能力,您可以根据具体管理场景针对性设置企业内用户和腾讯云的互通能力。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档