ユニティ高度:AssetBundleとJSONとは、プレイヤーのログイン画面を作りました

著作権:

  • 次のWebサイトで始まるこの記事オリジナル:
  1. ブログパーク「優れた夢のメイカーズムーブメント」空間にします。https://www.cnblogs.com/raymondking123
  2. 優秀な夢メイカーズムーブメント公式ブログます。https://91make.top
  3. 優れたゲーム夢のメーカー文化講演します。https://91make.ke.qq.com
  4. マイクロチャンネル公衆数の「優秀な夢メイカーズムーブメント」:umaketop
  • あなたは転載は自由ですが、完全な著作権表示を含める必要があります

1. UIプレイヤログイン画面作成

2.登録インターフェース入力し、登録ボタンをクリックして

プレイヤー情報の登録は、ファイルPlayerInfo.jsonに保存されます。3.

ユーザー名またはパスワードが間違っは、エラーに関する情報をユーザーに促している場合プレーヤーの情報を入力します。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);
    }
}

おすすめ

転載: www.cnblogs.com/raymondking123/p/11428138.html