public static void createCustomBrush() { GameObject tileParentObject = new GameObject(); if (YuME_mapEditor.selectedTiles.Count > 0) { // When creating a custom brush we need to find the lowest Z transform in the selection to become the pivot transform GameObject bottomLevelOfSelection = YuME_mapEditor.selectedTiles[0]; foreach (GameObject checkObjects in YuME_mapEditor.selectedTiles) { if (checkObjects.transform.position.z < bottomLevelOfSelection.transform.position.z) { bottomLevelOfSelection = checkObjects; } } // center the parent object around the lowest block to make sure all the selected brushes are centered around the parent tileParentObject.transform.position = bottomLevelOfSelection.transform.position; // Build the brush by finding the parent prefab, creating an instance and then setting it to the transform of the original scene prefab foreach (GameObject tile in YuME_mapEditor.selectedTiles) { GameObject newBrushObject = (GameObject)PrefabUtility.InstantiatePrefab(PrefabUtility.GetPrefabParent(tile) as GameObject); newBrushObject.transform.position = tile.transform.position; newBrushObject.transform.eulerAngles = tile.transform.eulerAngles; newBrushObject.transform.localScale = tile.transform.localScale; newBrushObject.transform.parent = tileParentObject.transform; } // reset the parents position so it is zero when dropped in the scene tileParentObject.transform.position = Vector3.zero; // Add the prefab to the project tileParentObject.name = "CustomBrush" + YuTools_Utils.numberOfFilesInFolder(YuME_mapEditor.availableTileSets[YuME_mapEditor.currentTileSetIndex].customBrushDestinationFolder, "*_YuME.prefab") + "_YuME.prefab"; string destinationPath = YuTools_Utils.getAssetPath(YuME_mapEditor.availableTileSets[YuME_mapEditor.currentTileSetIndex]) + "CustomBrushes/"; PrefabUtility.CreatePrefab(destinationPath + tileParentObject.name, tileParentObject); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); // refesh the asset database to tell unity changes have been made // remove our temporary builder object DestroyImmediate(tileParentObject); YuME_mapEditor.selectedTiles.Clear(); // reload the custom brushes YuME_mapEditor.loadCustomBrushes(); } }
public static void createCustomBrush(string customBrushName) { subMeshCount = 0; YuME_tileFunctions.checkTileSelectionStatus(); GameObject tileParentObject = new GameObject(); tileParentObject.AddComponent <YuME_tileGizmo>(); tileParentObject.GetComponent <YuME_tileGizmo>().customBrushMeshName = new List <string>(); string customBrushGUID = Guid.NewGuid().ToString("N"); tileParentObject.name = customBrushName + "_YuME.prefab"; string destinationPath = YuTools_Utils.getAssetPath(YuME_mapEditor.availableTileSets[YuME_mapEditor.currentTileSetIndex]) + "CustomBrushes/"; if (YuME_mapEditor.selectedTiles.Count > 0) { // When creating a custom brush we need to find the lowest Z transform in the selection to become the pivot transform GameObject bottomLevelOfSelection = YuME_mapEditor.selectedTiles[0]; foreach (GameObject checkObjects in YuME_mapEditor.selectedTiles) { if (checkObjects.transform.position.z < bottomLevelOfSelection.transform.position.z) { bottomLevelOfSelection = checkObjects; } } // center the parent object around the lowest block to make sure all the selected brushes are centered around the parent tileParentObject.transform.position = bottomLevelOfSelection.transform.position; // New Custom Brush implementation. Uses a technique similar to the freeze map, except for selected tils List <GameObject> tilesToCombine = new List <GameObject>(); List <Transform> _freezeTiles = new List <Transform>(); List <Transform> freezeTiles = new List <Transform>(); foreach (GameObject tile in YuME_mapEditor.selectedTiles) { tile.GetComponentsInChildren <Transform>(false, _freezeTiles); freezeTiles.AddRange(_freezeTiles); } foreach (Transform tile in freezeTiles) { if (tile.GetComponent <MeshRenderer>()) { tilesToCombine.Add(tile.gameObject); } } tilesToCombine = tilesToCombine.OrderBy(x => x.GetComponent <MeshRenderer>().sharedMaterial.name).ToList(); Material previousMaterial = tilesToCombine[0].GetComponent <MeshRenderer>().sharedMaterial; List <CombineInstance> combine = new List <CombineInstance>(); CombineInstance tempCombine = new CombineInstance(); int vertexCount = 0; foreach (GameObject mesh in tilesToCombine) { vertexCount += mesh.GetComponent <MeshFilter>().sharedMesh.vertexCount; if (vertexCount > 60000) { vertexCount = 0; newSubMesh(combine, mesh.GetComponent <MeshRenderer>().sharedMaterial, tileParentObject, destinationPath, customBrushGUID); combine = new List <CombineInstance>(); } if (mesh.GetComponent <MeshRenderer>().sharedMaterial.name != previousMaterial.name) { newSubMesh(combine, previousMaterial, tileParentObject, destinationPath, customBrushGUID); combine = new List <CombineInstance>(); } tempCombine.mesh = mesh.GetComponent <MeshFilter>().sharedMesh; tempCombine.transform = mesh.GetComponent <MeshFilter>().transform.localToWorldMatrix; combine.Add(tempCombine); previousMaterial = mesh.GetComponent <MeshRenderer>().sharedMaterial; } newSubMesh(combine, previousMaterial, tileParentObject, destinationPath, customBrushGUID); tileParentObject.transform.position = Vector3.zero; // Add the prefab to the project #if UNITY_2018_3_OR_NEWER PrefabUtility.SaveAsPrefabAsset(tileParentObject, destinationPath + tileParentObject.name); #else PrefabUtility.CreatePrefab(destinationPath + tileParentObject.name, tileParentObject); #endif AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); // refesh the asset database to tell unity changes have been made // remove our temporary builder object Debug.Log("Custom Brush " + tileParentObject.name + " created."); DestroyImmediate(tileParentObject); YuME_mapEditor.selectedTiles.Clear(); // reload the custom brushes YuME_mapEditor.loadCustomBrushes(); } }