Exemplo n.º 1
0
        internal static IEnumerable <UnityEngine.Object> DuplicateAssets(IEnumerable <UnityEngine.Object> assets)
        {
            AssetDatabase.Refresh();
            List <string> list = new List <string>();

            foreach (UnityEngine.Object current in assets)
            {
                string assetPath = AssetDatabase.GetAssetPath(current);
                if (AssetDatabase.IsSubAsset(current))
                {
                    string extensionForAsset = NativeFormatImporterUtility.GetExtensionForAsset(current);
                    string text = AssetDatabase.GenerateUniqueAssetPath(Path.Combine(Path.GetDirectoryName(assetPath), string.Format("{0}.{1}", current.name, extensionForAsset)));
                    AssetDatabase.CreateAsset(UnityEngine.Object.Instantiate(current), text);
                    list.Add(text);
                }
                else if (EditorUtility.IsPersistent(current))
                {
                    string text2 = AssetDatabase.GenerateUniqueAssetPath(assetPath);
                    if (text2.Length > 0 && AssetDatabase.CopyAsset(assetPath, text2))
                    {
                        list.Add(text2);
                    }
                }
            }
            AssetDatabase.Refresh();
            IEnumerable <string> arg_F3_0 = list;

            if (ProjectWindowUtil.< > f__mg$cache0 == null)
            {
                ProjectWindowUtil.< > f__mg$cache0 = new Func <string, UnityEngine.Object>(AssetDatabase.LoadMainAssetAtPath);
            }
            return(arg_F3_0.Select(ProjectWindowUtil.< > f__mg$cache0));
        }
        internal static string GetExtensionForAsset(UnityEngine.Object asset)
        {
            Type   type = asset.GetType();
            string result;

            if (!typeof(ScriptableObject).IsAssignableFrom(type))
            {
                result = NativeFormatImporterUtility.Internal_GetExtensionForNativeAsset(asset);
            }
            else
            {
                while (type != typeof(UnityEngine.Object))
                {
                    foreach (KeyValuePair <Type, string[]> current in NativeFormatImporterUtility.s_RegisteredExtensionsByType)
                    {
                        if (current.Key == type)
                        {
                            result = current.Value[0];
                            return(result);
                        }
                    }
                    type = type.BaseType;
                }
                result = "asset";
            }
            return(result);
        }
        private static string ValidateExtension(string extension, Type requestingType)
        {
            string extension2;

            if (string.Equals(extension, "asset", StringComparison.OrdinalIgnoreCase))
            {
                extension2 = extension;
            }
            else
            {
                KeyValuePair <Type, string[]> keyValuePair = NativeFormatImporterUtility.s_RegisteredExtensionsByType.FirstOrDefault((KeyValuePair <Type, string[]> kv) => kv.Value.Count((string ext) => string.Equals(ext, extension, StringComparison.OrdinalIgnoreCase)) > 0);
                if (keyValuePair.Key != null)
                {
                    throw new ArgumentException(string.Format("Extension \"{0}\" is already registered for type {1}. It cannot also be used for {2}.", extension, keyValuePair.Key, requestingType), "extension");
                }
                bool flag;
                Type type = NativeFormatImporterUtility.Internal_GetNativeTypeForExtension(extension, out flag);
                if (!flag)
                {
                    throw new ArgumentException(string.Format("Extension \"{0}\" must also be registered in NativeFormatImporterExtensions.h with identical capitalization.", extension), "extension");
                }
                if (type != null && type != requestingType)
                {
                    throw new ArgumentException(string.Format("Extension \"{0}\" is registered for type {1}. It cannot also be used for {2}.", extension, type, requestingType), "extension");
                }
                extension2 = extension;
            }
            return(extension2);
        }
Exemplo n.º 4
0
        internal static IEnumerable <Object> DuplicateAssets(IEnumerable <Object> assets)
        {
            AssetDatabase.Refresh();

            var    copiedPaths = new List <string>();
            Object firstDuplicatedObjectToFail = null;

            foreach (var asset in assets)
            {
                var assetPath = AssetDatabase.GetAssetPath(asset);

                // if duplicating a sub-asset, then create a copy next to the main asset file
                if (AssetDatabase.IsSubAsset(asset))
                {
                    if (asset is ISubAssetNotDuplicatable)
                    {
                        firstDuplicatedObjectToFail = firstDuplicatedObjectToFail ? firstDuplicatedObjectToFail : asset;
                        continue;
                    }

                    var extension = NativeFormatImporterUtility.GetExtensionForAsset(asset);
                    var newPath   = AssetDatabase.GenerateUniqueAssetPath(
                        Path.Combine(Path.GetDirectoryName(assetPath), string.Format("{0}.{1}", asset.name, extension))
                        );
                    AssetDatabase.CreateAsset(Object.Instantiate(asset), newPath);
                    copiedPaths.Add(newPath);
                }
                // otherwise duplicate the main asset file
                else if (EditorUtility.IsPersistent(asset))
                {
                    var newPath = AssetDatabase.GenerateUniqueAssetPath(assetPath);
                    if (newPath.Length > 0 && AssetDatabase.CopyAsset(assetPath, newPath))
                    {
                        copiedPaths.Add(newPath);
                    }
                }
            }

            if (firstDuplicatedObjectToFail != null)
            {
                Debug.LogError("Duplication error: One or more sub assets could not be duplicated directly, use the appropriate editor instead", firstDuplicatedObjectToFail);
            }

            AssetDatabase.Refresh();

            return(copiedPaths.Select(AssetDatabase.LoadMainAssetAtPath));
        }
 static NativeFormatImporterUtility()
 {
     NativeFormatImporterUtility.s_RegisteredExtensionsByType = new Dictionary <Type, string[]>();
     foreach (Type current in EditorAssemblies.GetAllTypesWithAttribute <AssetFileNameExtensionAttribute>())
     {
         AssetFileNameExtensionAttribute assetFileNameExtensionAttribute = current.GetCustomAttributes(typeof(AssetFileNameExtensionAttribute), false)[0] as AssetFileNameExtensionAttribute;
         try
         {
             NativeFormatImporterUtility.RegisterExtensionForType(current, assetFileNameExtensionAttribute.preferredExtension, assetFileNameExtensionAttribute.otherExtensions.ToArray <string>());
         }
         catch (ArgumentException exception)
         {
             Debug.LogException(exception);
         }
         catch (NotSupportedException exception2)
         {
             Debug.LogException(exception2);
         }
     }
 }
        internal static void RegisterExtensionForType(Type type, string preferredExtension, params string[] otherExtensions)
        {
            if (!type.IsSubclassOf(typeof(ScriptableObject)))
            {
                throw new NotSupportedException(string.Format("{0} may only be added to {1} types.", typeof(AssetFileNameExtensionAttribute), typeof(ScriptableObject)));
            }
            if (NativeFormatImporterUtility.s_RegisteredExtensionsByType.ContainsKey(type))
            {
                throw new ArgumentException(string.Format("Extension already registered for type {0}.", type), "type");
            }
            string[] array = new string[otherExtensions.Length + 1];
            array[0] = NativeFormatImporterUtility.ValidateExtension(preferredExtension, type);
            int i   = 0;
            int num = otherExtensions.Length;

            while (i < num)
            {
                array[i + 1] = NativeFormatImporterUtility.ValidateExtension(otherExtensions[i], type);
                i++;
            }
            NativeFormatImporterUtility.s_RegisteredExtensionsByType[type] = array;
        }
Exemplo n.º 7
0
        internal static IEnumerable <Object> DuplicateAssets(IEnumerable <Object> assets)
        {
            AssetDatabase.Refresh();

            var    copiedPaths = new List <string>();
            Object firstDuplicatedObjectToFail = null;

            foreach (var asset in assets)
            {
                var assetPath = AssetDatabase.GetAssetPath(asset);

                // if duplicating a sub-asset, then create a copy next to the main asset file
                if (!String.IsNullOrEmpty(assetPath) && AssetDatabase.IsSubAsset(asset))
                {
                    if (asset is ISubAssetNotDuplicatable)
                    {
                        firstDuplicatedObjectToFail = firstDuplicatedObjectToFail ? firstDuplicatedObjectToFail : asset;
                        continue;
                    }

                    var extension = NativeFormatImporterUtility.GetExtensionForAsset(asset);

                    // We dot sanitize or block unclean the asset filename (asset.name)
                    // since the assertdb will do it for us and has a whole tailored logic for that.

                    // It feels wrong that the asset name (that can apparently contain any char)
                    // is conflated with the orthogonal notion of filename. From the user's POV
                    // it will force an asset dup but with mangled names if the original name contained
                    // "invalid chars" for filenames.
                    // Path.Combine is not used here to avoid blocking asset names that might
                    // contain chars not allowed in filenames.
                    if ((new HashSet <char>(Path.GetInvalidFileNameChars())).Intersect(asset.name).Count() != 0)
                    {
                        Debug.LogWarning(string.Format("Duplicated asset name '{0}' contains invalid characters. Those will be replaced in the duplicated asset name.", asset.name));
                    }

                    var newPath = AssetDatabase.GenerateUniqueAssetPath(
                        string.Format("{0}{1}{2}.{3}",
                                      Path.GetDirectoryName(assetPath),
                                      Path.DirectorySeparatorChar,
                                      asset.name,
                                      extension)
                        );
                    AssetDatabase.CreateAsset(Object.Instantiate(asset), newPath);
                    copiedPaths.Add(newPath);
                }
                // otherwise duplicate the main asset file
                else if (EditorUtility.IsPersistent(asset))
                {
                    var newPath = AssetDatabase.GenerateUniqueAssetPath(assetPath);
                    if (newPath.Length > 0 && AssetDatabase.CopyAsset(assetPath, newPath))
                    {
                        copiedPaths.Add(newPath);
                    }
                }
            }

            if (firstDuplicatedObjectToFail != null)
            {
                var errString = string.Format(
                    "Duplication error: One or more sub assets (with types of {0}) can not be duplicated directly, use the appropriate editor instead",
                    firstDuplicatedObjectToFail.GetType().Name
                    );

                Debug.LogError(errString, firstDuplicatedObjectToFail);
            }

            AssetDatabase.Refresh();

            return(copiedPaths.Select(AssetDatabase.LoadMainAssetAtPath));
        }