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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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";
private static Dictionary<string, Mesh> _uniqueMeshes = new Dictionary<string, Mesh>();
[MenuItem("GameObject/Process level", false, 0)]
public static void CreateTextArea(MenuCommand menuCommand)
{
var rootObject = (GameObject)menuCommand.context;
List<GameObject> childrenObjects = rootObject.GetChildren();
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");
}
_uniqueMeshes.Clear();
foreach (GameObject child in childrenObjects)
{
Clone(child, 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;
papa.transform.position = go.transform.position;
var clone = Instantiate(go, papa.transform);
clone.transform.localScale = Vector3.one;
clone.transform.localPosition = Vector3.zero;
PrefabUtility.SaveAsPrefabAssetAndConnect(papa, folder + @"/" + papa.name + ".prefab", InteractionMode.UserAction);
ExtractMeshes(clone, folder);
DestroyImmediate(go);
}
private static void ExtractMeshes(GameObject selectedObject, string folder)
{
MeshFilter viewedModelFilter = (MeshFilter)selectedObject.GetComponent("MeshFilter");
Renderer modelRenderer = selectedObject.GetComponent<Renderer>();
//Mesh mesh = viewedModelFilter.mesh;
Mesh mesh;// = Instantiate(viewedModelFilter.sharedMesh) as Mesh;
//Material material = selectedObject.GetComponent<Renderer>().material;
Material material = Instantiate(selectedObject.GetComponent<Renderer>().sharedMaterial) as Material;
string _meshID = GetMeshUniqueId(viewedModelFilter.sharedMesh);
if (_uniqueMeshes.ContainsKey(_meshID))
{
Debug.Log("Такой меш уже есть в словаре: <" + _meshID + ">. Пропускаем!");
_uniqueMeshes.TryGetValue(_meshID, out mesh);
}
else
{
mesh = Instantiate(viewedModelFilter.sharedMesh) as Mesh;
_uniqueMeshes.Add(_meshID, mesh);
Debug.Log(" Нужно добавить новый префаб меша: " + _meshID);
//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);
}
}
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();
}
viewedModelFilter.mesh = mesh;
selectedObject.GetComponent<Renderer>().material = material;
}
private static string GetMeshUniqueId(Mesh mesh)
{
var verticeFirst = mesh.vertices[0];
var verticeLast = mesh.vertices[mesh.vertices.Length - 1];
var res = mesh.vertexCount
+ "_" + mesh.triangles.Length
+ "_" + verticeFirst.x + "_" + verticeFirst.y + "_" + verticeFirst.z
+ "_" + verticeLast.x + "_" + verticeLast.y + "_" + verticeLast.z;
return res;
}
private static string GetMaterialUniqueID(Material mat)
{
var res = "";
return res;
}
}