//---------------------------------------------------------------------------------------------------------------------- private static void InitializeAssetFromDefaultAsset(StreamingImageSequencePlayableAsset playableAsset, UnityEditor.DefaultAsset timelineDefaultAsset) { string path = AssetDatabase.GetAssetPath(timelineDefaultAsset).Replace("\\", "/"); const bool ASK_TO_COPY = false; ImageSequenceImporter.ImportImages(path, playableAsset, ASK_TO_COPY); }
private static void RegisterFilesAndCreateStreamingImageSequence() { string path = EditorUtility.OpenFilePanel("Open File", "", PNG_EXTENSION + "," + TGA_EXTENSION); if (string.IsNullOrEmpty(path)) { return; } ImageSequenceImporter.ImportImages(path, null); }
//---------------------------------------------------------------------------------------------------------------------- /// Import images in the path to create StreamingImageSequence assets with those images /// <param name="path"> Can be a directory path or a file path</param> /// <param name="targetAsset"> The target asset where the images are assigned to</param> /// <param name="askToCopy"> Ask to copy if path is not under StreamingAssets. Default to true</param> internal static void ImportImages(string path, StreamingImageSequencePlayableAsset targetAsset, bool askToCopy = true) { Assert.IsFalse(string.IsNullOrEmpty(path)); FindFolderAndImages(path, out string folder, out List <string> relFilePaths); if (relFilePaths.Count <= 0) { EditorUtility.DisplayDialog(StreamingImageSequenceConstants.DIALOG_HEADER, @"No files in folder:: " + folder, "OK"); return; } //Estimate the asset name. Use the filename without numbers at the end string assetName = EstimateAssetName(relFilePaths[0]); // set dest folder string streamingAssetsPath = AssetEditorUtility.NormalizeAssetPath(Application.streamingAssetsPath); //Set importer param ImageFileImporterParam importerParam = new ImageFileImporterParam { strSrcFolder = folder, RelativeFilePaths = relFilePaths, CopyToStreamingAssets = true, TargetAsset = targetAsset }; //Import immediately if the assets are already under StreamingAssets if (folder.StartsWith(streamingAssetsPath) || !askToCopy) { importerParam.strDstFolder = importerParam.strSrcFolder; importerParam.CopyToStreamingAssets = false; ImageSequenceImporter.Import(importerParam); } else { importerParam.strDstFolder = Path.Combine(streamingAssetsPath, assetName).Replace("\\", "/"); ImageSequenceImportWindow.Show(importerParam); } }
//---------------------------------------------------------------------------------------------------------------------- void OnGUI() { if (m_importerParam == null) { Debug.LogError("m_importerParam is null"); return; } InitStyles(); 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); if (m_importerParam.RelativeFilePaths != null) { int numFiles = m_importerParam.RelativeFilePaths.Count; GUILayout.Label(numFiles.ToString() + " external files found in: "); GUILayout.Label(m_importerParam.strSrcFolder); const int SCROLL_VIEW_COUNT = 16; const int SCROLL_ITEM_HEIGHT = 12; const int TOP_MARGIN = 12; int numDigits = (int)Math.Floor(Math.Log10(numFiles) + 1); m_scrollPos = DrawScrollView(m_scrollPos, TOP_MARGIN, numFiles, SCROLL_VIEW_COUNT, SCROLL_ITEM_HEIGHT, (int index) => { GUILayout.Space(30); string indexStr = index.ToString(); indexStr = indexStr.PadLeft(numDigits - indexStr.Length); string str = indexStr + ":"; EditorGUILayout.LabelField(str, GUILayout.Width(40)); EditorGUILayout.LabelField(m_importerParam.RelativeFilePaths[index]); }); } GUILayout.Space(4); //Copy Toggle EditorGUILayout.BeginHorizontal(); // C#var options = new GUILayoutOption[] { GUILayout.MaxWidth(Screen.width- space), GUILayout.MinWidth(120.0F) }; string copyText = @"Copy to StreamingAssets (Recommended)"; m_importerParam.CopyToStreamingAssets = GUILayout.Toggle(m_importerParam.CopyToStreamingAssets, copyText); EditorGUILayout.EndHorizontal(); GUILayout.Space(8); EditorGUILayout.BeginHorizontal(); GUILayout.Space(8); EditorGUI.BeginDisabledGroup(!m_importerParam.CopyToStreamingAssets); 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_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")) { ImageSequenceImporter.Import(m_importerParam); this.Close(); } if (GUILayout.Button("Cancel")) { this.Close(); } EditorGUILayout.EndHorizontal(); EditorGUILayout.EndVertical(); }
//---------------------------------------------------------------------------------------------------------------------- internal static void Import(ImageFileImporterParam param) { string destFolder = null; if (!param.CopyToStreamingAssets) { destFolder = param.strSrcFolder.Replace("\\", "/"); } else { destFolder = param.strDstFolder.Replace("\\", "/"); if (destFolder.StartsWith(Application.dataPath) && !destFolder.StartsWith(Path.Combine(Application.dataPath, "StreamingAssets").Replace("\\", "/"))) { Debug.LogError("Files must be located under StreamingAssets folder."); return; } foreach (string relPath in param.RelativeFilePaths) { string strAbsFilePathDst = Path.Combine(destFolder, relPath).Replace("\\", "/"); if (File.Exists(strAbsFilePathDst)) { File.Delete(strAbsFilePathDst); } string strAbsFilePathSrc = Path.Combine(param.strSrcFolder, relPath).Replace("\\", "/"); Directory.CreateDirectory(Path.GetDirectoryName(strAbsFilePathDst));//make sure dir exists FileUtil.CopyFileOrDirectory(strAbsFilePathSrc, strAbsFilePathDst); } } // create assets StreamingImageSequencePlayableAssetParam playableAssetParam = new StreamingImageSequencePlayableAssetParam(); playableAssetParam.Pictures = new List <string>(param.RelativeFilePaths); //if possible, convert folder names to relative path. string strUnityProjectFolder = null; Regex regAssetFolder = new Regex("/Assets$"); strUnityProjectFolder = Application.dataPath; strUnityProjectFolder = regAssetFolder.Replace(strUnityProjectFolder, ""); if (destFolder.StartsWith(strUnityProjectFolder)) { int start = strUnityProjectFolder.Length + 1; int end = destFolder.Length - start; destFolder = destFolder.Substring(start, end); } playableAssetParam.Folder = destFolder; //StreamingAsset StreamingImageSequencePlayableAsset playableAsset = param.TargetAsset; if (null == playableAsset) { string assetName = ImageSequenceImporter.EstimateAssetName(playableAssetParam.Pictures[0]); playableAsset = CreateUniqueSISAsset( Path.Combine("Assets", assetName + "_StreamingImageSequence.playable").Replace("\\", "/") ); } playableAsset.InitFolder(playableAssetParam); if (param.CopyToStreamingAssets) { AssetDatabase.Refresh(); } }
//---------------------------------------------------------------------------------------------------------------------- private void ImportImages(string path) { ImageSequenceImporter.ImportImages(path, m_asset); m_isImageListDirty = true; }