zoukankan      html  css  js  c++  java
  • Unity进阶:用AssetBundle和Json做了一个玩家登陆界面

    版权申明:

    • 本文原创首发于以下网站:
    1. 博客园『优梦创客』的空间:https://www.cnblogs.com/raymondking123
    2. 优梦创客的官方博客:https://91make.top
    3. 优梦创客的游戏讲堂:https://91make.ke.qq.com
    4. 『优梦创客』的微信公众号:umaketop
    • 您可以自由转载,但必须加入完整的版权声明

    1.创建玩家登陆界面UI

    2.点击注册按钮进入注册界面

    3.注册的玩家信息会被保存到PlayerInfo.json文件中

    4.输入玩家信息如果用户名或密码出错会提示用户信息错误,并不执行加载

    5.输入正确的信息才能加载到LoadScene场景中

    6.加载结束后即可进入游戏画面

    代码如下:
    1.创建一个玩家信息类

    [Serializable]//让玩家信息类可序列化和反序列化
    public class PlayerInfo {
        public string userName;
        public string userPassword;
    
    }
    

    2.将游戏场景打包成AssetBundle资源

    public class BuildAsset : MonoBehaviour {
        [MenuItem("AssetBundle/Build")]
    
        public static void Build() {
            BuildPipeline.BuildAssetBundles(Application.dataPath + "/AssetBundle",BuildAssetBundleOptions.None,BuildTarget.StandaloneWindows);
        }
    }
    

    3.UI控制脚本

    public class UI_Manager : MonoBehaviour, IPointerClickHandler
    {
        public PlayerInfo playerInfo;
        public InputField userName;
        public InputField userPassword;
        public GameObject error;
        public GameObject register;
        public WWW www;
    
        public void OnPointerClick(PointerEventData eventData)
        {
            switch (this.name) {
                case "Register":
                    Register();
                    break;
                case "Confirm":
                    Save();
                    break;
                case "Login":
                    Load();
                    break;
                case "Cancel":
                    Cancel();
                    break;
                case "Exit":
                    Exit();
                    break;
            }
        }
    
        private void Start()
        {
            if (this.name == "Load") {
                Loading();
            }
        }
    
        private void Update()
        {
            if (this.name == "Load") {
                GetComponent<Slider>().value = www.progress;
            }
        }
    
        public void Exit() {
            EditorApplication.isPlaying = false;
        }
    
        public void Cancel() {
            Destroy(register);
        }
    
        public void Loading() {
            StartCoroutine(DownLoadAB());
        }
    
        public IEnumerator DownLoadAB() {
            www = new WWW("file://" + Application.dataPath + "/AssetBundle/scene-bundle");
            yield return www;
            AssetBundle ab = www.assetBundle;
            SceneManager.LoadScene("DemoScene");
        }
    
        public void Load()
        {
            string[] s = File.ReadAllLines(Application.dataPath + "/JsonFiles/PlayerInfo.json");
            for (int i=0;i<s.Length;i++) {
                playerInfo = JsonUtility.FromJson<PlayerInfo>(s[i]);
                if (userName.text == playerInfo.userName && userPassword.text == playerInfo.userPassword) {
                    SceneManager.LoadScene("LoadScene");
                    return;
                }
            }
            error.SetActive(true);
        }
    
        public void Save()
        {
            playerInfo.userName = userName.text;
            playerInfo.userPassword = userPassword.text;
            if (playerInfo.userName != "" && playerInfo.userPassword != "") {
                string s = JsonUtility.ToJson(playerInfo) + "
    ";
                File.AppendAllText(Application.dataPath + "/JsonFiles/PlayerInfo.json", s);
                Destroy(register);
                return;
            }
            error.SetActive(true);
        }
    
        public void Register() {
            Instantiate(register);
        }
    }
    
    
  • 相关阅读:
    C# 泛型
    EventHandler<TEventArgs>委托
    只能输入数字 ,只能有一位小数点。
    MVC过滤器 AuthorizeAttribute使用
    NuGet EntityFramework 常用命令
    Stride游戏引擎试毒
    Unity EditorWindow GUI裁剪
    unity2017自定义编译dll
    Unity
    WPF
  • 原文地址:https://www.cnblogs.com/raymondking123/p/11428138.html
Copyright © 2011-2022 走看看