/// <param name="importerMode"> Importer mode: StreamingAssets or SpriteAnimation</param> /// <param name="path"> Can be a directory path or a file path</param> public static void ImportPictureFiles(PictureFileImporterParam.Mode importerMode, string path, StreamingImageSequencePlayableAsset targetAsset) { Assert.IsFalse(string.IsNullOrEmpty(path)); //Convert path to folder here string folder = path; FileAttributes attr = File.GetAttributes(path); if (!attr.HasFlag(FileAttributes.Directory)) { folder = Path.GetDirectoryName(folder); } string fullSrcPath = Path.GetFullPath(folder).Replace("\\", "/"); Uri fullSrcPathUri = new Uri(fullSrcPath + "/"); if (string.IsNullOrEmpty(folder)) { Debug.LogError(@"Folder is empty. Path: " + path); return; } //Enumerate all files with the supported extensions and sort List <string> relFilePaths = new List <string>(); string[] extensions = { "*." + PictureFileImporter.PNG_EXTENSION, "*." + PictureFileImporter.TGA_EXTENSION, }; foreach (string ext in extensions) { IEnumerable <string> files = Directory.EnumerateFiles(fullSrcPath, ext, SearchOption.AllDirectories); foreach (string filePath in files) { Uri curPathUri = new Uri(filePath.Replace("\\", "/")); Uri diff = fullSrcPathUri.MakeRelativeUri(curPathUri); relFilePaths.Add(diff.OriginalString); } } if (relFilePaths.Count <= 0) { EditorUtility.DisplayDialog(StreamingImageSequenceConstants.DIALOG_HEADER, @"No files in folder:: " + folder, "OK"); return; } relFilePaths.Sort(FileNameComparer); //Estimate the asset name. Use the filename without numbers at the end string assetName = EstimateAssetName(relFilePaths[0]); // set dest folder string rootDestFolder = Application.streamingAssetsPath; if (importerMode == PictureFileImporterParam.Mode.SpriteAnimation) { rootDestFolder = Application.dataPath; } //Set importer param PictureFileImporterParam importerParam = new PictureFileImporterParam { strAssetName = assetName, strSrcFolder = folder, RelativeFilePaths = relFilePaths, mode = importerMode, DoNotCopy = false, TargetAsset = targetAsset }; if (fullSrcPath.StartsWith(rootDestFolder)) { //Import immediately if the assets are already under Unity importerParam.strDstFolder = importerParam.strSrcFolder; importerParam.DoNotCopy = true; PictureFileImporter.Import(importerParam); } else { importerParam.strDstFolder = Path.Combine(rootDestFolder, assetName).Replace("\\", "/"); PictureFileImportWindow.SetParam(importerParam); PictureFileImportWindow.InitWindow(); } }
void OnGUI() { if (m_importerParam == null) { Debug.LogError("m_importerParam is null"); return; } m_isSelectingFolder = false; Rect rect = new Rect(0, 0, Screen.width, Screen.height); Rect rect2 = new Rect(2, 2, Screen.width - 4, Screen.height - 4); EditorGUI.DrawRect(rect, Color.gray); EditorGUI.DrawRect(rect2, new Color(0.3f, 0.3f, 0.3f)); EditorGUILayout.BeginVertical(); GUILayout.Space(8); GUILayout.Label(StreamingImageSequenceConstants.DIALOG_HEADER + " Importer", m_headerStyle); int numFiles = m_importerParam.RelativeFilePaths.Count; GUILayout.Label(numFiles.ToString() + " external files found in: "); GUILayout.Label(m_importerParam.strSrcFolder); m_scrollPos = EditorGUILayout.BeginScrollView(m_scrollPos, GUILayout.Width(Screen.width - 4)); if (m_importerParam.RelativeFilePaths != null) { for (int ii = 0; ii < numFiles; ii++) { EditorGUILayout.BeginHorizontal(); GUILayout.Space(16); string str = "" + ii + ":"; EditorGUILayout.LabelField(str, GUILayout.Width(40)); EditorGUILayout.LabelField(m_importerParam.RelativeFilePaths[ii], GUILayout.Width(Screen.width - 130)); GUILayout.Space(1); EditorGUILayout.EndHorizontal(); } } EditorGUILayout.EndScrollView(); GUILayout.Space(4); //Copy Toggle EditorGUILayout.BeginHorizontal(); // C#var options = new GUILayoutOption[] { GUILayout.MaxWidth(Screen.width- space), GUILayout.MinWidth(120.0F) }; EditorGUI.BeginDisabledGroup(m_importerParam.mode == PictureFileImporterParam.Mode.SpriteAnimation); string noCopyText = @"Don't copy(Use original)."; if (m_importerParam.DoNotCopy) { noCopyText += " Warning! Copying external assets inside Unity project IS recommended."; } m_importerParam.DoNotCopy = GUILayout.Toggle(m_importerParam.DoNotCopy, noCopyText, m_copyToggleStyle); EditorGUI.EndDisabledGroup(); EditorGUILayout.EndHorizontal(); GUILayout.Space(8); EditorGUILayout.BeginHorizontal(); GUILayout.Space(8); EditorGUI.BeginDisabledGroup(m_importerParam.DoNotCopy); EditorGUILayout.LabelField("Copy to:", GUILayout.Width(120)); m_importerParam.strDstFolder = EditorGUILayout.TextField(m_importerParam.strDstFolder); if (GUILayout.Button("...", GUILayout.Width(40))) { if (Directory.Exists(m_importerParam.strDstFolder)) { m_isSelectingFolder = true; m_importerParam.strDstFolder = EditorUtility.OpenFolderPanel("Choose folder to copy", m_importerParam.strDstFolder, null); } } EditorGUI.EndDisabledGroup(); EditorGUILayout.EndHorizontal(); GUILayout.Space(4); EditorGUILayout.BeginHorizontal(); GUILayout.Space(320 / 2); if (GUILayout.Button("OK")) { PictureFileImporter.Import(m_importerParam); this.Close(); } if (GUILayout.Button("Cancel")) { this.Close(); } EditorGUILayout.EndHorizontal(); EditorGUILayout.EndVertical(); }