using UnityEngine;
public abstract class BasePanel : MonoBehaviour
{
public GameObject Go;
public object O;
string patnName;
public BasePanel(Transform parent = null)
{
Init(parent);
Open(parent);
}
public void Init(Transform parent)
{
patnName = "UI/" + GetType().Name;
Go = Resources.Load<GameObject>(patnName);
Go = GameObject.Instantiate<GameObject>(Go);
if (parent != null)
{
Go.transform.SetParent(parent, false);
}
Go.name = GetType().Name;
Go.SetActive(false);
}
public virtual void Open(object o)
{
O = o;
Go.SetActive(true);
SetPanel(o);
}
public abstract void SetPanel(object o);
public virtual void Close()
{
Go.SetActive(false);
}
}
Resoutces文件夹中加载预制体
/*
* Filename: UIRegistration.cs
* Created Date: 2020年07月01日 09:35:06 星期三
* Author: zhlong
*
* Copyright (c) 2019 Xuzhou LingTe Technology CO.,LTD
*/
using System;
using System.IO;
using System.Linq;
using System.Net.NetworkInformation;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 该类的功能描述
/// 无需写出实现细节
/// <example>
/// 如果使用该类使用比较复杂,给出使用demo代码
/// <code>
/// 此处添加范例代码
/// </code>
/// </example>
/// </summary>
namespace Zhlong.UI {
public class UIRegistration : BasePanel {
private GameObject g;
string loginValue;
string configPath;
InputField inputFielfTxt;
Button btnOK;
private Text txtSN;
/// <summary>
/// 退出按钮
/// </summary>
private Button _Exit;
public class Model//当前界面要传入的数据
{
public string str;
public Model()
{
}
public Model(string _str)
{
str = _str;
}
}
Model model;
public UIRegistration(Transform t=null) : base(t)//数据赋值
{
}
void PanelInit()
{
configPath = Application.dataPath+ "/login.byte";
inputFielfTxt = Go.FindNodeName("inputFielfTxt").GetComponent<InputField>();
btnOK = Go.FindNodeName("BtnOk").GetComponent<Button>();
_Exit = Go.FindNodeName ( "_Exit" ).GetComponent<Button> ();
txtSN = Go.FindNodeName("txtSN").GetComponent<Text>();
btnOK.Add(()=> {
Debug.Log("OK");
if (inputFielfTxt.text != "")
{
if (Encryption(GetMacAddress()) == inputFielfTxt.text)
{
Debug.LogError("注册成功!");
File.WriteAllText(configPath,inputFielfTxt.text);
AddUI._Ins._IsOK = true;
Close ();
return;
}
Debug.Log(GetMacAddress()+"注册失败!");
AddUI._Ins._IsOK = false;
}
});
_Exit.Add ( ( ) =>
{
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#else
Application.Quit();
#endif
} );
txtSN.text = "你的SN码:"+GetMacAddress();
}
/// <summary>
/// 获取PC识别码
/// </summary>
/// <returns></returns>
private static string GetMacAddress()
{
string physicalAddress = "";
NetworkInterface[] nice = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adaper in nice)
{
//Debug.Log(adaper.Description);
if (adaper.Description == "en0")
{
physicalAddress = adaper.GetPhysicalAddress().ToString();
break;
}
else
{
physicalAddress = adaper.GetPhysicalAddress().ToString();
if (physicalAddress != "")
{
break;
};
}
}
return physicalAddress;
}
public override void Open(object o)//打开界面
{
base.Open(o);
Transform tParent = Go.transform.parent;
// Debug.LogError(tParent.childCount);
Go.transform.SetSiblingIndex(3);
}
public override void Close()//关闭界面
{
base.Close();
// UILevel2Manager.Instance.IsRegistered = true;
}
public override void SetPanel(object o)
{
//model = (Model)o;
// Debug.LogError(model.str);
PanelInit();
if (File.Exists(configPath))
{
loginValue = File.ReadAllText(configPath);
if (Encryption(GetMacAddress()) == loginValue)
{
AddUI._Ins._IsOK = true;
Close();
Debug.LogError("已注册");
return;
}
}
Debug.LogError("需要注册");
AddUI._Ins._IsOK = false;
}
string Encryption(string mac)
{
char[] strs = mac.ToArray();
int num = 0;
for (int i = 0; i < strs.Length; i++)
{
num += stringToInt(strs[i].ToString());
}
num = num * 6 / 2;
return num + ".LTJD";
}
int stringToInt(string str)
{
switch (str)
{
case "A":
return 155;
case "B":
return 22;
case "C":
return 14;
case "D":
return 45;
case "E":
return 152;
case "F":
return 454;
case "G":
return 327;
case "H":
return 421;
case "I":
return 429;
case "J":
return 244;
case "K":
return 454;
case "L":
return 473;
case "M":
return 122;
case "N":
return 277;
case "O":
return 129;
case "P":
return 973;
case "Q":
return 528;
case "R":
return 772;
case "S":
return 553;
case "T":
return 537;
case "U":
return 877;
case "V":
return 321;
case "W":
return 624;
case "X":
return 752;
case "Y":
return 429;
case "Z":
return 450;
default:
return 136;
}
}
}
}