• unity3d 场景间数据传递


    在游戏项目中,常常会使用到用户信息,获取信息当然可以从数据库中获取。但是对场景多的游戏这样做是不正确的,那么我我们就需要再第一次获取用户信息之后,

    同时在其它的场景中共享用户数据,避免对服务器增加负担。好的!现在上图

    首先新建第一个场景,命名为one

    场景中物体如下:

    接着我们新建第二个场景:two

    其中场景对象如下:

    这样做一个简单的区分,以便测试。

    制作思想:

    在前一个场景中,添加新的EmptyGameObject,同时制作脚本PersistentData.cs,在脚本中添加一些全局变量。在游戏加载下一个场景的时候,使用

    Object.DontDestroyOnLoad()方法进行保护。使得有关的数据得以持久化!

    1、PersistentData.cs

    using UnityEngine;
    using System.Collections;


    public class PersistentData : MonoBehaviour {


    public  string userName="";
    public  string pwd="";
    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    if (Application.isLoadingLevel)
       Object.DontDestroyOnLoad(gameObject);
    }


    }

    在场景one中,添加空游戏物体,同时附加如上的代码.

    同时呢,在场景的相机上添加user.cs文件

    代码如下

    using UnityEngine;
    using System.Collections;


    public class user : MonoBehaviour {


    public float width=0.0f;

    public float height=0.0f;
    public string levelName="";
    private string _name="";
    private string _pwd="";
    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }

    void OnGUI() {
     
    GUILayout.BeginVertical(GUILayout.Width(200));
    GUILayout.BeginHorizontal(GUILayout.Width(200));
    GUILayout.Label("Name:");
    _name= GUILayout.TextField(_name,10);
    GUILayout.EndHorizontal();

    GUILayout.BeginHorizontal(GUILayout.Width(200));
    GUILayout.Label("passWord:");
    _pwd= GUILayout.TextField(_pwd,10);
       GUILayout.EndHorizontal();

    if (GUILayout.Button("login",GUILayout.Width(80),GUILayout.Height(60)))
    {
    //save data 保存数据到PersistentData的变量中
    GameObject.Find("data").GetComponent<PersistentData>().userName = _name;
    GameObject.Find("data").GetComponent<PersistentData>().pwd = _pwd;
    Application.LoadLevel("two");
    }
    GUILayout.EndVertical();

    }
    }

    然后在场景two中,给相机添加上PersistentDataGetting.cs文件

    代码如下:

    using UnityEngine;
    using System.Collections;


    public class PersistentDataGetting : MonoBehaviour {


    private string userName="";
    private string pwd="";

    private PersistentData pdScript;
    // Use this for initialization
    void Start () {
     pdScript = GameObject.Find("data").GetComponent<PersistentData>();
     userName = pdScript.userName;
     pwd = pdScript.pwd;
    }

    // Update is called once per frame
    void Update () {

    }

    void OnGUI(){

                   //用户信息显示
    GUI.Label(new Rect(Screen.width/2,Screen.height/2,100,30),userName);
    GUI.Label(new Rect(Screen.width/2, Screen.height/2+40,100,30), pwd);
    }
    }

    最终效果:

    结束语:

    当然数据持久话的方式还有很多,这是其中一种,各有各的优缺点,要根据你存储的数据而定了。

    欢迎大家加入梦想之家 游戏讨论群 63438968    175492844  

    Enjoy!

  • 相关阅读:
    麦茶商务的网站
    jQuery Ajax 实例 ($.ajax、$.post、$.get)
    关于meta知多少
    WebApp之Meta标签
    html5开发之viewport使用
    bootstrap
    8.8&8.9 dp训练小结
    2019.8.10小结
    2019.8.17 小结
    [NOI2001]炮兵阵地 题解
  • 原文地址:https://www.cnblogs.com/alongu3d/p/3252666.html
Copyright © 2020-2023  润新知