场景数据类:

/// <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. POJ 1275-Cashier Employment(差分约束系统)

    题目地址:id=1275">POJ 1275 题意: 给出一个超市24小时各须要R[i]个雇员工作,有N个雇员能够雇佣.他们開始工作时间分别为A[i],求须要的最少的雇员人数. 思路: ...

  2. 教你如何搭建vue项目

    笔者工作也有一些时间,需要用vue写项目时也总是项目组长已经把项目搭建好了, 偶尔心血来潮想试着自己搭建一个vue项目 我们搭建vue项目呢主要是用到了vue-cli来搭建,但是前提是必须要已经安装好 ...

  3. svn: warning: xxxx is already under version control

    svn stat  查看当前目录下svn状态 svn remove xxxx svn add xxx svn ci -m "注释"

  4. Quartz JobStore管理Job

    Quartz提供了RAMJobStore和JDBC JobStore两种方式用来Job,RAMJobStore将Job任务存入内存中,速度快:JobStore采用数据库的方式管理中,本文介绍JobSt ...

  5. XP系统如何把桌面图标变大

    右击桌面,属性,外观,高级,在项目里面找到图标,大小改为你喜欢的样式.   我测试的结果是:图标大小改为42,字体大小改为8,图标垂直间距改为100,水平间距改为54效果不错.

  6. com.apple.installer.pagecontroller 错误 -1 pkg安装错误

    在网上下载了一个pkg 的安装文件: 在mac上安装一打就出现错误 原因是,文件从网上直接下载的,会出权限问题,需要修复安装软件的安装权限: 我的原因是,下载的是个rar的mac解压不了,就在线解压, ...

  7. 七个你无法忽视的Git使用技巧

    与其他技术相比,Git应该拯救了更多开发人员的饭碗.只要你经常使用Git保存自己的工作,你就一直有机会可以将代码退回到之前的状态,因此就可以挽回那些你深夜里迷迷糊糊犯下的错误. 尽管这么说,Git的命 ...

  8. highcharts 坐标轴 数值 格式化

    以Y轴为示例: yAxis: { min: 0, gridLineColor: '#ececee', gridLineWidth: 1, lineColor: '#ececee', lineWidth ...

  9. nodejs入门篇---创建project并具体解释

    想了非常久.总想写点对大家有优点的,今天解说生成项目. 如今市面上一般须要人全栈-----mean(mongo,express.angular,nodejs),这样能够从前端开发到后端以及数据库,听起 ...

  10. 【Python 数据分析】module 'numpy' has no attribute 'array'

    安装好Numpy模块后,开始做了几个小测试都可以运行,但是当我创建numpy.py这个文件后 numpy.py import numpy y = numpy.array([[11,4,2],[2,6, ...