コード例 #1
0
        public static void Import(PictureFileImporterParam param)
        {
            if (param.DoNotCopy)
            {
                param.strDstFolder = param.strSrcFolder.Replace("\\", "/");
            }
            else
            {
                string dstFolder = param.strDstFolder.Replace("\\", "/");
                if (param.mode == PictureFileImporterParam.Mode.StreamingAssets)
                {
                    if (dstFolder.StartsWith(Application.dataPath) && !dstFolder.StartsWith(Path.Combine(Application.dataPath, "StreamingAssets").Replace("\\", "/")))
                    {
                        Debug.LogError("Files must be located under StreamingAssets folder.");
                        return;
                    }
                }
                else
                {
                    if (dstFolder.StartsWith(Application.dataPath) && dstFolder.StartsWith(Path.Combine(Application.dataPath, "StreamingAssets").Replace("\\", "/")))
                    {
                        Debug.LogError("Files must not be located under StreamingAssets folder.");
                        return;
                    }
                }

                foreach (string relPath in param.RelativeFilePaths)
                {
                    string strAbsFilePathDst = Path.Combine(param.strDstFolder, 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 trackMovieContainer = new StreamingImageSequencePlayableAssetParam();

            trackMovieContainer.Pictures = new string[param.RelativeFilePaths.Count];
            for (int ii = 0; ii < param.RelativeFilePaths.Count; ii++)
            {
                trackMovieContainer.Pictures[ii] = param.RelativeFilePaths[ii];
            }

            ///   if possible, convert folder names to relative path.
            string strUnityProjectFolder = null;
            Regex  regAssetFolder        = new Regex("/Assets$");

            strUnityProjectFolder = Application.dataPath;
            strUnityProjectFolder = regAssetFolder.Replace(strUnityProjectFolder, "");


            if (param.strDstFolder.StartsWith(strUnityProjectFolder))
            {
                int start = strUnityProjectFolder.Length + 1;
                int end   = param.strDstFolder.Length - start;
                param.strDstFolder = param.strDstFolder.Substring(start, end);
            }
            trackMovieContainer.Folder = param.strDstFolder;

            if (param.mode == PictureFileImporterParam.Mode.SpriteAnimation)
            {
                Sprite[] sprites = new Sprite[param.RelativeFilePaths.Count];
                for (int ii = 0; ii < param.RelativeFilePaths.Count; ii++)
                {
                    string strAssetPath = Path.Combine(param.strDstFolder, param.RelativeFilePaths[ii]).Replace("\\", "/");

                    AssetDatabase.ImportAsset(strAssetPath);
                    TextureImporter importer = AssetImporter.GetAtPath(strAssetPath) as TextureImporter;
                    importer.textureType = TextureImporterType.Sprite;
                    AssetDatabase.WriteImportSettingsIfDirty(strAssetPath);

                    Texture2D tex = (Texture2D)AssetDatabase.LoadAssetAtPath(strAssetPath, typeof(Texture2D));

                    sprites[ii] = Sprite.Create(tex, new Rect(0.0f, 0.0f, tex.width, tex.height), new Vector2(0.5f, 0.5f));
                }

                AnimationClip newClip = new AnimationClip();
                newClip.wrapMode = WrapMode.Once;
                SerializedObject   serializedClip = new SerializedObject(newClip);
                SerializedProperty settings       = serializedClip.FindProperty("m_AnimationClipSettings");
                while (settings.Next(true))
                {
                    if (settings.name == "m_LoopTime")
                    {
                        break;
                    }
                }

                settings.boolValue = true;
                serializedClip.ApplyModifiedProperties();
                ObjectReferenceKeyframe[] Keyframes    = new ObjectReferenceKeyframe[param.RelativeFilePaths.Count];
                EditorCurveBinding        curveBinding = new EditorCurveBinding();


                for (int ii = 0; ii < param.RelativeFilePaths.Count; ii++)
                {
                    Keyframes[ii]       = new ObjectReferenceKeyframe();
                    Keyframes[ii].time  = 0.25F * ii;
                    Keyframes[ii].value = sprites[ii];
                }
#if false
                curveBinding.type         = typeof(SpriteRenderer);
                curveBinding.path         = string.Empty;
                curveBinding.propertyName = "m_Sprite";
#else
                curveBinding.type         = typeof(Image);
                curveBinding.path         = string.Empty;
                curveBinding.propertyName = "m_Sprite";
#endif
                AnimationUtility.SetObjectReferenceCurve(newClip, curveBinding, Keyframes);
                AssetDatabase.CreateAsset(newClip, Path.Combine(param.strDstFolder, "Animation.anim").Replace("\\", "/"));

                //            var proxyAsset = ScriptableObject.CreateInstance<StreamingImageSequencePlayableAsset>(); //new StreamingImageSequencePlayableAsset(trackMovieContainer);
                //            proxyAsset.SetParam(trackMovieContainer);
                //            var strProxyPath = AssetDatabase.GenerateUniqueAssetPath(Path.Combine("Assets", param.strAssetName + "_MovieProxy.playable").Replace("\\", "/"));*/
                AssetDatabase.Refresh();
            }
            else
            {
                //StreamingAsset
                StreamingImageSequencePlayableAsset proxyAsset = param.TargetAsset;
                if (null == proxyAsset)
                {
                    proxyAsset = ScriptableObject.CreateInstance <StreamingImageSequencePlayableAsset>();
                    var strProxyPath = AssetDatabase.GenerateUniqueAssetPath(Path.Combine("Assets", param.strAssetName + "_MovieProxy.playable").Replace("\\", "/"));
                    AssetDatabase.CreateAsset(proxyAsset, strProxyPath);
                }

                proxyAsset.SetParam(trackMovieContainer);
                if (!param.DoNotCopy)
                {
                    AssetDatabase.Refresh();
                }
            }
        }
コード例 #2
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();
            }
        }
コード例 #3
0
 internal static void SetParam(PictureFileImporterParam param)
 {
     m_importerParam = param;
 }