场景数据类:

/// <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. C++ 初始化列表(转载)

    何谓初始化列表 与其他函数不同,构造函数除了有名字,参数列表和函数体之外,还可以有初始化列表,初始化列表以冒号开头,后跟一系列以逗号分隔的初始化字段.在 C++中,struct和class的唯一区别是 ...

  2. Item 33: 避免覆盖(hiding)“通过继承得到的名字”

    莎士比亚有一个关于名字的说法."What's in a name?" 他问道,"A rose by any other name would smell as sweet ...

  3. JOptionPane的经常使用4种对话框

    JOptionPane类有4个用于显示对话框的静态方法: 消息.选项.确认,输入对话框 showMessageDialog://显示一条消息并等待用户OK showConfirmDialog://显示 ...

  4. 怎样删除Weblogic Domain?

    转自:http://blog.csdn.net/biplusplus/article/details/7433558 旁白 由于没有现成的配置工具可以做这件事,我们需要手工来删除. 正题 以下方法适用 ...

  5. NSData 转 bytes 字节数据

    NSData 转 bytes 字节数据 NSData *data = [NSData dataWithContentsOfFile:filePath]; NSUInteger len = [data ...

  6. Sun公司开源游戏服务器

    http://www.360doc.com/content/11/0307/12/2902158_98866885.shtml http://www.cnblogs.com/daidu/archive ...

  7. 機器學習基石(Machine Learning Foundations) 机器学习基石 作业三 课后习题解答

    今天和大家分享coursera-NTU-機器學習基石(Machine Learning Foundations)-作业三的习题解答.笔者在做这些题目时遇到非常多困难,当我在网上寻找答案时却找不到,而林 ...

  8. centos7 install flash player

    1.在 https://get.adobe.com/cn/flashplayer/ 上选择需要下载版本---> ( YUM,适用于Linux (YUM) ); 2.进入root权限后,进入你的下 ...

  9. hibernate 多对多双向关联

    package com.bjsxt.hibernate; import java.util.HashSet; import java.util.Set; import javax.persistenc ...

  10. 微信小程序之趣闻

    代码地址如下:http://www.demodashi.com/demo/13433.html 前言 小程序 的火热程度我就不多说了,我之前对这个就蛮有兴趣的,于是花了大概5天的时间,完成了 学习-入 ...