经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » 游戏设计 » 查看文章
Unity进阶:用AssetBundle和Json做了一个玩家登陆界面
来源:cnblogs  作者:优梦创客  时间:2019/8/30 9:14:30  对本文有异议

版权申明:

  • 本文原创首发于以下网站:
  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.创建一个玩家信息类

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

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

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

3.UI控制脚本

  1. public class UI_Manager : MonoBehaviour, IPointerClickHandler
  2. {
  3. public PlayerInfo playerInfo;
  4. public InputField userName;
  5. public InputField userPassword;
  6. public GameObject error;
  7. public GameObject register;
  8. public WWW www;
  9. public void OnPointerClick(PointerEventData eventData)
  10. {
  11. switch (this.name) {
  12. case "Register":
  13. Register();
  14. break;
  15. case "Confirm":
  16. Save();
  17. break;
  18. case "Login":
  19. Load();
  20. break;
  21. case "Cancel":
  22. Cancel();
  23. break;
  24. case "Exit":
  25. Exit();
  26. break;
  27. }
  28. }
  29. private void Start()
  30. {
  31. if (this.name == "Load") {
  32. Loading();
  33. }
  34. }
  35. private void Update()
  36. {
  37. if (this.name == "Load") {
  38. GetComponent<Slider>().value = www.progress;
  39. }
  40. }
  41. public void Exit() {
  42. EditorApplication.isPlaying = false;
  43. }
  44. public void Cancel() {
  45. Destroy(register);
  46. }
  47. public void Loading() {
  48. StartCoroutine(DownLoadAB());
  49. }
  50. public IEnumerator DownLoadAB() {
  51. www = new WWW("file://" + Application.dataPath + "/AssetBundle/scene-bundle");
  52. yield return www;
  53. AssetBundle ab = www.assetBundle;
  54. SceneManager.LoadScene("DemoScene");
  55. }
  56. public void Load()
  57. {
  58. string[] s = File.ReadAllLines(Application.dataPath + "/JsonFiles/PlayerInfo.json");
  59. for (int i=0;i<s.Length;i++) {
  60. playerInfo = JsonUtility.FromJson<PlayerInfo>(s[i]);
  61. if (userName.text == playerInfo.userName && userPassword.text == playerInfo.userPassword) {
  62. SceneManager.LoadScene("LoadScene");
  63. return;
  64. }
  65. }
  66. error.SetActive(true);
  67. }
  68. public void Save()
  69. {
  70. playerInfo.userName = userName.text;
  71. playerInfo.userPassword = userPassword.text;
  72. if (playerInfo.userName != "" && playerInfo.userPassword != "") {
  73. string s = JsonUtility.ToJson(playerInfo) + "\r\n";
  74. File.AppendAllText(Application.dataPath + "/JsonFiles/PlayerInfo.json", s);
  75. Destroy(register);
  76. return;
  77. }
  78. error.SetActive(true);
  79. }
  80. public void Register() {
  81. Instantiate(register);
  82. }
  83. }

原文链接:http://www.cnblogs.com/raymondking123/p/11428138.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号