1 // Copyright 2017 The Draco Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 using System;
16 using System.Collections;
17 using System.Collections.Generic;
18 using UnityEditor;
19 using UnityEngine;
20 
21 public class DracoFileImporter : AssetPostprocessor {
OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)22   static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets,
23       string[] movedAssets, string[] movedFromAssetPaths) {
24     foreach(string str in importedAssets) {
25       // Compressed file must be renamed to ".drc.bytes".
26       if (str.IndexOf(".drc.bytes") == -1) {
27         return;
28       }
29 
30       // If the original mesh exceeds the limit of number of verices, the
31       // loader will split it to a list of smaller meshes.
32       List<Mesh> meshes = new List<Mesh>();
33       DracoMeshLoader dracoLoader = new DracoMeshLoader();
34 
35       // The decoded mesh will be named without ".drc.bytes"
36       str.LastIndexOf('/');
37       int length = str.Length - ".drc.bytes".Length - str.LastIndexOf('/') - 1;
38       string fileName = str.Substring(str.LastIndexOf('/') + 1, length);
39 
40       int numFaces = dracoLoader.LoadMeshFromAsset(fileName + ".drc", ref meshes);
41       if (numFaces > 0) {
42         // Create mesh assets. Combine the smaller meshes to a single asset.
43         // TODO: Figure out how to combine to an unseen object as .obj files.
44         AssetDatabase.CreateAsset (meshes [0], "Assets/Resources/" + fileName + ".asset");
45         AssetDatabase.SaveAssets ();
46         for (int i = 1; i < meshes.Count; ++i) {
47           AssetDatabase.AddObjectToAsset(meshes [i], meshes [0]);
48           AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath (meshes [i]));
49         }
50 
51         // Also create a Prefab for easy usage.
52         GameObject newAsset = new GameObject();
53         newAsset.hideFlags = HideFlags.HideInHierarchy;
54         for (int i = 0; i < meshes.Count; ++i) {
55           GameObject subObject = new GameObject();
56           subObject.hideFlags = HideFlags.HideInHierarchy;
57           subObject.AddComponent<MeshFilter>();
58           subObject.AddComponent<MeshRenderer>();
59           subObject.GetComponent<MeshFilter>().mesh =
60             UnityEngine.Object.Instantiate(meshes[i]);
61           subObject.transform.parent = newAsset.transform;
62         }
63         PrefabUtility.CreatePrefab("Assets/Resources/" + fileName + ".prefab", newAsset);
64       } else {
65         // TODO: Throw exception?
66         Debug.Log("Error: Decodeing Draco file failed.");
67       }
68     }
69   }
70 }
71