示例#1
0
    void _bakeMeshesInPlace(MB2_MeshBakerCommon mom)
    {
        Mesh mesh;

        //MB2_MeshBakerCommon mom = (MB2_MeshBakerCommon) target;
        if (!MB_Utility.doCombinedValidate(mom, MB_ObjsToCombineTypes.prefabOnly))
        {
            return;
        }
        mom.DestroyMesh();

        List <GameObject> objsToMesh = mom.objsToMesh;

        if (mom.useObjsToMeshFromTexBaker && mom.GetComponent <MB2_TextureBaker>() != null)
        {
            objsToMesh = mom.GetComponent <MB2_TextureBaker>().objsToMesh;
        }

        GameObject[]  objs               = new GameObject[1];
        List <string> usedNames          = new List <string>();
        MB_RenderType originalRenderType = mom.renderType;

        for (int i = 0; i < objsToMesh.Count; i++)
        {
            if (objsToMesh[i] == null)
            {
                Debug.LogError("The " + i + "th object on the list of objects to combine is 'None'. Use Command-Delete on Mac OS X; Delete or Shift-Delete on Windows to remove this one element.");
                return;
            }
            objs[0] = objsToMesh[i];
            Renderer r = MB_Utility.GetRenderer(objsToMesh[i]);
            if (r is SkinnedMeshRenderer)
            {
                mom.renderType = MB_RenderType.skinnedMeshRenderer;
            }
            else
            {
                mom.renderType = MB_RenderType.meshRenderer;
            }
            mesh = mom.AddDeleteGameObjects(objs, null, false);
            if (mesh != null)
            {
                //mom.ApplyAll();
                mom.Apply();
                Mesh mf = MB_Utility.GetMesh(objs[0]);
                if (mf != null)
                {
                    string baseName, folderPath, newFilename;
                    string pth = AssetDatabase.GetAssetPath(mf);
                    if (pth != null && pth.Length != 0)
                    {
                        baseName   = System.IO.Path.GetFileNameWithoutExtension(pth) + "_" + objs[0].name + "_MB";
                        folderPath = System.IO.Path.GetDirectoryName(pth);
                    }
                    else                                           //try to get the name from prefab
                    {
                        pth = AssetDatabase.GetAssetPath(objs[0]); //get prefab name
                        if (pth != null && pth.Length != 0)
                        {
                            baseName   = System.IO.Path.GetFileNameWithoutExtension(pth) + "_" + objs[0].name + "_MB";
                            folderPath = System.IO.Path.GetDirectoryName(pth);
                        }
                        else                             //save in root
                        {
                            baseName   = objs[0].name + "mesh_MB";
                            folderPath = "Assets";
                        }
                    }
                    //make name unique
                    newFilename = System.IO.Path.Combine(folderPath, baseName + ".asset");
                    int j = 0;
                    while (usedNames.Contains(newFilename))
                    {
                        newFilename = System.IO.Path.Combine(folderPath, baseName + j + ".asset");
                        j++;
                    }
                    usedNames.Add(newFilename);
                    updateProgressBar("Created mesh saving mesh on " + objs[0].name + " to asset " + newFilename, .6f);
                    if (newFilename != null && newFilename.Length != 0)
                    {
                        Debug.Log("Creating mesh for " + objs[0].name + " with adjusted UVs at: " + newFilename);
                        AssetDatabase.CreateAsset(mesh, newFilename);
                    }
                    else
                    {
                        Debug.LogWarning("Could not save mesh for " + objs[0].name);
                    }
                }
            }
            mom.DestroyMesh();
        }
        mom.renderType = originalRenderType;
        return;
    }
示例#2
0
    void _bakeIntoCombined(MB2_MeshBakerCommon mom, MB_OutputOptions prefabOrSceneObject)
    {
        if (prefabOrSceneObject != MB_OutputOptions.bakeIntoPrefab && prefabOrSceneObject != MB_OutputOptions.bakeIntoSceneObject)
        {
            Debug.LogError("Paramater prefabOrSceneObject must be bakeIntoPrefab or bakeIntoSceneObject");
            return;
        }
        //MB2_MeshBakerCommon mom = (MB2_MeshBakerCommon) target;
        MB2_TextureBaker tb = mom.GetComponent <MB2_TextureBaker>();

        if (mom.textureBakeResults == null && tb != null)
        {
            Debug.Log("setting results");
            mom.textureBakeResults = tb.textureBakeResults;
        }

        if (mom.useObjsToMeshFromTexBaker)
        {
            if (tb != null)
            {
                mom.objsToMesh.Clear();
                mom.objsToMesh.AddRange(tb.objsToMesh);
            }
        }

        Mesh mesh;

        if (!MB_Utility.doCombinedValidate(mom, MB_ObjsToCombineTypes.sceneObjOnly))
        {
            return;
        }
        if (prefabOrSceneObject == MB_OutputOptions.bakeIntoPrefab && mom.resultPrefab == null)
        {
            Debug.LogError("Need to set the Combined Mesh Prefab field.");
            return;
        }
        mom.ClearMesh();

        mesh = mom.AddDeleteGameObjects(mom.objsToMesh.ToArray(), null, false, true);
        if (mesh != null)
        {
            mom.Apply();
            updateProgressBar("Created mesh saving assets", .6f);
            if (prefabOrSceneObject == MB_OutputOptions.bakeIntoSceneObject)
            {
//				mom.BuildSceneMeshObject();
            }
            else if (prefabOrSceneObject == MB_OutputOptions.bakeIntoPrefab)
            {
                string prefabPth = AssetDatabase.GetAssetPath(mom.resultPrefab);
                if (prefabPth == null || prefabPth.Length == 0)
                {
                    Debug.LogError("Could not save result to prefab. Result Prefab value is not an Asset.");
                    return;
                }
                string baseName    = System.IO.Path.GetFileNameWithoutExtension(prefabPth);
                string folderPath  = prefabPth.Substring(0, prefabPth.Length - baseName.Length - 7);
                string newFilename = folderPath + baseName + "-mesh";
                mom.SaveMeshsToAssetDatabase(folderPath, newFilename);
                mom.RebuildPrefab();
//				saveMeshToAssetDatabase(mom, mesh);
//				if (mom.renderType == MB_RenderType.skinnedMeshRenderer){
//					Debug.LogWarning("Prefab will not be updated for skinned mesh. This is because all bones need to be included in the prefab for it to be usefull.");
//				} else {
//					rebuildPrefab(mom, mesh);
//				}
            }
            else
            {
                Debug.LogError("Unknown parameter");
            }
        }
    }
示例#3
0
    public static void _bakeIntoCombined(MB2_MeshBakerCommon mom, MB_OutputOptions prefabOrSceneObject)
    {
        if (MB2_MeshCombiner.EVAL_VERSION && prefabOrSceneObject == MB_OutputOptions.bakeIntoPrefab)
        {
            Debug.LogError("Cannot BakeIntoPrefab with evaluation version.");
            return;
        }
        if (prefabOrSceneObject != MB_OutputOptions.bakeIntoPrefab && prefabOrSceneObject != MB_OutputOptions.bakeIntoSceneObject)
        {
            Debug.LogError("Paramater prefabOrSceneObject must be bakeIntoPrefab or bakeIntoSceneObject");
            return;
        }
        //MB2_MeshBakerCommon mom = (MB2_MeshBakerCommon) target;
        MB2_TextureBaker tb = mom.GetComponent <MB2_TextureBaker>();

        if (mom.textureBakeResults == null && tb != null)
        {
            Debug.Log("setting results");
            mom.textureBakeResults = tb.textureBakeResults;
        }

        if (mom.useObjsToMeshFromTexBaker)
        {
            if (tb != null)
            {
                mom.GetObjectsToCombine().Clear();
                mom.GetObjectsToCombine().AddRange(tb.GetObjectsToCombine());
            }
        }

        Mesh mesh;

        if (!MB2_MeshBakerRoot.doCombinedValidate(mom, MB_ObjsToCombineTypes.sceneObjOnly, new MB2_EditorMethods()))
        {
            return;
        }
        if (prefabOrSceneObject == MB_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;
        }
        mom.ClearMesh();
        mesh = mom.AddDeleteGameObjects(mom.GetObjectsToCombine().ToArray(), null, false, true);
        if (mesh != null)
        {
            mom.Apply(Unwrapping.GenerateSecondaryUVSet);
            Debug.Log(String.Format("Successfully baked {0} meshes into a combined mesh", mom.GetObjectsToCombine().Count));
            if (prefabOrSceneObject == MB_OutputOptions.bakeIntoSceneObject)
            {
                PrefabType pt = PrefabUtility.GetPrefabType(mom.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;
                }
            }
            else if (prefabOrSceneObject == MB_OutputOptions.bakeIntoPrefab)
            {
                string prefabPth = AssetDatabase.GetAssetPath(mom.resultPrefab);
                if (prefabPth == null || prefabPth.Length == 0)
                {
                    Debug.LogError("Could not save result to prefab. Result Prefab value is not an Asset.");
                    return;
                }
                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.renderType == MB_RenderType.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.");
                }
                else
                {
                    RebuildPrefab(mom);
                }
            }
            else
            {
                Debug.LogError("Unknown parameter");
            }
        }
    }
    public static void _bakeIntoCombined(MB2_MeshBakerCommon mom, MB_OutputOptions prefabOrSceneObject)
    {
        if (MB2_MeshCombiner.EVAL_VERSION && prefabOrSceneObject == MB_OutputOptions.bakeIntoPrefab)
        {
            Debug.LogError("Cannot BakeIntoPrefab with evaluation version.");
            return;
        }
        if (prefabOrSceneObject != MB_OutputOptions.bakeIntoPrefab && prefabOrSceneObject != MB_OutputOptions.bakeIntoSceneObject)
        {
            Debug.LogError("Paramater prefabOrSceneObject must be bakeIntoPrefab or bakeIntoSceneObject");
            return;
        }
        //MB2_MeshBakerCommon mom = (MB2_MeshBakerCommon) target;
        MB2_TextureBaker tb = mom.GetComponent <MB2_TextureBaker>();

        if (mom.textureBakeResults == null && tb != null)
        {
            Debug.Log("setting results");
            mom.textureBakeResults = tb.textureBakeResults;
        }

        if (mom.useObjsToMeshFromTexBaker)
        {
            if (tb != null)
            {
                mom.objsToMesh.Clear();
                mom.objsToMesh.AddRange(tb.objsToMesh);
            }
        }

        Mesh mesh;

        if (!MB2_MeshBakerRoot.doCombinedValidate(mom, MB_ObjsToCombineTypes.sceneObjOnly))
        {
            return;
        }
        if (prefabOrSceneObject == MB_OutputOptions.bakeIntoPrefab &&
            mom.resultPrefab == null)
        {
            Debug.LogError("Need to set the Combined Mesh Prefab field.");
            return;
        }
        mom.ClearMesh();

        mesh = mom.AddDeleteGameObjects(mom.objsToMesh.ToArray(), null, false, true);
        if (mesh != null)
        {
            mom.Apply();
//			updateProgressBar("Created mesh saving assets",.6f);
            if (prefabOrSceneObject == MB_OutputOptions.bakeIntoSceneObject)
            {
//				mom.BuildSceneMeshObject();
            }
            else if (prefabOrSceneObject == MB_OutputOptions.bakeIntoPrefab)
            {
                string prefabPth = AssetDatabase.GetAssetPath(mom.resultPrefab);
                if (prefabPth == null || prefabPth.Length == 0)
                {
                    Debug.LogError("Could not save result to prefab. Result Prefab value is not an Asset.");
                    return;
                }
                string baseName    = Path.GetFileNameWithoutExtension(prefabPth);
                string folderPath  = prefabPth.Substring(0, prefabPth.Length - baseName.Length - 7);
                string newFilename = folderPath + baseName + "-mesh";
                mom.SaveMeshsToAssetDatabase(folderPath, newFilename);

                if (mom.renderType == MB_RenderType.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.");
                }
                else
                {
                    mom.RebuildPrefab();
                }
            }
            else
            {
                Debug.LogError("Unknown parameter");
            }
        }
    }
	public static void _bakeIntoCombined(MB2_MeshBakerCommon mom, MB_OutputOptions prefabOrSceneObject){
		if (MB2_MeshCombiner.EVAL_VERSION && prefabOrSceneObject == MB_OutputOptions.bakeIntoPrefab){
			Debug.LogError("Cannot BakeIntoPrefab with evaluation version.");
			return;
		}
		if (prefabOrSceneObject != MB_OutputOptions.bakeIntoPrefab && prefabOrSceneObject != MB_OutputOptions.bakeIntoSceneObject){
			Debug.LogError("Paramater prefabOrSceneObject must be bakeIntoPrefab or bakeIntoSceneObject");
			return;
		}
		//MB2_MeshBakerCommon mom = (MB2_MeshBakerCommon) target;
		MB2_TextureBaker tb = mom.GetComponent<MB2_TextureBaker>();
		if (mom.textureBakeResults == null && tb != null){
			Debug.Log("setting results");
			mom.textureBakeResults = tb.textureBakeResults;	
		}

		if (mom.useObjsToMeshFromTexBaker){
			if (tb != null){
				mom.GetObjectsToCombine().Clear();
				mom.GetObjectsToCombine().AddRange(tb.GetObjectsToCombine());
			}	
		}
		
		Mesh mesh;
		if (!MB2_MeshBakerRoot.doCombinedValidate(mom, MB_ObjsToCombineTypes.sceneObjOnly, new MB2_EditorMethods())) return;	
		if (prefabOrSceneObject == MB_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;
		}
		mom.ClearMesh();
		mesh = mom.AddDeleteGameObjects(mom.GetObjectsToCombine().ToArray(),null,false, true);
		if (mesh != null){
			mom.Apply( Unwrapping.GenerateSecondaryUVSet );
			Debug.Log(String.Format("Successfully baked {0} meshes into a combined mesh",mom.GetObjectsToCombine().Count));
			if (prefabOrSceneObject == MB_OutputOptions.bakeIntoSceneObject){
				PrefabType pt = PrefabUtility.GetPrefabType(mom.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;
				}
			} else if (prefabOrSceneObject == MB_OutputOptions.bakeIntoPrefab){
				string prefabPth = AssetDatabase.GetAssetPath(mom.resultPrefab);
				if (prefabPth == null || prefabPth.Length == 0){
					Debug.LogError("Could not save result to prefab. Result Prefab value is not an Asset.");
					return;
				}
				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.renderType == MB_RenderType.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.");	
					
				} else {
					RebuildPrefab(mom);
				}
			} else {
				Debug.LogError("Unknown parameter");
			}
		}
		
	}