public void SaveUMA() { avatarString = Avatar.GetAvatarDefinitionString(true); saveString = Avatar.GetCurrentRecipe(); compressedString = Avatar.GetAvatarDefinition(true).ToCompressedString("|"); asciiStringSize = Avatar.GetAvatarDefinition(true).ToASCIIString().Length; binarySize = BinaryDefinition.ToBinary(new BinaryFormatter(), Avatar.GetAvatarDefinition(true)).Length; saveStringSize = saveString.Length * 2; avatarStringSize = avatarString.Length * 2; compressedStringSize = compressedString.Length * 2; // utf-16 LoadButton.interactable = true; }
public static void ConvertToNonUMA(GameObject baseObject, UMAAvatarBase avatar, string Folder, bool ConvertNormalMaps, string CharName, bool AddStandaloneDNA) { Folder = Folder + "/" + CharName; if (!System.IO.Directory.Exists(Folder)) { System.IO.Directory.CreateDirectory(Folder); } SkinnedMeshRenderer[] renderers = avatar.umaData.GetRenderers(); int meshno = 0; foreach (SkinnedMeshRenderer smr in renderers) { Material[] mats = smr.sharedMaterials; int Material = 0; foreach (Material m in mats) { // get each texture. // if the texture has been generated (has no path) then we need to convert to Texture2D (if needed) save that asset. // update the material with that material. List <Texture> allTexture = new List <Texture>(); Shader shader = m.shader; for (int i = 0; i < ShaderUtil.GetPropertyCount(shader); i++) { if (ShaderUtil.GetPropertyType(shader, i) == ShaderUtil.ShaderPropertyType.TexEnv) { string propertyName = ShaderUtil.GetPropertyName(shader, i); Texture texture = m.GetTexture(propertyName); if (texture is Texture2D || texture is RenderTexture) { bool isNormal = false; string path = AssetDatabase.GetAssetPath(texture.GetInstanceID()); if (string.IsNullOrEmpty(path)) { if (ConvertNormalMaps) { if (propertyName.ToLower().Contains("bumpmap") || propertyName.ToLower().Contains("normal")) { // texture = ConvertNormalMap(texture); texture = sconvertNormalMap(texture); isNormal = true; } } string texName = Path.Combine(Folder, CharName + "_Mat_" + Material + propertyName + ".png"); SaveTexture(texture, texName); AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport); if (isNormal) { TextureImporter importer = (TextureImporter)TextureImporter.GetAtPath(texName); importer.isReadable = true; importer.textureType = TextureImporterType.NormalMap; importer.maxTextureSize = 1024; // or whatever importer.textureCompression = TextureImporterCompression.CompressedHQ; EditorUtility.SetDirty(importer); importer.SaveAndReimport(); } Texture2D tex = AssetDatabase.LoadAssetAtPath <Texture2D>(CustomAssetUtility.UnityFriendlyPath(texName)); m.SetTexture(propertyName, tex); } } } } string matname = Folder + "/" + CharName + "_Mat_" + Material + ".mat"; CustomAssetUtility.SaveAsset <Material>(m, matname); Material++; // Save the material to disk? // update the SMR } string meshName = Folder + "/" + CharName + "_Mesh_" + meshno + ".asset"; meshno++; // Save Mesh to disk. smr.sharedMesh.Optimize(); CustomAssetUtility.SaveAsset <Mesh>(smr.sharedMesh, meshName); smr.sharedMaterials = mats; smr.materials = mats; } // save Animator Avatar. var animator = baseObject.GetComponent <Animator>(); string avatarName = Folder + "/" + CharName + "_Avatar.asset"; CustomAssetUtility.SaveAsset <Avatar>(animator.avatar, avatarName); DestroyImmediate(avatar); var lod = baseObject.GetComponent <UMASimpleLOD>(); if (lod != null) { DestroyImmediate(lod); } if (AddStandaloneDNA) { UMAData uda = baseObject.GetComponent <UMAData>(); StandAloneDNA sda = baseObject.AddComponent <UMA.StandAloneDNA>(); sda.PackedDNA = UMAPackedRecipeBase.GetPackedDNA(uda._umaRecipe); if (avatar is DynamicCharacterAvatar) { DynamicCharacterAvatar avt = avatar as DynamicCharacterAvatar; sda.avatarDefinition = avt.GetAvatarDefinition(true); } sda.umaData = uda; } else { var ud = baseObject.GetComponent <UMAData>(); if (ud != null) { DestroyImmediate(ud); } } var ue = baseObject.GetComponent <UMAExpressionPlayer>(); if (ue != null) { DestroyImmediate(ue); } baseObject.name = CharName; string prefabName = Folder + "/" + CharName + ".prefab"; prefabName = CustomAssetUtility.UnityFriendlyPath(prefabName); PrefabUtility.SaveAsPrefabAssetAndConnect(baseObject, prefabName, InteractionMode.AutomatedAction); }