using AuShell; using System; using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; public enum LoadMethod { 同步, 异步 } public class AuthorTool : MonoBehaviour { public static AuthorTool m_Instance; /// /// 授权完成加载的场景名称 /// [Header("授权完成加载的场景名称")] public int m_LoadSceneIndex; [Header("场景加载方式")] public LoadMethod loadMethod = LoadMethod.同步; [Header("授权完成关掉界面")] public bool m_HidePanelAfterAuthor; [Header("展示联系信息")] public bool m_ShowAuthorMessage; /// /// 授权联系人 /// public string authorContact; /// /// 授权联系电话 /// public string authorPhone; /// /// 解密狗授权页面 /// private UsbKeyAuthorPanel m_UsbAuthorPanel; /// /// 授权码授权页面 /// private CodeAuthorPanel m_CodeAuthorPanel; /// /// 背景 /// private GameObject m_Bg; /// /// 配置文件路径 /// private string clientConfigFilePath; private AsyncOperation loader; /// /// 授权配置文件 /// private AuthorConfig m_AuthorConfig; /// /// 联系人 /// private Text m_AuthorContact; /// /// 联系电话 /// private Text m_AuthorPhone; private void Awake() { m_Instance = this; clientConfigFilePath = Path.Combine(Application.streamingAssetsPath, "AuthorConfig.json"); m_AuthorConfig = ParseClientConfig(); //初始化UI m_UsbAuthorPanel = transform.Find("UsbAuthorPanel").GetComponent(); m_CodeAuthorPanel = transform.Find("CodeAuthorPanel").GetComponent(); m_AuthorContact = transform.Find("Author/AuthorContact/AuthorContact").GetComponent(); m_AuthorPhone = transform.Find("Author/AuthorPhone/AuthorPhone").GetComponent(); m_Bg = transform.Find("BG").gameObject; transform.Find("CodeAuthorPanel/Loading").gameObject.SetActive(false); } private void Start() { //联系方式 m_AuthorContact.transform.parent.gameObject.SetActive(m_ShowAuthorMessage); m_AuthorPhone.transform.parent.gameObject.SetActive(m_ShowAuthorMessage); m_AuthorContact.text = authorContact; m_AuthorPhone.text = authorPhone; //授权验证,首先验证是否为参数启动的 if (VerifyArg()) { //产品已授权 VerifyArgPass(); } else { ShowAuthorPanel(); } } /// /// 验证参数 /// public bool VerifyArg() { string[] LineAgrs = Environment.GetCommandLineArgs(); if (LineAgrs.Length >= 2) { string tmpLineArg = LineAgrs[1]; try { tmpLineArg = tmpLineArg.Decrypt("chivatech"); string[] tmpArgs = tmpLineArg.Split('/'); Debug.Log(tmpLineArg); Debug.Log(SystemInfo.deviceUniqueIdentifier); //判断机器码是否相同 if (tmpArgs[0] == SystemInfo.deviceUniqueIdentifier) { return true; } } catch (Exception) { return false; } } return false; } /// /// 显示授权界面 /// public void ShowAuthorPanel() { if (m_AuthorConfig.UsbKeyAuthor) { m_UsbAuthorPanel.gameObject.SetActive(true); m_CodeAuthorPanel.gameObject.SetActive(false); return; } if (m_AuthorConfig.CodeAuthor) { m_UsbAuthorPanel.gameObject.SetActive(false); m_CodeAuthorPanel.gameObject.SetActive(true); } } /// /// 加载下一场景 /// public void LoadScene() { SceneManager.LoadScene(m_LoadSceneIndex); } /// /// 授权完成关闭界面 /// private void HidePanel() { m_UsbAuthorPanel.gameObject.SetActive(false); m_CodeAuthorPanel.gameObject.SetActive(false); m_Bg.SetActive(false); } /// /// 授权通过 /// public void VerifyArgPass() { transform.Find("CodeAuthorPanel/Loading").gameObject.SetActive(true); m_CodeAuthorPanel.SetMessageText("产品已授权"); if (m_HidePanelAfterAuthor) HidePanel(); switch (loadMethod) { case LoadMethod.同步: LoadScene(); break; case LoadMethod.异步: StartCoroutine(LoadSceneAsync(LoadSceneAsyncCallback)); break; default: break; } } private void LoadSceneAsyncCallback() { loader.allowSceneActivation = true; } private IEnumerator LoadSceneAsync(Action callBack) { yield return new WaitForEndOfFrame(); loader = SceneManager.LoadSceneAsync(m_LoadSceneIndex); yield return loader; loader.allowSceneActivation = false; if (callBack != null) callBack.Invoke(); } /// /// 解析配置文件 /// /// public AuthorConfig ParseClientConfig() { if (!File.Exists(clientConfigFilePath)) { StreamWriter streamWriter = File.CreateText(clientConfigFilePath); streamWriter.Write(JsonUtility.ToJson(new AuthorConfig())); streamWriter.Close(); } return JsonUtility.FromJson(File.ReadAllText(clientConfigFilePath)); } }