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

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

首先新建第一个场景,命名为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!

最新文章

  1. 谈一谈NOSQL的应用,Redis/Mongo
  2. 安卓开发学习历程1——《第一行代码》coolweather项目setOnItemClickListener函数,Sql语句修改对模拟app程序机影响
  3. URAL 1119. Metro(DP)
  4. PAIP.提升效率----论项目知识库的建设。。
  5. 启用Apache Mod_rewrite模块
  6. PHP学习之-1.2 认识PHP脚本标识
  7. tt程序分析(一)
  8. saiku的源码包Bulid常见问题和jar包
  9. APP上传APP Store遇到的各种问题
  10. Flask 页面缓存逻辑,jinja2 过滤器,测试器
  11. Django + Axios &amp; Ajax post和get 传参
  12. JS 从斐波那契数列浅谈递归
  13. NodeJs完全迁移到D盘,形成绿色安装版
  14. Mina - 模拟同步请求
  15. Date时间
  16. 在linux下创建自定义service服务
  17. PL/SQL 下 Command window 与 SQL window 的区别
  18. &lt;译&gt;Zookeeper官方文档
  19. java.lang.IllegalArgumentException: Failed to decrypt.
  20. Python批量复制和重命名文件

热门文章

  1. Android中RelativeLayout的字符水平(垂直居中)对齐
  2. leetcode第一刷_Validate Binary Search Tree
  3. Android 颜色渲染(八) SweepGradient扫描/梯度渲染
  4. 读TIJ -1 对象入门
  5. 分享一个牛逼的PHP无限极分类生成树方法,巧用引用(转)
  6. jQuery日期联动插件
  7. PL/SQL 记录集合IS TABLE OF的使用
  8. [原创] SQLite数据库使用清单(下)
  9. one way WebService
  10. oracle 触发器number判断空值,:NEW赋值,for each row,sql变量引号,to_date,to_char