码迷,mamicode.com
首页 > 编程语言 > 详细

Unity—JsonFx序列化场景

时间:2016-06-04 11:59:28      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:

场景数据类:

/// <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序列化场景

标签:

原文地址:http://www.cnblogs.com/Sakya00/p/5558394.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!