1.创建玩家登陆界面UI 2.点击注册按钮进入注册界面 3.注册的玩家信息会被保存到PlayerInfo.json文件中 4.输入玩家信息如果用户名或密码出错会提示用户信息错误,并不执行加载 5.输入正确的信息才能加载到LoadScene场景中 6.加载结束后即可进入游戏画面
代码如下: 1.创建一个玩家信息类
[Serializable]//让玩家信息类可序列化和反序列化public class PlayerInfo { public string userName; public string userPassword;}
[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); }}
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); }}
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();
case "Login":
Load();
case "Cancel":
Cancel();
case "Exit":
Exit();
private void Start()
if (this.name == "Load") {
Loading();
private void Update()
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);
public void Register() {
Instantiate(register);
原文链接: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