private void displayInputInfos() { GUILayout.Label("Import a glTF (*.gltf, *.glb, *.zip)", _ui.sketchfabModelName); _ui.displaySubContent("Source file:"); GUILayout.BeginHorizontal(); Color backup = GUI.color; if (_importFilePath == _sourceFileHint) { GUI.contentColor = Color.red; } GUILayout.TextField(_importFilePath, GUILayout.MinWidth(UI_SIZE.x), GUILayout.Height(UI_SIZE.y)); GUI.contentColor = backup; GUILayout.FlexibleSpace(); if (GUILayout.Button("Select file", GUILayout.Height(UI_SIZE.y), GUILayout.Width(minWidthButton))) { _importFilePath = EditorUtility.OpenFilePanel("Choose gltf or zip file", "", "zip,gltf,glb"); string modelfileName = Path.GetFileNameWithoutExtension(_importFilePath); _importDirectory = GLTFUtils.unifyPathSeparator(Path.Combine(_defaultImportDirectory, modelfileName)); _currentSampleName = modelfileName; } GUILayout.EndHorizontal(); }
// File serialization public Mesh saveMesh(Mesh mesh, string objectName = "Scene") { string baseName = GLTFUtils.cleanName(objectName + ".asset"); string fullPath = Path.Combine(_importMeshesDirectory, baseName); string meshProjectPath = GLTFUtils.getPathProjectFromAbsolute(fullPath); serializeAsset(mesh, meshProjectPath, fullPath, true); return(AssetDatabase.LoadAssetAtPath(meshProjectPath, typeof(Mesh)) as Mesh); }
private void LoadClip(Sein_audioClipsExtension.AudioClip clip, string gltfPath, string basePath, int i) { var uri = Path.Combine(gltfPath, clip.uri); if (clip.uri != null && File.Exists(uri)) { var tmp = clip.uri.Split('/'); var name = tmp[tmp.Length - 1]; if (clip.name != null) { name = clip.name; } var path = Path.Combine(basePath, name); if (File.Exists(path)) { if (!IMPORTED_URIS.Contains(clip.uri)) { name = Path.GetFileNameWithoutExtension(name) + "-" + i + Path.GetExtension(name); path = Path.Combine(basePath, name); FileUtil.CopyFileOrDirectory(uri, path); IMPORTED_URIS.Add(clip.uri); } } else { FileUtil.CopyFileOrDirectory(uri, path); IMPORTED_URIS.Add(clip.uri); } AssetDatabase.Refresh(); var unityClip = AssetDatabase.LoadAssetAtPath <AudioClip>(path); var directory = GLTFUtils.getPathProjectFromAbsolute(basePath); path = Path.Combine(directory, name + ".asset"); var seinClip = ScriptableObject.CreateInstance <SeinAudioClip>(); seinClip.clip = unityClip; seinClip.mode = clip.mode; seinClip.isLazy = clip.isLazy; AssetDatabase.CreateAsset(seinClip, path); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); seinClip = AssetDatabase.LoadAssetAtPath <SeinAudioClip>(path); IMPORTED_CLIPS.Add(seinClip); } else { Debug.LogWarning("Audio clip not found"); } }
public Material saveMaterial(Material material, int index) { string baseName = generateName(material.name.Length > 0 ? material.name.Trim() : "material", index) + ".mat"; string materialAssetPath = Path.Combine(_importMaterialsDirectory, baseName); string materialProjectPath = GLTFUtils.getPathProjectFromAbsolute(materialAssetPath); serializeAsset(material, materialProjectPath, materialAssetPath, true); // Reload as asset return((Material)AssetDatabase.LoadAssetAtPath(materialProjectPath, typeof(Material))); }
public Texture2D saveTexture(Texture2D texture, int index = -1, string imageName = "") { string basename = GLTFUtils.cleanName(texture.name + (index >= 0 ? "_" + index.ToString() : "") + ".png"); // Extension will be overridden string fullPath = Path.Combine(_importTexturesDirectory, basename); // Write texture string newAssetPath = GLTFTextureUtils.writeTextureOnDisk(texture, fullPath, true); // Reload as asset string projectPath = GLTFUtils.getPathProjectFromAbsolute(newAssetPath); Texture2D tex = (Texture2D)AssetDatabase.LoadAssetAtPath(projectPath, typeof(Texture2D)); _parsedImages.Add(tex); return(tex); }
private void handleDragNDrop() { DragAndDrop.visualMode = DragAndDropVisualMode.Generic; if (Event.current.type == EventType.DragExited) { if (DragAndDrop.paths.Length > 0) { _importFilePath = DragAndDrop.paths[0]; string modelfileName = Path.GetFileNameWithoutExtension(_importFilePath); _importDirectory = GLTFUtils.unifyPathSeparator(Path.Combine(_defaultImportDirectory, modelfileName)); _currentSampleName = modelfileName; } } }
private void displayImportButton() { GUILayout.BeginHorizontal(); Color old = GUI.color; GUI.color = GlTFUI.SKFB_BLUE; GUI.contentColor = Color.white; GUI.enabled = GLTFUtils.isFolderInProjectDirectory(_importDirectory) && File.Exists(_importFilePath); if (GUILayout.Button("IMPORT", _ui.GlTFButton)) { processImportButton(); } GUI.color = old; GUI.enabled = true; GUILayout.EndHorizontal(); }
public void loadFromBuffer(byte[] data) { if (!GLTFUtils.isFolderInProjectDirectory(_importDirectory)) { Debug.LogError("Import directory is outside of project directory. Please select path in Assets/Resources/"); return; } if (!Directory.Exists(_importDirectory)) { Directory.CreateDirectory(_importDirectory); } _gltfInput = unzipGLTFArchiveData(data); _importer.setupForPath(_gltfInput, _importDirectory, _currentSampleName, _addToCurrentScene, _generateLightMapUvs); _importer.Load(); }
// https://docs.unity3d.com/ScriptReference/Animations.AnimatorController.html public void createAnimatorAsset(List <AnimationClip> clips) { string animatorname = getPrefabName(_prefabName); string animatorPath = GLTFUtils.getPathProjectFromAbsolute(_importAnimationDirectory + "/" + animatorname + ".controller"); var controller = AnimatorController.CreateAnimatorControllerAtPath(animatorPath); foreach (var clip in clips) { Motion motion = (Motion)clip; controller.AddMotion(motion); } if (_animatorController == null) { _animatorController = controller; } }
public void saveAnimationClip(AnimationClip clip) { if (_importAnimationDirectory == null) { createAnimationDirectory(); } var directory = GLTFUtils.getPathProjectFromAbsolute(_importAnimationDirectory); var path = directory + "/" + clip.name + ".anim"; if (File.Exists(path)) { int i = 1; while (true) { var newDir = directory + "/" + i; path = newDir + "/" + clip.name + ".anim"; if (!Directory.Exists(newDir)) { Directory.CreateDirectory(newDir); break; } if (!File.Exists(path)) { break; } } } AssetDatabase.CreateAsset(clip, path); AssetDatabase.Refresh(); // Add animation to animator AnimationClip loadedClip = AssetDatabase.LoadAssetAtPath <AnimationClip>(path); loadedClip.wrapMode = WrapMode.Loop; }
public void configure(string importDirectory, string prefabName, bool addToScene = false, bool generateLightMapUvs = false) { if (importDirectory.Length > 0) { if (!GLTFUtils.isFolderInProjectDirectory(importDirectory)) { Debug.LogError("Import directory in not in Assets/Resources"); } else { _importDirectory = importDirectory; } } if (prefabName.Length > 0) { _currentSampleName = prefabName; } _addToCurrentScene = addToScene; _generateLightMapUvs = generateLightMapUvs; }
private string unzipGltfArchive(string zipPath) { if (!Directory.Exists(_unzipDirectory)) { Directory.CreateDirectory(_unzipDirectory); } // Clean previously unzipped files GLTFUtils.removeFileList(_unzippedFiles.ToArray()); string gltfFile = findGltfFile(); if (gltfFile != "") { File.Delete(gltfFile); } // Extract archive ZipFile zipfile = ZipFile.Read(zipPath); zipfile.ExtractAll(_unzipDirectory, ExtractExistingFileAction.OverwriteSilently); return(findGltfFile()); }
private void displayImportDirectory() { _ui.displaySubContent("Import into"); GUILayout.BeginHorizontal(); _importDirectory = GUILayout.TextField(_importDirectory, GUILayout.MinWidth(UI_SIZE.x), GUILayout.Height(UI_SIZE.y)); GUILayout.FlexibleSpace(); if (GUILayout.Button("Change destination", GUILayout.Height(UI_SIZE.y), GUILayout.Width(minWidthButton))) { string newImportDir = EditorUtility.OpenFolderPanel("Choose import directory", _importDirectory, ""); if (newImportDir == "") { } else if (GLTFUtils.isFolderInProjectDirectory(newImportDir)) { Config.SetImportPath(newImportDir); } else { EditorUtility.DisplayDialog("Error", "Please select a path within your current Unity project (with Assets/)", "Ok"); } } GUILayout.EndHorizontal(); }
public void hardClean() { softClean(); for (int i = 0; i < _createdGameObjects.Count; ++i) { Object.DestroyImmediate(_createdGameObjects[i]); } GLTFUtils.removeFileList(_generatedFiles.ToArray()); AssetDatabase.Refresh(); // also triggers Resources.UnloadUnusedAssets() // Remove directories if empty GLTFUtils.removeEmptyDirectory(_importMeshesDirectory); GLTFUtils.removeEmptyDirectory(_importTexturesDirectory); GLTFUtils.removeEmptyDirectory(_importMaterialsDirectory); GLTFUtils.removeEmptyDirectory(_importAnimationDirectory); _createdGameObjects.Clear(); AssetDatabase.Refresh(); // also triggers Resources.UnloadUnusedAssets() GLTFUtils.removeEmptyDirectory(_importDirectory); AssetDatabase.Refresh(); // also triggers Resources.UnloadUnusedAssets() }
private static Texture2D SplitRoughnessTexture(Texture2D texture) { int width = texture.width; int height = texture.height; string assetPath = AssetDatabase.GetAssetPath(texture); TextureImporter im = AssetImporter.GetAtPath(assetPath) as TextureImporter; im.isReadable = true; im.SaveAndReimport(); var iColor = texture.GetPixels(); im.isReadable = false; im.SaveAndReimport(); // Let's consider that the three textures have the same resolution Color[] colors = new Color[width * height]; for (int i = 0; i < colors.Length; i += 1) { float a = 1 - iColor[i].a; colors[i] = new Color(a, a, a); } var res = new Texture2D(width, height); res.SetPixels(colors); string basename = Path.GetFileNameWithoutExtension(assetPath) + "_rg"; string fullPath = Path.GetFullPath(Path.GetDirectoryName(assetPath)) + "/" + basename + ".png"; string newAssetPath = GLTFTextureUtils.writeTextureOnDisk(res, fullPath, true); string projectPath = GLTFUtils.getPathProjectFromAbsolute(newAssetPath); Texture2D tex = (Texture2D)AssetDatabase.LoadAssetAtPath(projectPath, typeof(Texture2D)); return(tex); }
public override void Import(EditorImporter importer, GameObject gameObject, Node gltfNode, Extension extension) { var seinAnimator = (Sein_animatorExtension)extension; var id = ""; for (var i = 0; i < seinAnimator.modelAnimations.Length; i += 1) { var prefix = (seinAnimator.prefixes != null && seinAnimator.prefixes.Length > i) ? seinAnimator.prefixes[i] : seinAnimator.prefix; var name = seinAnimator.modelAnimations[i]; if (prefix != null && prefix.Length > 0) { id += prefix + "@" + name; } else { id += name; } } AnimatorController controller; if (IMPORTED_CONTROLLERS.ContainsKey(id)) { controller = IMPORTED_CONTROLLERS[id]; } else { var controllerName = seinAnimator.name != null && seinAnimator.name != "" ? seinAnimator.name : gameObject.name; var controllerPath = Path.Combine(new string[] { importer.importDirectoryPath, "animations", controllerName + ".controller" }); if (File.Exists(controllerPath)) { int index = 1; while (true) { controllerPath = Path.Combine(new string[] { importer.importDirectoryPath, "animations", controllerName + "-" + index + ".controller" }); if (!File.Exists(controllerPath)) { break; } } } controller = AnimatorController.CreateAnimatorControllerAtPath(GLTFUtils.getPathProjectFromAbsolute(controllerPath)); for (var i = 0; i < seinAnimator.modelAnimations.Length; i += 1) { var prefix = (seinAnimator.prefixes != null && seinAnimator.prefixes.Length > i) ? seinAnimator.prefixes[i] : seinAnimator.prefix; var name = seinAnimator.modelAnimations[i]; if (prefix != null && prefix.Length > 0) { name = prefix + "@" + name; } if (!IMPORTED_CLIPS.ContainsKey(name)) { continue; } var clip = IMPORTED_CLIPS[name]; controller.AddMotion((Motion)clip); var nodePath = AnimationUtility.CalculateTransformPath(gameObject.transform, importer.sceneObject.transform); var temp = new List <Temp>(); foreach (var binding in AnimationUtility.GetCurveBindings(clip)) { var path = binding.path; var realPath = path.Substring(nodePath.Length); var propertyName = binding.propertyName; var type = binding.type; var curve = AnimationUtility.GetEditorCurve(clip, binding); temp.Add(new Temp { path = realPath, propertyName = propertyName, type = type, curve = curve }); } clip.ClearCurves(); foreach (var binding in temp) { clip.SetCurve(binding.path, binding.type, binding.propertyName, binding.curve); } } } var animator = gameObject.AddComponent <Animator>(); animator.runtimeAnimatorController = controller; }
public GameObject savePrefab(GameObject sceneObject, string _importDirectory, bool instanciateInScene = false) { string baseName = getPrefabName(sceneObject.name) + ".prefab"; string fullPath = Path.Combine(_importDirectory, baseName); string prefabPathInProject = GLTFUtils.getPathProjectFromAbsolute(fullPath); if (File.Exists(fullPath)) { var list = new List <GameObjectAndPath>(); var temp = new List <GameObjectAndPath> { new GameObjectAndPath { path = new List <string>(), obj = sceneObject } }; var children = new List <GameObjectAndPath>(); var prefabResPath = prefabPathInProject.Replace("Assets/Resources/", "").Replace(".prefab", ""); GameObject originPrefab = Resources.Load(prefabResPath) as GameObject; // Get all children and path while (true) { children.Clear(); temp.ForEach(item => children.Add(item)); temp.Clear(); if (children.Count == 0) { break; } foreach (GameObjectAndPath child in children) { list.Add(child); foreach (Transform tr in child.obj.transform) { var obj = tr.gameObject; var path = new List <string>(child.path); path.Add(obj.name); temp.Add(new GameObjectAndPath { path = path, obj = obj }); } } } // 遍历并更新新prefab中物体的transform和组件 // 最终保存新的的prefab // 之后重新绑定prefab copyComponets(originPrefab, sceneObject); list.RemoveAt(0); foreach (GameObjectAndPath child in list) { var tr = originPrefab.transform.Find(string.Join("/", child.path.ToArray())); if (tr == null) { continue; } var origObj = tr.gameObject; var obj = child.obj; copyComponets(origObj, obj); } File.Delete(fullPath); File.Delete(fullPath + ".meta"); AssetDatabase.Refresh(); } if (_animatorController != null) { var animator = sceneObject.GetComponent <Animator>(); if (animator == null && !hasAnimatorExtension) { animator = sceneObject.AddComponent <Animator>(); animator.runtimeAnimatorController = _animatorController; } } _generatedFiles.Add(fullPath); return(PrefabUtility.CreatePrefab(prefabPathInProject, sceneObject)); }
public string getPrefabName(string sceneObjectName) { return(GLTFUtils.cleanName(sceneObjectName.Length > 0 ? sceneObjectName : "GlTF")); }
public void cleanArtifacts() { GLTFUtils.removeFileList(_unzippedFiles.ToArray()); }
public void OnDestroy() { GLTFUtils.removeFileList(_unzippedFiles.ToArray()); GLTFUtils.removeEmptyDirectory(_unzipDirectory); }
public override void Import(EditorImporter importer, GameObject gameObject, Node gltfNode, Extension extension) { var seinAnimator = (Sein_animatorExtension)extension; var id = ""; for (var i = 0; i < seinAnimator.modelAnimations.Length; i += 1) { var prefix = (seinAnimator.prefixes != null && seinAnimator.prefixes.Length > i) ? seinAnimator.prefixes[i] : seinAnimator.prefix; var name = seinAnimator.modelAnimations[i]; if (prefix != null && prefix.Length > 0) { id += prefix + "@" + name; } else { id += name; } } AnimatorController controller; if (IMPORTED_CONTROLLERS.ContainsKey(id)) { controller = IMPORTED_CONTROLLERS[id]; } else { var controllerName = seinAnimator.name != null && seinAnimator.name != "" ? seinAnimator.name : gameObject.name; var controllerPath = Path.Combine(new string[] { importer.importDirectoryPath, "animations", controllerName + ".controller" }); if (File.Exists(controllerPath)) { int index = 1; while (true) { controllerPath = Path.Combine(new string[] { importer.importDirectoryPath, "animations", controllerName + "-" + index + ".controller" }); if (!File.Exists(controllerPath)) { break; } } } controller = AnimatorController.CreateAnimatorControllerAtPath(GLTFUtils.getPathProjectFromAbsolute(controllerPath)); for (var i = 0; i < seinAnimator.modelAnimations.Length; i += 1) { var prefix = (seinAnimator.prefixes != null && seinAnimator.prefixes.Length > i) ? seinAnimator.prefixes[i] : seinAnimator.prefix; var name = seinAnimator.modelAnimations[i]; if (prefix != null && prefix.Length > 0) { name = prefix + "@" + name; } if (!IMPORTED_CLIPS.ContainsKey(name)) { continue; } controller.AddMotion((Motion)IMPORTED_CLIPS[name]); } } var animator = gameObject.AddComponent <Animator>(); animator.runtimeAnimatorController = controller; }
public string generateName(string name, int index) { return(GLTFUtils.cleanName(name + "_" + index).Replace(":", "_")); }