コード例 #1
0
        /// <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();
            }
        }
コード例 #2
0
        static void importPictureFiles(PictureFileImporterParam.Mode mode)
        {
            PictureFileImporterParam param = new PictureFileImporterParam();

            string strPath      = EditorUtility.OpenFilePanel("Open File", "", param.strExtensionPng + "," + param.strExtentionTga);
            string strExtension = Path.GetExtension(strPath).ToLower();

            if (strExtension == "." + param.strExtensionPng.ToLower())
            {
                param.strExtension = param.strExtensionPng;
            }
            else if (strExtension == "." + param.strExtentionTga.ToLower())
            {
                param.strExtension = param.strExtentionTga;
            }

            var strFileneWithoutExtention = Path.GetFileNameWithoutExtension(strPath);

            if (!Regex.IsMatch(strFileneWithoutExtention, @"\d+$"))
            {
                Debug.LogError(@"Input doesn't include number.");
                return;
            }



            /// cehck Importing file name
            var regNumbers = new Regex(@"\d+$");
            var matches    = regNumbers.Matches(strFileneWithoutExtention);

            Assert.IsTrue(matches.Count > 0);

            param.match = null;
            foreach (Match match in matches)
            {
                param.match = match;
            }

            Assert.IsTrue(param.match != null);

            var parsed      = int.Parse(param.match.Value);
            int periodIndex = strFileneWithoutExtention.Length;
            int digits      = param.match.Value.Length;

            param.strSrcFolder = Path.GetDirectoryName(strPath);
            var strBaseName = strFileneWithoutExtention.Substring(0, param.match.Index);


            /// create copy destination path


            var strDistFolder = Application.streamingAssetsPath;

            if (mode == PictureFileImporterParam.Mode.SpriteAnimation)
            {
                strDistFolder = Application.dataPath;
            }
            if (!Directory.Exists(strDistFolder))
            {
                Directory.CreateDirectory(strDistFolder);
            }

            param.strAssetName = strBaseName;
            if (param.strAssetName.EndsWith("_") || param.strAssetName.EndsWith("-"))
            {
                param.strAssetName = param.strAssetName.Substring(0, param.strAssetName.Length - 1);
            }

            param.strDstFolder = Path.Combine(strDistFolder, param.strAssetName).Replace("\\", "/");



            /// making list of the files and copy them.
            List <string> strNames = new List <string>();

            for (; ;)
            {
                string strZero     = string.Format("{0:D" + digits + "}", parsed++);
                string strFileName = strBaseName + strZero + "." + param.strExtension;
                strFileName = strFileName.Replace("\\", "/");
                string path = Path.Combine(param.strSrcFolder, strFileName).Replace("\\", "/");
                if (!File.Exists(path))
                {
                    break;
                }
                strNames.Add(strFileName);
            }

            param.files = new string[strNames.Count];
            for (int ii = 0; ii < strNames.Count; ii++)
            {
                param.files[ii] = strNames[ii];
            }
            param.mode = mode;
            PictureFileImportWindow.Init(param);
        }