using System Collections using System Collections Generic using UnityE

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class HolebusterProcessor : MonoBehaviour
{
private static string _progressTitle = "Extracting Meshes";
private static string _sourceExtension = ".prefab";
private static string _targetExtension = ".asset";
[MenuItem("GameObject/Process level", false, 0)]
public static void CreateTextArea(MenuCommand menuCommand)
{
var rootObject = (GameObject)menuCommand.context;
string selectedPath = EditorUtility.OpenFolderPanel("Folder to export", "Assets", "");
if (selectedPath.IndexOf(Application.dataPath) != 0)
{
EditorUtility.DisplayDialog("Error", "Please select a folder inside project", "OK");
return;
}
// Здесь загружаем данные об уже имеющихся моделях и сохраняем данные о них
var path = selectedPath.Replace(Application.dataPath, "Assets");
if (AssetDatabase.IsValidFolder(path + @"/Prefabs") == false)
{
AssetDatabase.CreateFolder(path, "Prefabs");
}
// Здесь проходим циклом по всем моделям внутри нашего объекта
// Проверяем что подходящего префаба нет (Если он есть тогда удаляем объект и ставим в его координатах нужный префаб)
// Если префаба нет то сначала сохраняем его (А также при необходимости меш и материал) в нужной папке и потом заменяем объект префабом.
Clone(rootObject, path + @"/Prefabs");
}
private static void Clone(GameObject go, string folder)
{
var transform = go.transform;
var papa = new GameObject();
papa.transform.SetParent(transform.parent);
papa.name = go.name.Replace(" ", "");
papa.transform.localScale = transform.localScale;
var clone = Instantiate(go, papa.transform);
var mesh = clone.GetComponent<MeshFilter>().sharedMesh;
clone.transform.localScale = Vector3.one;
var verticalPosition = (mesh.bounds.extents.y - mesh.bounds.center.y) / 2;
clone.transform.localPosition = Vector3.up * verticalPosition;
PrefabUtility.SaveAsPrefabAssetAndConnect(papa, folder + @"/" + papa.name + ".prefab", InteractionMode.UserAction);
ExtractMeshes(clone, folder);
}
private static void ExtractMeshes(GameObject selectedObject, string folder)
{
//Create Folder Hierarchy
string selectedObjectPath = AssetDatabase.GetAssetPath(selectedObject);
//string parentfolderPath = selectedObjectPath.Substring(0, selectedObjectPath.Length - (selectedObject.name.Length + 5));
string objectFolderName = selectedObject.name;
string objectFolderPath = folder + "/" + objectFolderName;
string meshFolderName = "Meshes";
string meshFolderPath = objectFolderPath + "/" + meshFolderName;
string materialFolderName = "Material";
string materialFolderPath = objectFolderPath + "/" + materialFolderName;
if (!AssetDatabase.IsValidFolder(objectFolderPath))
{
AssetDatabase.CreateFolder(folder, objectFolderName);
if (!AssetDatabase.IsValidFolder(meshFolderPath))
{
AssetDatabase.CreateFolder(objectFolderPath, meshFolderName);
}
if (!AssetDatabase.IsValidFolder(materialFolderPath))
{
AssetDatabase.CreateFolder(objectFolderPath, materialFolderName);
}
}
MeshFilter viewedModelFilter = (MeshFilter)selectedObject.GetComponent("MeshFilter");
Mesh mesh = viewedModelFilter.mesh;
Material material = selectedObject.GetComponent<Renderer>().material;
AssetDatabase.CreateAsset(mesh, meshFolderPath + "/" + selectedObject.name.Trim() + _targetExtension);
AssetDatabase.CreateAsset(material, materialFolderPath + "/" + selectedObject.name.Trim() + _targetExtension);
//Cleanup
AssetDatabase.MoveAsset(selectedObjectPath, objectFolderPath + "/" + selectedObject.name + _sourceExtension);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
}