public static void SetAnimation(this JObject jsonNode, UnityEngine.GameObject obj, UnityEngine.AnimationClip[] animationClips) { var exportAnimations = new JArray(); jsonNode.Add("_animations", exportAnimations); foreach (var animationClip in animationClips) { var asset = SerializeObject.SerializeAsset(animationClip); var assetIndex = SerializeObject.currentData.AddAsset(asset); exportAnimations.AddAsset(assetIndex); } }
public static void SetMesh(this JContainer jsonNode, UnityEngine.GameObject obj, UnityEngine.Mesh mesh) { if (mesh == null) { return; } var asset = SerializeObject.SerializeAsset(mesh); var assetIndex = SerializeObject.currentData.AddAsset(asset); jsonNode.SetAsset("mesh", assetIndex); }
public static void SetLightmaps(this JObject jsonNode, string exportPath) { var lightmapsJson = new JArray(); jsonNode.Add("lightmaps", lightmapsJson); foreach (var lightmapData in LightmapSettings.lightmaps) { Texture2D lightmap = lightmapData.lightmapColor; var asset = SerializeObject.SerializeAsset(lightmap); var assetIndex = SerializeObject.currentData.AddAsset(asset); lightmapsJson.AddAsset(assetIndex); } }
void OnEnable() { _showStyle.fontSize = 15; _showStyle.normal.textColor = new Color(1, 0, 0, 1); // _serializeObject = new SerializedObject(ExportToolsSetting.instance); _meshIgnoresProperty = _serializeObject.FindProperty("meshIgnores"); // //加载配置文件 ExportConfig.Reload(PathHelper.ConfigPath, PathHelper.SaveRootDirectory); //初始化一些全局的方法 SerializeObject.Initialize(); GLTFInitialize.Initialize(); }
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); } }
public static void SetMaterials(this JObject jsonNode, UnityEngine.GameObject obj, UnityEngine.Material[] materials, bool isParticleMat = false, bool isAnimationMat = false) { var materialsItem = new JArray(); jsonNode.Add("materials", materialsItem); //写材质 foreach (var material in materials) { if (material == null) { UnityEngine.Debug.LogWarning(obj.gameObject.name + " 材质缺失,请检查资源"); continue; } var asset = SerializeObject.SerializeAsset(material); var assetIndex = SerializeObject.currentData.AddAsset(asset); materialsItem.AddAsset(assetIndex); } }
protected override void Serialize(Component component, ComponentData compData) { var obj = component.gameObject; Transform comp = component as Transform; Vector3 localPosition = comp.localPosition; Quaternion localRotation = comp.localRotation; Vector3 localScale = comp.localScale; compData.properties.SetBool("isStatic", obj.isStatic); //localPosition compData.properties.SetVector3("_localPosition", localPosition); //localRotation compData.properties.SetQuaternion("_localRotation", localRotation); //localScale compData.properties.SetVector3("_localScale", localScale); // var treeNode = SerializeObject.currentData.CreateComponent(SerializeClass.TreeNode); treeNode.properties.SetString("name", obj.name); treeNode.properties.SetString("tag", obj.tag); treeNode.properties.SetInt("layer", 1 << obj.layer); if (comp.childCount > 0) { for (int i = 0; i < comp.childCount; i++) { var child = comp.GetChild(i).gameObject; if (child.gameObject.activeInHierarchy) { var childEntity = SerializeObject.SerializeEntity(child); treeNode.AddChild(childEntity.treeNode); } } } compData.entity.AddComponent(treeNode); }
public static void Serialize(GameObject obj) { //未激活的不导出 if ((!ExportToolsSetting.instance.exportUnactivatedObject && !obj.activeInHierarchy)) { MyLog.Log(obj.name + "对象未激活"); return; } if (obj.GetComponent <RectTransform>() != null) { return; } MyLog.Log("导出对象:" + obj.name); MyJson_Object item = new MyJson_Object(); item.SetUUID(obj.GetInstanceID().ToString()); item.SetUnityID(obj.GetInstanceID()); item.SetClass("paper.GameObject"); item.SetString("name", obj.name); item.SetString("tag", obj.tag); var layerMask = 1 << obj.layer; item.SetInt("layer", layerMask); // item.SetInt("layer", LAYER[obj.layer >= LAYER.Length ? 0 : obj.layer]);; item.SetBool("isStatic", obj.isStatic); var componentsItem = new MyJson_Array(); item["components"] = componentsItem; ResourceManager.instance.AddObjectJson(item); var components = obj.GetComponents <Component>(); var index = 0;//TODO foreach (var comp in components) { if (comp is Animator) { components[index] = components[0]; components[0] = comp; } index++; } //遍历填充组件 foreach (var comp in components) { if (comp == null) { MyLog.LogWarning("空的组件"); continue; } string compClass = comp.GetType().Name; MyLog.Log("组件:" + compClass); if (!ExportToolsSetting.instance.exportUnactivatedComp) { //利用反射查看组件是否激活,某些组件的enabled不再继承链上,只能用反射,比如BoxCollider var property = comp.GetType().GetProperty("enabled"); if (property != null && !((bool)property.GetValue(comp, null))) { MyLog.Log(obj.name + "组件未激活"); continue; } } if (!SerializeObject.IsComponentSupport(compClass)) { MyLog.LogWarning("不支持的组件: " + compClass); continue; } if (SerializeObject.SerializedComponent(obj, compClass, comp)) { MyLog.Log("--导出组件:" + compClass); componentsItem.AddHashCode(comp); } else { MyLog.LogWarning("组件: " + compClass + " 导出失败"); } } //遍历子对象 if (obj.transform.childCount > 0) { for (int i = 0; i < obj.transform.childCount; i++) { var child = obj.transform.GetChild(i).gameObject; Serialize(child); } } }
public static EntityData SerializeEntity(GameObject obj) { //未激活的不导出 if ((!ExportSetting.instance.common.exportUnactivatedObject && !obj.activeInHierarchy)) { MyLog.Log(obj.name + "对象未激活"); return(null); } if (obj.GetComponent <RectTransform>() != null) { return(null); } MyLog.Log("对象:" + obj.name); var entityData = currentData.CreateEntity(); var components = obj.GetComponents <Component>(); var index = 0;//TODO foreach (var comp in components) { if (comp is Animator) { components[index] = components[0]; components[0] = comp; } index++; } foreach (var comp in components) { if (comp == null) { MyLog.LogWarning("空的组件"); continue; } string compClass = comp.GetType().Name; // MyLog.Log("组件:" + compClass); if (!ExportSetting.instance.common.exportUnactivatedComp) { //利用反射查看组件是否激活,某些组件的enabled不再继承链上,只能用反射,比如BoxCollider var property = comp.GetType().GetProperty("enabled"); if (property != null && !((bool)property.GetValue(comp, null))) { MyLog.Log(obj.name + "组件未激活"); continue; } } if (!SerializeObject.IsComponentSupport(compClass)) { MyLog.LogWarning("不支持的组件: " + compClass); continue; } var compData = SerializeObject.SerializeComponent(obj, compClass, comp, entityData); if (compData != null) { entityData.AddComponent(compData); MyLog.Log("--导出组件:" + compClass); } else { MyLog.LogWarning("组件: " + compClass + " 导出失败"); } } return(entityData); }
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); }