protected virtual bool _CreateTemporaryTextrueBakeResult(GameObject[] gos) { _usingTemporaryTextureBakeResult = true; _textureBakeResults = TextureBakeResults.CreateForMaterialsOnRenderer(gos); return(true); }
/// <summary> /// Bakes a combined mesh. /// 如果是从Inspector代码中调用的,则传入该组件的SerializedObject /// 对于“烘焙到预制”可能会损坏SerializedObject。这是必需的 /// </summary> public static bool BakeIntoCombined(MeshBakerCommon mom, out bool createdDummyTextureBakeResults, ref SerializedObject so) { OutputOptions prefabOrSceneObject = mom.meshCombiner.outputOption; createdDummyTextureBakeResults = false; if (prefabOrSceneObject != OutputOptions.bakeIntoPrefab && prefabOrSceneObject != OutputOptions.bakeIntoSceneObject) { Debug.LogError("Paramater prefabOrSceneObject must be bakeIntoPrefab or bakeIntoSceneObject"); return(false); } //从父物体获得 贴图合并组件及其贴图合并结果 Asset TextureCombineEntrance tb = mom.GetComponentInParent <TextureCombineEntrance>(); if (mom.textureBakeResults == null && tb != null) { mom.textureBakeResults = tb.textureBakeResults; } //贴图合并结果为空时,则创建 if (mom.textureBakeResults == null) { if (_OkToCreateDummyTextureBakeResult(mom)) { createdDummyTextureBakeResults = true; List <GameObject> gos = mom.GetObjectsToCombine(); if (mom.GetNumObjectsInCombined() > 0) { if (mom.clearBuffersAfterBake) { mom.ClearMesh(); } else { Debug.LogError("'Texture Bake Result' must be set to add more objects to a combined mesh that " + "already contains objects. Try enabling 'clear buffers after bake'"); return(false); } } mom.textureBakeResults = TextureBakeResults.CreateForMaterialsOnRenderer( gos.ToArray(), mom.meshCombiner.GetMaterialsOnTargetRenderer()); Debug.Log("'Texture Bake Result' was not set. " + "Creating a temporary one. Each material will be mapped to a separate submesh."); } } //合并检测 ValidationLevel vl = Application.isPlaying ? ValidationLevel.quick : ValidationLevel.robust; if (!MeshBakerRoot.DoCombinedValidate(mom, ObjsToCombineTypes.sceneObjOnly, new EditorMethods(), vl)) { return(false); } //检测空预制体资源是否已创建 if (prefabOrSceneObject == OutputOptions.bakeIntoPrefab && mom.resultPrefab == null) { Debug.LogError("Need to set the Combined Mesh Prefab field. " + "Create a prefab asset, drag an empty game object into it, and drag it to the 'Combined Mesh Prefab' field."); return(false); } if (mom.meshCombiner.resultSceneObject != null && (SceneBakerUtilityInEditor.GetPrefabType(mom.meshCombiner.resultSceneObject) == PrefabType.modelPrefab || SceneBakerUtilityInEditor.GetPrefabType(mom.meshCombiner.resultSceneObject) == PrefabType.prefab)) { Debug.LogWarning("Result Game Object was a project asset not a scene object instance. Clearing this field."); mom.meshCombiner.resultSceneObject = null; } mom.ClearMesh(); //合并 if (mom.AddDeleteGameObjects(mom.GetObjectsToCombine().ToArray(), null, false)) { mom.Apply(UnwrapUV2); if (createdDummyTextureBakeResults) { //临时合并的贴图 Debug.Log(String.Format("Successfully baked {0} meshes each material is mapped to its own submesh.", mom.GetObjectsToCombine().Count)); } else { Debug.Log(String.Format("Successfully baked {0} meshes", mom.GetObjectsToCombine().Count)); } if (prefabOrSceneObject == OutputOptions.bakeIntoSceneObject) { PrefabType pt = SceneBakerUtilityInEditor.GetPrefabType(mom.meshCombiner.resultSceneObject); if (pt == PrefabType.prefab || pt == PrefabType.modelPrefab) { Debug.LogError("Combined Mesh Object is a prefab asset. " + "If output option bakeIntoSceneObject then this must be an instance in the scene."); return(false); } } else if (prefabOrSceneObject == OutputOptions.bakeIntoPrefab) { string prefabPth = AssetDatabase.GetAssetPath(mom.resultPrefab); if (prefabPth == null || prefabPth.Length == 0) { Debug.LogError("无法保存,合并游戏物体并非磁盘上的资源。"); return(false); } string baseName = Path.GetFileNameWithoutExtension(prefabPth); string folderPath = prefabPth.Substring(0, prefabPth.Length - baseName.Length - 7); string newFilename = folderPath + baseName + "-mesh"; //保存网格资源 SaveMeshsToAssetDatabase(mom, folderPath, newFilename); if (mom.meshCombiner.renderType == RendererType.skinnedMeshRenderer) { Debug.LogWarning("Render type is skinned mesh renderer. " + "Can't create prefab until all bones have been added to the combined mesh object " + mom.resultPrefab + " Add the bones then drag the combined mesh object to the prefab."); } //构建 Prefab RebuildPrefab(mom, ref so); MeshBakerUtility.Destroy(mom.meshCombiner.resultSceneObject); } else { Debug.LogError("合并输出类型出错"); return(false); } } else { //加入合并失败 if (mom.clearBuffersAfterBake) { mom.meshCombiner.ClearBuffers(); } if (createdDummyTextureBakeResults) { MeshBakerUtility.Destroy(mom.textureBakeResults); } return(false); } //清除缓存数据 if (mom.clearBuffersAfterBake) { mom.meshCombiner.ClearBuffers(); } //临时Texture if (createdDummyTextureBakeResults) { MeshBakerUtility.Destroy(mom.textureBakeResults); } return(true); }