< Summary

Class:DCLServices.EmotesService.GlbToPrefabConverter
Assembly:EmotesServiceEditor
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLServices/EmotesService/Utils/GlbToPrefabConverter.cs
Covered lines:0
Uncovered lines:38
Coverable lines:38
Total lines:82
Line coverage:0% (0 of 38)
Covered branches:0
Total branches:0
Covered methods:0
Total methods:2
Method coverage:0% (0 of 2)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ValidateProcessGLBFile()0%6200%
ProcessGLBFile()0%72800%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLServices/EmotesService/Utils/GlbToPrefabConverter.cs

#LineLine coverage
 1using System.IO;
 2using UnityEditor;
 3using UnityEngine;
 4
 5namespace DCLServices.EmotesService
 6{
 7    public class GlbToPrefabConverter : Editor
 8    {
 9        [MenuItem("Assets/Convert GLB to Prefab", true)]
 10        private static bool ValidateProcessGLBFile()
 11        {
 012            Object selectedObject = Selection.activeObject;
 13
 014            if (selectedObject != null)
 15            {
 016                string assetPath = AssetDatabase.GetAssetPath(selectedObject);
 017                return Path.GetExtension(assetPath).Equals(".glb", System.StringComparison.OrdinalIgnoreCase);
 18            }
 19
 020            return false;
 21        }
 22
 23        [MenuItem("Assets/Convert GLB to Prefab")]
 24        private static void ProcessGLBFile()
 25        {
 026            Object selectedObject = Selection.activeObject;
 27
 028            if (selectedObject != null)
 29            {
 030                string assetPath = AssetDatabase.GetAssetPath(selectedObject);
 031                var customAsset = AssetDatabase.LoadAssetAtPath<GameObject>(assetPath);
 32
 033                GameObject assetInstance = Instantiate(customAsset);
 34
 035                var assetDirectory = Path.GetDirectoryName(assetPath);
 036                var assetFolder = $"{assetDirectory}/{customAsset.name}/";
 37
 038                var path = $"{Path.GetDirectoryName(assetPath)}/{customAsset.name}.prefab";
 039                PrefabUtility.SaveAsPrefabAsset(assetInstance, path);
 40
 041                if (!Directory.Exists(assetFolder))
 042                    Directory.CreateDirectory(assetFolder);
 43
 044                Object[] subAssets = AssetDatabase.LoadAllAssetsAtPath(assetPath);
 45
 046                foreach (Object subAsset in subAssets)
 47                {
 048                    if (subAsset is Mesh mesh)
 49                    {
 050                        string meshPath = assetFolder + mesh.name + ".asset";
 051                        AssetDatabase.RemoveObjectFromAsset(mesh);
 052                        AssetDatabase.CreateAsset(mesh, AssetDatabase.GenerateUniqueAssetPath(meshPath));
 53                    }
 054                    else if (subAsset is Material material)
 55                    {
 056                        string materialPath = assetFolder + material.name + ".mat";
 057                        AssetDatabase.RemoveObjectFromAsset(material);
 058                        AssetDatabase.CreateAsset(material, AssetDatabase.GenerateUniqueAssetPath(materialPath));
 59                    }
 060                    else if (subAsset is Texture2D)
 61                    {
 062                        Texture2D texture = subAsset as Texture2D;
 063                        string texturePath = assetFolder + texture.name + ".asset";
 064                        AssetDatabase.RemoveObjectFromAsset(texture);
 065                        AssetDatabase.CreateAsset(texture, AssetDatabase.GenerateUniqueAssetPath(texturePath));
 66                    }
 067                    else if (subAsset is AnimationClip animationClip)
 68                    {
 069                        string animationPath = assetFolder + animationClip.name + ".anim";
 070                        AssetDatabase.RemoveObjectFromAsset(animationClip);
 071                        AssetDatabase.CreateAsset(animationClip, AssetDatabase.GenerateUniqueAssetPath(animationPath));
 72                    }
 73                }
 74
 75                // Destroy temporary instances
 076                DestroyImmediate(assetInstance);
 77
 078                AssetDatabase.Refresh();
 79            }
 080        }
 81    }
 82}