コード例 #1
0
        /**
         * 导出资源
         */
        public static void ExportPrefab(GameObject curObj, string exportPath = "")
        {
            ResourceManager.instance.Clean();
            string prefabPath = "Assets/" + curObj.name + ".prefab.json";

            //如果是Unity预制体那么就导出所在目录,如果是场景的一个普通GameObject,那么导出Assets下
            if (UnityEditor.PrefabUtility.GetPrefabType(curObj) == UnityEditor.PrefabType.PrefabInstance)
            {
                UnityEngine.Object parentObject = UnityEditor.PrefabUtility.GetPrefabParent(curObj);
                prefabPath = UnityEditor.AssetDatabase.GetAssetPath(parentObject) + ".json";
            }
            //保存路径
            PathHelper.SetSceneOrPrefabPath(prefabPath);
            //预制体坐标归零,直接改坐标
            var savePosition = curObj.transform.localPosition;

            if (ExportToolsSetting.instance.prefabResetPos)
            {
                curObj.transform.localPosition = Vector3.zero;
            }

            SerializeObject.Serialize(curObj);
            curObj.transform.localPosition = savePosition;
            ResourceManager.instance.ExportFiles(prefabPath, exportPath);
        }
コード例 #2
0
        /**
         * 导出资源
         */
        public static void Export(GameObject curObj, string exportPath)
        {
            SerializeObject.Clear();
            string prefabPath = "Assets/" + curObj.name + ".prefab.json";

            //如果是Unity预制体那么就导出所在目录,如果是场景的一个普通GameObject,那么导出Assets下

            #if UNITY_2018_4_OR_NEWER
            if (UnityEditor.PrefabUtility.GetPrefabAssetType(curObj) == UnityEditor.PrefabAssetType.Variant)
            {
                UnityEngine.Object parentObject = UnityEditor.PrefabUtility.GetPrefabInstanceHandle(curObj);
                prefabPath = UnityEditor.AssetDatabase.GetAssetPath(parentObject) + ".json";
            }
            #else
            if (UnityEditor.PrefabUtility.GetPrefabType(curObj) == UnityEditor.PrefabType.PrefabInstance)
            {
                UnityEngine.Object parentObject = UnityEditor.PrefabUtility.GetPrefabParent(curObj);
                prefabPath = UnityEditor.AssetDatabase.GetAssetPath(parentObject) + ".json";
            }
            #endif
            //保存路径
            PathHelper.SetSceneOrPrefabPath(prefabPath);
            //预制体坐标归零,直接改坐标
            var savePosition = curObj.transform.localPosition;
            if (ExportSetting.instance.common.posToZero)
            {
                curObj.transform.localPosition = Vector3.zero;
            }

            SerializeObject.SerializeEntity(curObj);

            SerializeContext.Export(exportPath, prefabPath);
            curObj.transform.localPosition = savePosition;
        }
コード例 #3
0
        public static void ExportScene(List <GameObject> roots, string exportPath = "")
        {
            string sceneName = PathHelper.CurSceneName;

            //导出场景
            try
            {
                ExportImageTools.instance.Clear();
                ResourceManager.instance.Clean();
                //路径
                string scenePath = sceneName + ".scene.json";
                PathHelper.SetSceneOrPrefabPath(scenePath);
                //Scene
                var           scene     = UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene();
                MyJson_Object sceneJson = new MyJson_Object();
                sceneJson.SetUUID(scene.GetHashCode().ToString());//用场景名称的hashCode
                sceneJson.SetUnityID(scene.GetHashCode());
                sceneJson.SetClass("paper.Scene");
                sceneJson.SetString("name", sceneName.Substring(sceneName.LastIndexOf('/') + 1));

                sceneJson.SetColor("ambientColor", RenderSettings.ambientLight);
                sceneJson.SetNumber("lightmapIntensity", UnityEditor.Lightmapping.indirectOutputScale);
                //allGameObjects
                var gameObjectsJson = new MyJson_Array();
                sceneJson["gameObjects"] = gameObjectsJson;
                GameObject[] allObjs = GameObject.FindObjectsOfType <GameObject>();
                for (int i = 0; i < allObjs.Length; i++)
                {
                    gameObjectsJson.AddHashCode(allObjs[i]);
                }
                //lightmaps
                sceneJson["lightmaps"] = ExportSceneTools.AddLightmaps(exportPath);
                ResourceManager.instance.AddObjectJson(sceneJson);
                //序列化
                foreach (var root in roots)
                {
                    SerializeObject.Serialize(root);
                }
                ResourceManager.instance.ExportFiles(scenePath, exportPath);
                MyLog.Log("----场景导出成功----");
            }
            catch (System.Exception e)
            {
                MyLog.LogError(sceneName + "  : 导出失败-----------" + e.StackTrace);
            }
        }
コード例 #4
0
        public static void Export(List <GameObject> roots, string exportPath)
        {
            string sceneName = PathHelper.CurSceneName;

            SerializeObject.Clear();
            //路径
            string scenePath = sceneName + ".scene.json";

            PathHelper.SetSceneOrPrefabPath(scenePath);

            var scene = UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene();

            var sceneEntity = SerializeObject.currentData.CreateEntity();

            var sceneComp = SerializeObject.currentData.CreateComponent(SerializeClass.Scene);

            sceneComp.properties.SetString("name", sceneName.Substring(sceneName.LastIndexOf('/') + 1));
            sceneEntity.AddComponent(sceneComp);

            var treeComp = SerializeObject.currentData.CreateComponent(SerializeClass.TreeNode);

            treeComp.properties.SetString("name", "Root");
            treeComp.properties.SetReference("scene", sceneComp.uuid);
            sceneEntity.AddComponent(treeComp);

            var sceneSetting = ExportSetting.instance.scene;
            // 环境光和光照贴图
            var sceneLightComp = SerializeObject.currentData.CreateComponent(SerializeClass.SceneLight);

            sceneLightComp.properties.SetColor("ambientColor", RenderSettings.ambientLight);
            if (sceneSetting.lightmap)
            {
                sceneLightComp.properties.SetNumber("lightmapIntensity", UnityEditor.Lightmapping.indirectOutputScale);
                sceneLightComp.properties.SetLightmaps(exportPath);
            }

            sceneEntity.AddComponent(sceneLightComp);

            if (sceneSetting.staticBatching)
            {
                var staticBatching = SerializeObject.currentData.CreateComponent(SerializeClass.StaticBatching);
                sceneEntity.AddComponent(staticBatching);
            }

            // 雾
            if (RenderSettings.fog && sceneSetting.fog)
            {
                var fogComp = SerializeObject.currentData.CreateComponent(SerializeClass.Fog);
                if (RenderSettings.fogMode == FogMode.Linear)
                {
                    fogComp.properties.SetInt("mode", 0);
                    fogComp.properties.SetNumber("near", RenderSettings.fogStartDistance);
                    fogComp.properties.SetNumber("far", RenderSettings.fogEndDistance);
                }
                else
                {
                    fogComp.properties.SetInt("mode", 1);
                    fogComp.properties.SetNumber("density", RenderSettings.fogDensity);
                }
                fogComp.properties.SetColor("color", RenderSettings.fogColor);
                sceneEntity.AddComponent(fogComp);
            }

            foreach (var child in roots)
            {
                var childEntity = SerializeObject.SerializeEntity(child);
                if (childEntity != null)
                {
                    treeComp.AddChild(childEntity.treeNode);
                }
            }

            SerializeContext.Export(exportPath, scenePath);
        }