| | 1 | | using System.IO; |
| | 2 | | using UnityEditor; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | namespace DCLServices.EmotesService |
| | 6 | | { |
| | 7 | | public class GlbToPrefabConverter : Editor |
| | 8 | | { |
| | 9 | | [MenuItem("Assets/Convert GLB to Prefab", true)] |
| | 10 | | private static bool ValidateProcessGLBFile() |
| | 11 | | { |
| 0 | 12 | | Object selectedObject = Selection.activeObject; |
| | 13 | |
|
| 0 | 14 | | if (selectedObject != null) |
| | 15 | | { |
| 0 | 16 | | string assetPath = AssetDatabase.GetAssetPath(selectedObject); |
| 0 | 17 | | return Path.GetExtension(assetPath).Equals(".glb", System.StringComparison.OrdinalIgnoreCase); |
| | 18 | | } |
| | 19 | |
|
| 0 | 20 | | return false; |
| | 21 | | } |
| | 22 | |
|
| | 23 | | [MenuItem("Assets/Convert GLB to Prefab")] |
| | 24 | | private static void ProcessGLBFile() |
| | 25 | | { |
| 0 | 26 | | Object selectedObject = Selection.activeObject; |
| | 27 | |
|
| 0 | 28 | | if (selectedObject != null) |
| | 29 | | { |
| 0 | 30 | | string assetPath = AssetDatabase.GetAssetPath(selectedObject); |
| 0 | 31 | | var customAsset = AssetDatabase.LoadAssetAtPath<GameObject>(assetPath); |
| | 32 | |
|
| 0 | 33 | | GameObject assetInstance = Instantiate(customAsset); |
| | 34 | |
|
| 0 | 35 | | var assetDirectory = Path.GetDirectoryName(assetPath); |
| 0 | 36 | | var assetFolder = $"{assetDirectory}/{customAsset.name}/"; |
| | 37 | |
|
| 0 | 38 | | var path = $"{Path.GetDirectoryName(assetPath)}/{customAsset.name}.prefab"; |
| 0 | 39 | | PrefabUtility.SaveAsPrefabAsset(assetInstance, path); |
| | 40 | |
|
| 0 | 41 | | if (!Directory.Exists(assetFolder)) |
| 0 | 42 | | Directory.CreateDirectory(assetFolder); |
| | 43 | |
|
| 0 | 44 | | Object[] subAssets = AssetDatabase.LoadAllAssetsAtPath(assetPath); |
| | 45 | |
|
| 0 | 46 | | foreach (Object subAsset in subAssets) |
| | 47 | | { |
| 0 | 48 | | if (subAsset is Mesh mesh) |
| | 49 | | { |
| 0 | 50 | | string meshPath = assetFolder + mesh.name + ".asset"; |
| 0 | 51 | | AssetDatabase.RemoveObjectFromAsset(mesh); |
| 0 | 52 | | AssetDatabase.CreateAsset(mesh, AssetDatabase.GenerateUniqueAssetPath(meshPath)); |
| | 53 | | } |
| 0 | 54 | | else if (subAsset is Material material) |
| | 55 | | { |
| 0 | 56 | | string materialPath = assetFolder + material.name + ".mat"; |
| 0 | 57 | | AssetDatabase.RemoveObjectFromAsset(material); |
| 0 | 58 | | AssetDatabase.CreateAsset(material, AssetDatabase.GenerateUniqueAssetPath(materialPath)); |
| | 59 | | } |
| 0 | 60 | | else if (subAsset is Texture2D) |
| | 61 | | { |
| 0 | 62 | | Texture2D texture = subAsset as Texture2D; |
| 0 | 63 | | string texturePath = assetFolder + texture.name + ".asset"; |
| 0 | 64 | | AssetDatabase.RemoveObjectFromAsset(texture); |
| 0 | 65 | | AssetDatabase.CreateAsset(texture, AssetDatabase.GenerateUniqueAssetPath(texturePath)); |
| | 66 | | } |
| 0 | 67 | | else if (subAsset is AnimationClip animationClip) |
| | 68 | | { |
| 0 | 69 | | string animationPath = assetFolder + animationClip.name + ".anim"; |
| 0 | 70 | | AssetDatabase.RemoveObjectFromAsset(animationClip); |
| 0 | 71 | | AssetDatabase.CreateAsset(animationClip, AssetDatabase.GenerateUniqueAssetPath(animationPath)); |
| | 72 | | } |
| | 73 | | } |
| | 74 | |
|
| | 75 | | // Destroy temporary instances |
| 0 | 76 | | DestroyImmediate(assetInstance); |
| | 77 | |
|
| 0 | 78 | | AssetDatabase.Refresh(); |
| | 79 | | } |
| 0 | 80 | | } |
| | 81 | | } |
| | 82 | | } |