Unity登錄注冊(cè)時(shí)限制發(fā)送驗(yàn)證碼次數(shù)功能的解決方法
當(dāng)我們需要在Unity客戶(hù)端做一個(gè)限制功能,比如按鈕 (最好是發(fā)送驗(yàn)證碼按鈕)要求每天只能點(diǎn)擊三次,等到第二天又有三次機(jī)會(huì),這個(gè)過(guò)程不涉及到服務(wù)端消息,只涉及到本地存儲(chǔ),以下是我的解決方案:
直接上代碼:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UniRx; using System; using System.IO; using System.Text; using System.Globalization; public class RegisterPanel : MonoBehaviour { private LoginUIPanel mLoginUIPanel; StreamWriter writer; StreamReader reader; //本地存儲(chǔ)手機(jī)號(hào) private string set_phonenum; //同一個(gè)手機(jī)號(hào)碼使用次數(shù) private int useNum=1; FileInfo file; private Button btn_GetMsgCode; /**倒計(jì)時(shí) */ private Text txt_CountDownTimer; } private void Awake() { //獲取驗(yàn)證碼按鈕 btn_GetMsgCode = input_MsgCode.transform.Find("Btn_GetVerficationCode").GetComponent<Button>(); btn_GetMsgCode.onClick.AddListener(OnGetMsgCodeClick); txt_CountDownTimer = btn_GetMsgCode.transform.Find("Text").GetComponent<Text>(); } private void OnEnable() { ResetGetMsgCode(); } /** * 獲取驗(yàn)證碼 */ private void OnGetMsgCodeClick() { if (!mLoginUIPanel.CheckGetMsgCode(input_Account.text)) { Debug.Log("沒(méi)有輸入"); return; } set_phonenum = input_Account.text.ToString(); if (Limit()) { Debug.Log("返回true"); return; } else { int timer = 60; intervalStream = Observable.Interval(TimeSpan.FromSeconds(1)) .Where(value => { return timer > 1; }) .SubscribeToText(txt_CountDownTimer, value => { btn_GetMsgCode.interactable = false; return (timer--).ToString() + "秒"; }); //.AddTo(this); //綁定生命周期 timeStream = Observable.Timer(TimeSpan.FromSeconds(60)) .SubscribeToText(txt_CountDownTimer, _ => { btn_GetMsgCode.interactable = true; return "獲取驗(yàn)證碼"; }); //.AddTo(this); } } private bool Limit() { if (PlayerPrefs.HasKey(set_phonenum)) { mLoginUIPanel.ShowToast("號(hào)碼已經(jīng)被注冊(cè)過(guò)!"); return true; } PlayerPrefs.SetString(set_phonenum, set_phonenum); //Application.streamingAssetsPath //獲取當(dāng)前時(shí)間天數(shù) DateTime now = DateTime.Now; DateTimeOffset nowtimesss = DateTimeOffset.Now; string nowtime = now.Day.ToString(); string filenames = Application.dataPath+"/num.txt"; //寫(xiě)入當(dāng)前次數(shù) //第一次創(chuàng)建文件 FileInfo file = new FileInfo(filenames); if (!file.Exists) { file.CreateText().Close(); file.CreationTimeUtc = nowtimesss.UtcDateTime; Debug.Log("第一次創(chuàng)建" + file.CreationTimeUtc); string bb = file.CreationTime.ToString(); char[] ss = new char[] {'/'}; string[] nnn = bb.Split(ss); Debug.Log(nnn[1]); PlayerPrefs.SetString("FileTime", nnn[1]); PlayerPrefs.SetInt("i", 0); } if (Convert.ToInt32(nowtime)!=(Convert.ToInt32(PlayerPrefs.GetString("FileTime")))) { //刷新次數(shù) Debug.Log("刷新"); FileStream stream = File.Open(filenames, FileMode.OpenOrCreate, FileAccess.Write); stream.Seek(0, SeekOrigin.Begin); stream.SetLength(0); stream.Close(); //重置次數(shù)和時(shí)間 PlayerPrefs.SetInt("i", 0); PlayerPrefs.SetString("FileTime", nowtime); } //再判斷次數(shù) //如果是當(dāng)天 if ((Convert.ToInt32(PlayerPrefs.GetString("FileTime")))== Convert.ToInt32(nowtime)) { Debug.Log("執(zhí)行"); if (PlayerPrefs.GetInt("i") > 2) { Debug.Log("次數(shù)已達(dá)上限"); mLoginUIPanel.ShowToast("次數(shù)已達(dá)上限,請(qǐng)明天再來(lái)!"); return true; } WriteIntoTxt(useNum, filenames, file); //讀取本地?cái)?shù)據(jù) ReadOutTxt(filenames); //排序 Allmytxt.Sort(); //對(duì)當(dāng)前號(hào)碼取最大值存入 PlayerPrefs.SetInt("i", Allmytxt[Allmytxt.Count - 1]); useNum++; int a = 3 - Allmytxt[Allmytxt.Count - 1]; mLoginUIPanel.ShowToast("今天還剩下"+ a+ "次注冊(cè)機(jī)會(huì)"); Debug.Log(PlayerPrefs.GetInt("i")); // Debug.Log("當(dāng)前文件日期" + Convert.ToInt32(PlayerPrefs.GetString("FileTime"))); //先判斷時(shí)間 return false; } return false; } /** * reset重置倒計(jì)時(shí)· */ public void ResetGetMsgCode() { btn_GetMsgCode.interactable = true; txt_CountDownTimer.text = "獲取驗(yàn)證碼"; } //把所有的數(shù)據(jù)寫(xiě)入文本中 public void WriteIntoTxt(int message,string filename,FileInfo file) { // FileInfo file = new FileInfo(filename); //最后一次修改日期并存儲(chǔ) if (!file.Exists) { writer = file.CreateText(); } else { writer = file.AppendText(); } writer.WriteLine(message); writer.Flush(); writer.Dispose(); writer.Close(); string bb = file.LastAccessTime.ToString(); char[] ss = new char[] { '/' }; string[] nnn = bb.Split(ss); Debug.Log(nnn[1]); PlayerPrefs.SetString("FileTime", nnn[1]); } //讀取次數(shù) 存儲(chǔ)到列表中 public void ReadOutTxt(string filename) { Allmytxt.Clear(); reader = new StreamReader(filename, Encoding.UTF8); string text; int line = 0; while ((text = reader.ReadLine()) != null) { ++line; // Allmytxt.Add(int.Parse(text)); } //利用文本的行數(shù)來(lái)判斷次數(shù) Allmytxt.Add(line); Debug.Log(line); reader.Dispose(); reader.Close(); }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:Unity UI拖拽模型選擇功能
欄 目:C#教程
下一篇:詳解C#中對(duì)于接口的實(shí)現(xiàn)方式(隱式接口和顯式接口)
本文標(biāo)題:Unity登錄注冊(cè)時(shí)限制發(fā)送驗(yàn)證碼次數(shù)功能的解決方法
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/4563.html
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已安裝軟件變化的方法
- 01-10微信開(kāi)放平臺(tái)之網(wǎng)站授權(quán)微信登錄功能
- 01-10C#.NET實(shí)現(xiàn)網(wǎng)頁(yè)自動(dòng)登錄的方法
- 01-10基于C#實(shí)現(xiàn)簡(jiǎn)單離線注冊(cè)碼生成與驗(yàn)證
- 01-10C#實(shí)現(xiàn)的三種模擬自動(dòng)登錄和提交POST信息的方法
- 01-10Unity3d獲取系統(tǒng)時(shí)間
- 01-10Unity3D獲取當(dāng)前鍵盤(pán)按鍵及Unity3D鼠標(biāo)、鍵盤(pán)的基本操作
- 01-10C#實(shí)現(xiàn)簡(jiǎn)單的登錄界面
- 01-10C#有效防止同一賬號(hào)多次登錄(附三種方法)
- 01-10C#操作注冊(cè)表的方法詳解


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過(guò)重寫(xiě)Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-10delphi制作wav文件的方法
- 04-02jquery與jsp,用jquery
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改