当前位置: 代码迷 >> 综合 >> unity给不联网的客户端添加密码,使其只能在一台电脑登录
  详细解决方案

unity给不联网的客户端添加密码,使其只能在一台电脑登录

热度:14   发布时间:2023-09-23 01:40:51.0

 第一组代码:控制游戏运行,

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;public delegate void Del_ChangeButton_Hander();public class ControllerGameRuning : MonoBehaviour {public static event Del_ChangeButton_Hander ChangeButton_Event;public InputField InputField_SequenceCode;          //序列码public Text Text_Error;private bool IsSuccess = false;void Awake(){if (Text_Error != null){Text_Error.gameObject.SetActive(false);}//获取密码InputField_SequenceCode.text = PlayerPrefs.GetString("Str");//判断密码是否正确if (InputField_SequenceCode.text.Length != 0){//有内容,判断序列码是否正确,正确,则直接跳转JudgeSequenceCode();}}/// <summary>/// 点击确定按钮触发的事件/// </summary>public void OnClcik_Confrim_Event(){IsSuccess = false;          //1.存储信息PlayerPrefs.SetString("Str", InputField_SequenceCode.text);PlayerPrefs.Save();//2.将序列码赋值InformationScript.STR_SequenceCode = InputField_SequenceCode.text;//3.判断序列码和序列码数组中的内容是否相同,相同,则跳转场景JudgeSequenceCode();//4.不同,提示联系服务人员if (Text_Error != null&& IsSuccess==false){Text_Error.gameObject.SetActive(true);Text_Error.text = "序列码错误,请重新输入!";}}/// <summary>/// 判断序列码/// </summary>void JudgeSequenceCode(){for(int i = 0; i < InformationScript.STR_SequenceCode_Array.Length; i++){if(InputField_SequenceCode.text== InformationScript.STR_SequenceCode_Array[i]){//序列码相同,跳转场景IsSuccess = true;InformationScript.Bool_ChangeButtonState = false;           //场景跳转后,不显示按钮//修改按钮的状态if (ChangeButton_Event != null){ChangeButton_Event();}SceneManager.LoadScene("Test20190219");}}}}

第二组代码,给不删除物体添加,保证修改

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;public class DontDestroyOnLoadScript : MonoBehaviour {/// <summary>/// 跳转场景时不删除的物体组/// </summary>public GameObject[] GO_DontDestroyObjects;/// <summary>/// 物体是否已经存在/// </summary>private static bool _Bool_Exist = false;/// <summary>/// 后期用于修改的按钮/// </summary>public GameObject GO_ChangeButton;void Awake(){if (GO_ChangeButton != null){GO_ChangeButton.SetActive(InformationScript.Bool_ChangeButtonState);}//第一次进入场景,不删除物体if (!_Bool_Exist){//注册事件ControllerGameRuning.ChangeButton_Event += ChangeButtonState;for (int i = 0; i < GO_DontDestroyObjects.Length; i++){DontDestroyOnLoad(GO_DontDestroyObjects[i]);}_Bool_Exist = true;}else{//第二次以后进入,删除已存在的物体for (int i = 0; i < GO_DontDestroyObjects.Length; i++){Destroy(GO_DontDestroyObjects[i]);}}}void Update(){if (Input.GetKeyDown(KeyCode.Escape)){//修改按钮状态InformationScript.Bool_ChangeButtonState = !InformationScript.Bool_ChangeButtonState;GO_ChangeButton.SetActive(InformationScript.Bool_ChangeButtonState);}}/// <summary>/// 点击修改按钮/// </summary>public void OnClick_ChangeButton_Event(){InformationScript.Bool_ChangeButtonState = false;ChangeButtonState();//将存储的信息更改为空PlayerPrefs.SetString("Str", string.Empty);//跳转场景SceneManager.LoadScene("StartScene");}/// <summary>/// 修改按钮的状态/// </summary>private void ChangeButtonState(){if (GO_ChangeButton != null){GO_ChangeButton.SetActive(InformationScript.Bool_ChangeButtonState);}}
}

第三组代码,记录信息使用:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class InformationScript
{/// <summary>/// 序列码数组/// </summary>public static string[] STR_SequenceCode_Array = { "00001","00002","00003" };/// <summary>/// 序列码/// </summary>public static string STR_SequenceCode = string.Empty;/// <summary>/// 修改按钮的状态/// </summary>public static bool Bool_ChangeButtonState = false;}