• 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
  • 相关阅读:
    解決 Android Studio 不停 Indexing 的問題(Updating Indices: Indexing paused due to batch update)
    通过某个关键字排序一个字典列表
    django cmes 后台管理
    python 处理音视频
    python 加密
    PyEngine3D
    获取单词列表出现频率最高的单词
    django 上传文件
    python之MD5加密
    切片对象的demo
  • 原文地址:https://www.cnblogs.com/Sakya00/p/5558394.html
Copyright © 2020-2023  润新知