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

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

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;

}

猜你喜欢

转载自blog.csdn.net/qq_34444468/article/details/87914358
今日推荐