#if false using System; using System.Collections.Generic; using System.IO; using System.Linq; using UniGLTF; using UnityEditor; using UnityEngine; namespace VRM { public static class VRMAssetWriter { interface ISubAssetWriter { void WriteIfHas(GameObject go); } class SubAssetWriter: ISubAssetWriter where T : UnityEngine.Object { HashSet m_set = new HashSet(); String m_assetPath; public delegate IEnumerable GetterFunc(GameObject go); GetterFunc m_getter; public SubAssetWriter(string assetPath, GetterFunc getter) { m_assetPath = assetPath; m_getter = getter; } public void WriteIfHas(GameObject go) { foreach(var o in m_getter(go)) { if (!m_set.Contains(o)) { AssetDatabase.AddObjectToAsset(o, m_assetPath); m_set.Add(o); } } } } static IEnumerable GetMeshs(GameObject go) { var skinnedMesh = go.GetComponent(); if (skinnedMesh != null) { yield return skinnedMesh.sharedMesh; } var filter = go.GetComponent(); if (filter != null) { yield return filter.sharedMesh; } } static IEnumerable GetMaterials(GameObject go) { var renderer = go.GetComponent(); if (renderer != null) { return renderer.sharedMaterials; } else { return Enumerable.Empty(); } } static IEnumerable GetTextures(GameObject go) { foreach (var m in GetMaterials(go)) { foreach(Texture2D x in m.GetTextures()) { if (x != null) { yield return x; } } } } static IEnumerable GetAvatars(GameObject go) { var animator = go.GetComponent(); if(animator!=null && animator.avatar != null) { yield return animator.avatar; } } static IEnumerable GetBlendShapeClips(GameObject go) { var proxy = go.GetComponent(); if (proxy != null && proxy.BlendShapeAvatar != null) { return proxy.BlendShapeAvatar.Clips; } else { return Enumerable.Empty(); } } static IEnumerable GetBlendShapeAvatars(GameObject go) { var proxy = go.GetComponent(); if (proxy != null && proxy.BlendShapeAvatar != null) { yield return proxy.BlendShapeAvatar; } } static IEnumerable GetAvatarDecriptions(GameObject go) { var humanoid = go.GetComponent(); if (humanoid!=null && humanoid.Description != null) { yield return humanoid.Description; } else { var animator = go.GetComponent(); if(animator!=null && animator.avatar) { var description= UniHumanoid.AvatarDescription.CreateFrom(animator.avatar); if (description != null) { description.name = "AvatarDescription"; yield return description; } } } } static IEnumerable GetThumbnails(GameObject go) { var meta = go.GetComponent(); if (meta != null && meta.Thumbnail != null) { yield return meta.Thumbnail; } } static IEnumerable GetSubAssets(String prefabPath) { return AssetDatabase.LoadAllAssetsAtPath(prefabPath); } public static void SaveAsPrefab(GameObject root, String path) { var prefabPath = path.ToUnityRelativePath(); Debug.LogFormat("SaveAsPrefab: {0}", prefabPath); // clear subassets if (File.Exists(prefabPath)) { //Debug.LogFormat("Exist: {0}", m_prefabPath); // clear subassets foreach (var x in GetSubAssets(prefabPath)) { if (x is Transform || x is GameObject) { continue; } GameObject.DestroyImmediate(x, true); } } // add subassets var writers = new ISubAssetWriter[]{ new SubAssetWriter(prefabPath, GetTextures), new SubAssetWriter(prefabPath, GetMaterials), new SubAssetWriter(prefabPath, GetMeshs), new SubAssetWriter(prefabPath, GetAvatars), // VRM Objects new SubAssetWriter(prefabPath, GetBlendShapeClips), new SubAssetWriter(prefabPath, GetBlendShapeAvatars), new SubAssetWriter(prefabPath, GetAvatarDecriptions), new SubAssetWriter(prefabPath, GetThumbnails), }; foreach (var x in root.transform.Traverse()) { foreach (var writer in writers) { writer.WriteIfHas(x.gameObject); } } /// /// create prefab, after subasset AssetDatabase.AddObjectToAsset /// if (File.Exists(prefabPath)) { //Debug.LogFormat("ReplacePrefab: {0}", m_prefabPath); var prefab = AssetDatabase.LoadAssetAtPath(prefabPath); PrefabUtility.ReplacePrefab(root, prefab, ReplacePrefabOptions.ConnectToPrefab); } else { //Debug.LogFormat("CreatePrefab: {0}", m_prefabPath); PrefabUtility.CreatePrefab(prefabPath, root, ReplacePrefabOptions.ConnectToPrefab); } } } } #endif