當我們需要在Unity客戶端做一個限制功能,比如按鈕 (最好是發送驗證碼按鈕)要求每天只能點擊三次,等到第二天又有三次機會,這個過程不涉及到服務端消息,只涉及到本地存儲,以下是我的解決方案:
專業從事企業網站建設和網站設計服務,包括網站建設、國際域名空間、網頁空間、企業郵箱、微信公眾號開發、微信支付寶成都微信小程序、手機APP定制開發、軟件開發、等服務。公司始終通過不懈的努力和以更高的目標來要求自己,在不斷完善自身管理模式和提高技術研發能力的同時,大力倡導推行新經濟品牌戰略,促進互聯網事業的發展。
直接上代碼:
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; //本地存儲手機號 private string set_phonenum; //同一個手機號碼使用次數 private int useNum=1; FileInfo file; private Button btn_GetMsgCode; /**倒計時 */ private Text txt_CountDownTimer; } private void Awake() { //獲取驗證碼按鈕 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(); } /** * 獲取驗證碼 */ private void OnGetMsgCodeClick() { if (!mLoginUIPanel.CheckGetMsgCode(input_Account.text)) { Debug.Log("沒有輸入"); 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 "獲取驗證碼"; }); //.AddTo(this); } } private bool Limit() { if (PlayerPrefs.HasKey(set_phonenum)) { mLoginUIPanel.ShowToast("號碼已經被注冊過!"); return true; } PlayerPrefs.SetString(set_phonenum, set_phonenum); //Application.streamingAssetsPath //獲取當前時間天數 DateTime now = DateTime.Now; DateTimeOffset nowtimesss = DateTimeOffset.Now; string nowtime = now.Day.ToString(); string filenames = Application.dataPath+"/num.txt"; //寫入當前次數 //第一次創建文件 FileInfo file = new FileInfo(filenames); if (!file.Exists) { file.CreateText().Close(); file.CreationTimeUtc = nowtimesss.UtcDateTime; Debug.Log("第一次創建" + 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")))) { //刷新次數 Debug.Log("刷新"); FileStream stream = File.Open(filenames, FileMode.OpenOrCreate, FileAccess.Write); stream.Seek(0, SeekOrigin.Begin); stream.SetLength(0); stream.Close(); //重置次數和時間 PlayerPrefs.SetInt("i", 0); PlayerPrefs.SetString("FileTime", nowtime); } //再判斷次數 //如果是當天 if ((Convert.ToInt32(PlayerPrefs.GetString("FileTime")))== Convert.ToInt32(nowtime)) { Debug.Log("執行"); if (PlayerPrefs.GetInt("i") > 2) { Debug.Log("次數已達上限"); mLoginUIPanel.ShowToast("次數已達上限,請明天再來!"); return true; } WriteIntoTxt(useNum, filenames, file); //讀取本地數據 ReadOutTxt(filenames); //排序 Allmytxt.Sort(); //對當前號碼取最大值存入 PlayerPrefs.SetInt("i", Allmytxt[Allmytxt.Count - 1]); useNum++; int a = 3 - Allmytxt[Allmytxt.Count - 1]; mLoginUIPanel.ShowToast("今天還剩下"+ a+ "次注冊機會"); Debug.Log(PlayerPrefs.GetInt("i")); // Debug.Log("當前文件日期" + Convert.ToInt32(PlayerPrefs.GetString("FileTime"))); //先判斷時間 return false; } return false; } /** * reset重置倒計時· */ public void ResetGetMsgCode() { btn_GetMsgCode.interactable = true; txt_CountDownTimer.text = "獲取驗證碼"; } //把所有的數據寫入文本中 public void WriteIntoTxt(int message,string filename,FileInfo file) { // FileInfo file = new FileInfo(filename); //最后一次修改日期并存儲 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]); } //讀取次數 存儲到列表中 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)); } //利用文本的行數來判斷次數 Allmytxt.Add(line); Debug.Log(line); reader.Dispose(); reader.Close(); }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持創新互聯。
當前文章:Unity登錄注冊時限制發送驗證碼次數功能的解決方法
分享URL:http://www.2m8n56k.cn/article40/gpocho.html
成都網站建設公司_創新互聯,為您提供外貿網站建設、網站建設、小程序開發、網站設計、網站制作、搜索引擎優化
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:[email protected]。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯