Exemplo n.º 1
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;
        }
Exemplo n.º 2
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);
        }