场景数据类:

/// <summary>
/// 关卡数据
/// </summary>
public class LevelData
{
    //关卡名称
    public string levelName;
    //物体列表
    public List<DataType> objectsToData = new List< DataType>();
 
    public void AddObj(string prefabName, GameObject obj)
    {
        DataType data = new DataType(prefabName, obj.transform.position, obj.transform.eulerAngles, obj.transform.localScale);
        objectsToData.Add(data);
    }
}
 
/// <summary>
/// 物体数据,pos,rot,scale
/// </summary>
public class DataType
{
    public string prefabName;
 
    public float posX;
    public float posY;
    public float posZ;
    public float rotX;
    public float rotY;
    public float rotZ;
    public float scaleX;
    public float scaleY;
    public float scaleZ;
 
    public DataType()
    {
    }
 
    public DataType( string name, Vector3 position, Vector3 rotation, Vector3 scale)
    {
        prefabName = name;
 
        posX = position.x;
        posY = position.y;
        posZ = position.z;
        rotX = rotation.x;
        rotY = rotation.y;
        rotZ = rotation.z;
        scaleX = scale.x;
        scaleY = scale.y;
        scaleZ = scale.z;
    }
 
    public Vector3 GetPos()
    {
        return new Vector3(posX, posY, posZ);
    }
 
    public Vector3 GetRotation()
    {
        return new Vector3(rotX, rotY, rotZ);
    }
 
    public Vector3 GetScale()
    {
        return new Vector3(scaleX, scaleY, scaleZ);
    }
}
 
 
序列化场景物体:
public class SerializeScene : ScriptableWizard
{
    string assetPath;
 
    [MenuItem("Tools/Serialize Scene")]
    static void SerializeOpenScene()
    {
        SerializeScene ss = (SerializeScene)ScriptableWizard .DisplayWizard("Serialize Scene", typeof(SerializeScene ));
    }
 
    void OnWizardCreate()
    {
        // Get the path we'll use to write our assets:
        assetPath = Application.dataPath + "/Resources/" + "SceneInfo/" ;
 
        // Create the folder that will hold our assets:
        if (! Directory.Exists(assetPath))
        {
            Directory.CreateDirectory(assetPath);
        }
 
        FindAssets();
 
        // Make sure the new assets are (re-)imported:
        AssetDatabase.Refresh();
    }
 
    private void FindAssets()
    {
        List< GameObject> objList = new List <GameObject >();
        LevelData newLevel = new LevelData();
 
        newLevel.levelName = SceneManager.GetActiveScene().name;
        GameObject parent = GameObject .Find( "ObjectRoot" );
        if (parent == null)
        {
            Debug.LogError( "No ObjectRoot Node!");
            return;
        }
 
        foreach ( Transform trans in parent.transform)
        {
            Debug.Log(trans.name);
            AddObjects(trans.name, trans.gameObject, ref newLevel);
        }
 
        string json = JsonFx.Json. JsonWriter.Serialize(newLevel);
 
        FileInfo file = new FileInfo(assetPath + newLevel.levelName + ".txt");
        try
        {
            file.Delete();
        }
        catch (System.IO. IOException e)
        {
 
            Debug.Log(e.Message);
        }
 
        FileStream fs = new FileStream (assetPath + newLevel.levelName + ".txt", FileMode.OpenOrCreate, FileAccess .Write);
        StreamWriter sw = new StreamWriter (fs);
        sw.Write(json);
        sw.Close();
        fs.Close();
    }
 
    private void AddObjects( string prefabName, GameObject obj, ref LevelData level)
    {
        if ( PrefabType.PrefabInstance == PrefabUtility .GetPrefabType(obj))
        {
            level.AddObj(prefabName, obj);
        }
        else
        {
            Debug.Log( "Not a Prefab!");
        }
    }
}
 
序列化场景物体之前需要将ObjectRoot节点下的物体存为prefab,再到菜单中选择Tools-->Serialize Scene,在弹出的界面中点击create按钮即可生成当前场景的Json文件。
 
插件链接: http://pan.baidu.com/s/1jIgzRyY 密码: djqt
 
JsonFX GitHub:https://github.com/jsonfx/jsonfx
 
参考:1.http://www.cnblogs.com/sifenkesi/p/3597106.html
   2.http://zaxisgames.blogspot.com/2012/01/minimizing-build-size-in-unity-games.html

Unity—JsonFx序列化场景的更多相关文章

  1. UNITY Serializer 序列化 横向对比

    UNITY Serializer 序列化 横向对比 关于序列化,无论是.net还是unity自身都提供了一定保障.然而人总是吃着碗里想着锅里,跑去github挖个宝是常有的事.看看各家大佬的本事.最有 ...

  2. Unity跳转场景进度条制作教程(异步加载)

    Unity跳转场景进度条制作 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分享 ...

  3. Unity JsonFx 插件使用

    在Unity中使用 JsonFx 插件笔记(提示:以下在 Unity3D v5.4.0 版本 Win 平台下测试成功) 下载 JsonFx 插件注意:JsonFx 插件其实就是一个 dll 文件(如果 ...

  4. HoloLens开发手记 - Unity之Persistence 场景保持

    Persistence 场景保持是HoloLens全息体验的一个关键特性,当用户离开原场景中时,原场景中全息对象会保持在特定位置,当用户回到原场景时,能够准确还原原场景的全息内容.WorldAncho ...

  5. 【Unity入门】场景、游戏物体和组件的概念

    版权声明:本文为博主原创文章,转载请注明出处. 游戏和电影一样,是通过每一个镜头的串联来实现的,而这样的镜头我们称之为“场景”.一个游戏一般包含一个到多个场景,这些场景里面实现了不同的功能,把它们组合 ...

  6. 【Unity入门】场景编辑与场景漫游快捷键

    版权声明:本文为博主原创文章,转载请注明出处. 打开Unity主窗口,选择顶部菜单栏的“GameObject”->“3D Object”->“Plane”在游戏场景里面添加一个面板对象.然 ...

  7. Unity跳转场景

    Unity中如何加载场景 1.首先需要将场景添加到 Build Settings中,如下图: 2.引用using UnityEngine.SceneManagement; 同步加载:如果场景很大,有可 ...

  8. 【Unity】序列化字典Dictionary的问题

    问题:在C#脚本定义了public Dictionary字典,然而在编辑器检视面板Editor Inspector中看不到(即无法序列化字典).即不能在编辑器中拖拽给字典赋值. 目标:检视面板Insp ...

  9. unity 3D游戏场景转换

    //////////////////2015/07/07//////// /////////////////by xbw/////////////// ///////////////环境 unity ...

随机推荐

  1. 【OpenGL】用OpenGL shader实现将YUV(YUV420,YV12)转RGB-(直接调用GPU实现,纯硬件方式,效率高)

    这段时间一直在搞视频格式的转换问题,终于最近将一个图片的YUV格式转RGB格式转换成功了.下面就来介绍一下: 由于我的工程是在vs2008中的,其中包含一些相关头文件和库,所以下面只是列出部分核心代码 ...

  2. 【云计算】Docker build解决父镜像层级关系过多问题:Cannot create container with more than 127 parents

    docker export 8a6e92c71a77 > malakas.tzr.gz cat malakas.tzr.gz|docker import - /nscloud/malakas:1 ...

  3. javascript设置首页,加入收藏

    <a href="javascript:;" id="setHomePage" class="toolsbar" onclick=&q ...

  4. 使用jstack和TDA进行java线程dump分析

    转载:http://blog.csdn.net/everlasting_188/article/details/51943095 1.jstack重点关注 命令行:jstack [-l][F] pid ...

  5. TestNG参数化测试Spring应用Dubbo接口

    一.配置dubbo的Bean文件: 配置spring-dubbo.xml文件: <dubbo:reference interface="com.datatrees.basisdata. ...

  6. UVA270-Lining Up

    斜率斜率斜率......... #include<iostream> #include<cstdio> #include<algorithm> #include&l ...

  7. js 数组去重方法汇总

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...

  8. 如何从官网下载Spring

    1.Spring下载地址http://repo.spring.io/release/org/springframework/spring/ 里面有各自版本下载: 方法二: 1.在百度中输入Spring ...

  9. 补习知识:Entity Framework Code First属性映射约定

    Entity Framework Code First与数据表之间的映射方式有两种实现:Data Annotation和Fluent API.本文中采用创建Product类为例来说明tity Fram ...

  10. EMQ学习笔记---Clean Session和Retained Message

    MQTT会话(Clean Session)MQTT客户端向服务器发起CONNECT请求时,可以通过’Clean Session’标志设置会话.‘Clean Session’设置为0,表示创建一个持久会 ...