Unity—JsonFx序列化场景

场景数据类:

/// <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

转载于:https://www.cnblogs.com/Sakya00/p/5558394.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值