Tanoda
pb_Stl_AssetPostProcessor.cs
Go to the documentation of this file.
1using UnityEngine;
2using UnityEditor;
3using System.Linq;
4using System.Collections;
5using System.IO;
6
7namespace Parabox.STL
8{
9 public class pb_Stl_AssetPostProcessor : AssetPostprocessor
10 {
11 private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromPath)
12 {
13 foreach(string path in importedAssets.Where(x => x.EndsWith(".stl")))
14 {
15 string dir = Path.GetDirectoryName(path).Replace("\\", "/");
16 string name = Path.GetFileNameWithoutExtension(path);
17
18 Mesh[] meshes = pb_Stl_Importer.Import(path);
19
20 if(meshes == null)
21 continue;
22
23 GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
24 Material defaultDiffuse = cube.GetComponent<MeshRenderer>().sharedMaterial;
25 GameObject.DestroyImmediate(cube);
26
27 string prefab_path = string.Format("{0}/{1}.prefab", dir, name);
28
29#if UNITY_4_7
30 GameObject prefab_source = (GameObject) AssetDatabase.LoadAssetAtPath(prefab_path, typeof(GameObject));
31#else
32 GameObject prefab_source = AssetDatabase.LoadAssetAtPath<GameObject>(prefab_path);
33#endif
34 GameObject prefab = new GameObject();
35 prefab.name = name;
36 if(prefab_source == null)
37 prefab_source = PrefabUtility.CreatePrefab(prefab_path, prefab);
38 GameObject.DestroyImmediate(prefab);
39
40 Object[] children = AssetDatabase.LoadAllAssetsAtPath(prefab_path);
41
42 for(int i = 0; i < children.Length; i++)
43 {
44 if(AssetDatabase.IsSubAsset(children[i]))
45 GameObject.DestroyImmediate(children[i], true);
46 }
47
48 for(int i = 0; i < meshes.Length; i++)
49 AssetDatabase.AddObjectToAsset(meshes[i], prefab_source);
50
51 children = AssetDatabase.LoadAllAssetsAtPath(prefab_path);
52 GameObject render = new GameObject();
53
54 for(int i = 0; i < children.Length; i++)
55 {
56 Mesh m = children[i] as Mesh;
57 if(m == null) continue;
58 GameObject child = new GameObject();
59 child.name = string.Format("{0} ({1})", name, i);
60 m.name = child.name;
61 child.AddComponent<MeshFilter>().sharedMesh = m;
62 child.AddComponent<MeshRenderer>().sharedMaterial = defaultDiffuse;
63 child.transform.SetParent(render.transform, false);
64 }
65
66 PrefabUtility.ReplacePrefab(render, prefab_source, ReplacePrefabOptions.ReplaceNameBased);
67
68 GameObject.DestroyImmediate(render);
69 }
70 }
71
72 public static void CreateMeshAssetWithPath(string path)
73 {
74 string dir = Path.GetDirectoryName(path).Replace("\\", "/");
75 string name = Path.GetFileNameWithoutExtension(path);
76
77 Mesh[] meshes = pb_Stl_Importer.Import(path);
78
79 if(meshes == null)
80 return;
81
82 for(int i = 0; i < meshes.Length; i++)
83 AssetDatabase.CreateAsset(meshes[i], string.Format("{0}/{1}{2}.asset", dir, name, i));
84 }
85
86 [MenuItem("Tools/Force Import &d")]
87 static void ditos()
88 {
89 foreach(Object o in Selection.objects)
90 {
91 CreateMeshAssetWithPath(AssetDatabase.GetAssetPath(o));
92 }
93 }
94 }
95}
static void CreateMeshAssetWithPath(string path)
UnityEngine.Object Object