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) + "\r\n";
            File.AppendAllText(Application.dataPath + "/JsonFiles/PlayerInfo.json", s);
            Destroy(register);
            return;
        }
        error.SetActive(true);
    }

    public void Register() {
        Instantiate(register);
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM