DoAssetReimport() public static method

Does asset reimport.
public static DoAssetReimport ( string path, ImportAssetOptions options ) : void
path string Path.
options ImportAssetOptions Options.
return void
Ejemplo n.º 1
0
        /// <summary>
        /// Sets the asset Read/Write enabled state.
        /// </summary>
        /// <returns><c>true</c>, if set read write enabled was asseted, <c>false</c> otherwise.</returns>
        /// <param name="path">Path.</param>
        /// <param name="enabled">If set to <c>true</c> enabled.</param>
        /// <param name="force">If set to <c>true</c> force.</param>
        public static bool AssetSetReadWriteEnabled(string path, bool enabled, bool force)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(false);
            }

            TextureImporter ti = AssetImporter.GetAtPath(path) as TextureImporter;

            if (ti == null)
            {
                return(false);
            }

            TextureImporterSettings settings = new TextureImporterSettings();

            ti.ReadTextureSettings(settings);

            if (force || settings.readable != enabled)
            {
                settings.readable = enabled;
                ti.SetTextureSettings(settings);
                SPTools.DoAssetReimport(path, ImportAssetOptions.ForceUpdate | ImportAssetOptions.ForceSynchronousImport);
            }

            return(true);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Sets the asset texture format.
        /// </summary>
        /// <returns><c>true</c>, if set format was set, <c>false</c> otherwise.</returns>
        /// <param name="path">Path.</param>
        /// <param name="format">Format.</param>
        public static bool AssetSetFormat(string path, TextureImporterFormat format)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(false);
            }

            TextureImporter ti = AssetImporter.GetAtPath(path) as TextureImporter;

            if (ti == null)
            {
                return(false);
            }

            TextureImporterPlatformSettings tips = ti.GetDefaultPlatformTextureSettings();

            tips.format     = format;
            tips.overridden = true;

            ti.SetPlatformTextureSettings(tips);

            SPTools.DoAssetReimport(path, ImportAssetOptions.ForceUpdate | ImportAssetOptions.ForceSynchronousImport);

            return(true);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Sets the asset texture format.
        /// </summary>
        /// <returns><c>true</c>, if set format was set, <c>false</c> otherwise.</returns>
        /// <param name="path">Path.</param>
        /// <param name="format">Format.</param>
        public static bool AssetSetFormat(string path, TextureImporterFormat format)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(false);
            }

            TextureImporter ti = AssetImporter.GetAtPath(path) as TextureImporter;

            if (ti == null)
            {
                return(false);
            }

#if UNITY_5_5_OR_NEWER
            TextureImporterPlatformSettings platformSettings = ti.GetDefaultPlatformTextureSettings();
            platformSettings.format = format;
            ti.SetPlatformTextureSettings(platformSettings);
#else
            TextureImporterSettings settings = new TextureImporterSettings();
            ti.ReadTextureSettings(settings);
            settings.textureFormat = format;
            ti.SetTextureSettings(settings);
#endif
            SPTools.DoAssetReimport(path, ImportAssetOptions.ForceUpdate | ImportAssetOptions.ForceSynchronousImport);

            return(true);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Sets the asset texture format.
        /// </summary>
        /// <returns><c>true</c>, if set format was set, <c>false</c> otherwise.</returns>
        /// <param name="path">Path.</param>
        /// <param name="format">Format.</param>
        public static bool AssetSetFormat(string path, TextureImporterFormat format, Texture2D texture)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(false);
            }

            TextureImporter ti = AssetImporter.GetAtPath(path) as TextureImporter;

            if (ti == null)
            {
                return(false);
            }

            TextureImporterSettings settings = new TextureImporterSettings();

            ti.ReadTextureSettings(settings);
            int size = Mathf.Max(texture.height, texture.width);

            SetPlatformTextureSettings("Standalone", size, ti, format);
            SetPlatformTextureSettings("Android", size, ti, format);
            SetPlatformTextureSettings("iPhone", size, ti, format);
            SPTools.DoAssetReimport(path, ImportAssetOptions.ForceUpdate | ImportAssetOptions.ForceSynchronousImport);

            return(true);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Sets the asset texture format.
        /// </summary>
        /// <returns><c>true</c>, if set format was set, <c>false</c> otherwise.</returns>
        /// <param name="path">Path.</param>
        /// <param name="format">Format.</param>
        public static bool AssetSetFormat(string path, TextureImporterFormat format)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(false);
            }

            TextureImporter ti = AssetImporter.GetAtPath(path) as TextureImporter;

            if (ti == null)
            {
                return(false);
            }

            TextureImporterSettings         settings        = new TextureImporterSettings();
            TextureImporterPlatformSettings platformSetting = new TextureImporterPlatformSettings();

            ti.ReadTextureSettings(settings);

            platformSetting.format = format;
            ti.SetTextureSettings(settings);
            SPTools.DoAssetReimport(path, ImportAssetOptions.ForceUpdate | ImportAssetOptions.ForceSynchronousImport);

            return(true);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Creates a blank atlas texture.
        /// </summary>
        /// <returns><c>true</c>, if blank texture was created, <c>false</c> otherwise.</returns>
        /// <param name="path">Asset Path.</param>
        /// <param name="alphaTransparency">If set to <c>true</c> alpha transparency.</param>
        public static bool CreateBlankTexture(string path, bool alphaTransparency)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(false);
            }

            // Prepare blank texture
            Texture2D texture = new Texture2D(1, 1, TextureFormat.ARGB32, false);

            // Create the texture asset
            AssetDatabase.CreateAsset(texture, AssetDatabase.GenerateUniqueAssetPath(path));

            // Clear the read-only flag in texture file attributes
            if (!SPTools.RemoveReadOnlyFlag(path))
            {
                return(false);
            }

            // Write the texture data
            byte[] bytes = texture.EncodeToPNG();
            System.IO.File.WriteAllBytes(path, bytes);
            bytes = null;

            // Get the asset texture importer
            TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;

            if (textureImporter == null)
            {
                return(false);
            }

            TextureImporterSettings         settings        = new TextureImporterSettings();
            TextureImporterPlatformSettings platformSetting = new TextureImporterPlatformSettings();

            textureImporter.ReadTextureSettings(settings);

            settings.spriteMode            = 2;
            settings.readable              = false;
            textureImporter.maxTextureSize = 4096;
            settings.wrapMode              = TextureWrapMode.Clamp;
            settings.npotScale             = TextureImporterNPOTScale.ToNearest;
            platformSetting.format         = TextureImporterFormat.ARGB32;

            settings.filterMode          = FilterMode.Point;
            settings.aniso               = 4;
            settings.alphaIsTransparency = alphaTransparency;

            textureImporter.SetTextureSettings(settings);
            textureImporter.textureType = TextureImporterType.Sprite;

            AssetDatabase.SaveAssets();
            SPTools.DoAssetReimport(path, ImportAssetOptions.ForceUpdate | ImportAssetOptions.ForceSynchronousImport);

            return(true);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Imports a texture as asset.
        /// </summary>
        /// <returns><c>true</c>, if texture was imported, <c>false</c> otherwise.</returns>
        /// <param name="path">Path.</param>
        /// <param name="texture">Texture.</param>
        public static bool ImportTexture(string path, Texture2D texture)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(false);
            }

            // Clear the read-only flag in texture file attributes
            if (!SPTools.RemoveReadOnlyFlag(path))
            {
                return(false);
            }

            byte[] bytes = texture.EncodeToPNG();
            System.IO.File.WriteAllBytes(path, bytes);
            bytes = null;

            AssetDatabase.SaveAssets();
            SPTools.DoAssetReimport(path, ImportAssetOptions.ForceSynchronousImport);

            return(true);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Imports and configures atlas texture.
        /// </summary>
        /// <returns><c>true</c>, if import and configure atlas texture was successful, <c>false</c> otherwise.</returns>
        /// <param name="targetTexture">Target texture.</param>
        /// <param name="sourceTexture">Source texture.</param>
        /// <param name="uvs">Uvs.</param>
        /// <param name="names">Names.</param>
        /// <param name="defaultPivot">Default pivot.</param>
        /// <param name="defaultCustomPivot">Default custom pivot.</param>
        public static bool ImportAndConfigureAtlasTexture(Texture2D targetTexture, Texture2D sourceTexture, Rect[] uvs, SPSpriteImportData[] spritesImportData)
        {
            // Get the asset path
            string assetPath = SPTools.GetAssetPath(targetTexture);

            if (string.IsNullOrEmpty(assetPath))
            {
                Debug.LogError("Sprite Packer failed to Import and Configure the atlas texture, reason: Could not resolve asset path.");
                return(false);
            }

            // Clear the read-only flag in texture file attributes
            if (!SPTools.RemoveReadOnlyFlag(assetPath))
            {
                Debug.LogError("Sprite Packer failed to Import and Configure the atlas texture, reason: Could not remove the readonly flag from the asset.");
                return(false);
            }

            // Write the source texture data to the asset
            byte[] bytes = sourceTexture.EncodeToPNG();
            System.IO.File.WriteAllBytes(assetPath, bytes);
            bytes = null;

            // Get the asset texture importer
            TextureImporter texImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter;

            if (texImporter == null)
            {
                Debug.LogError("Sprite Packer failed to Import and Configure the atlas texture, reason: Could not get the texture importer for the asset.");
                return(false);
            }

            // Get the asset texture importer settings
            TextureImporterSettings texImporterSettings = new TextureImporterSettings();

            // Apply sprite type
            texImporter.textureType      = TextureImporterType.Sprite;
            texImporter.spriteImportMode = SpriteImportMode.Multiple;

            // Configure the spritesheet meta data
            SpriteMetaData[] spritesheetMeta = new SpriteMetaData[uvs.Length];
            for (int i = 0; i < uvs.Length; i++)
            {
                if (SPTools.HasSpritesheetMeta(texImporter.spritesheet, spritesImportData[i].name))
                {
                    SpriteMetaData currentMeta = SPTools.GetSpritesheetMeta(texImporter.spritesheet, spritesImportData[i].name);
                    Rect           currentRect = uvs[i];
                    currentRect.x      *= sourceTexture.width;
                    currentRect.width  *= sourceTexture.width;
                    currentRect.y      *= sourceTexture.height;
                    currentRect.height *= sourceTexture.height;
                    currentMeta.rect    = currentRect;
                    spritesheetMeta[i]  = currentMeta;
                }
                else
                {
                    SpriteMetaData currentMeta = new SpriteMetaData();
                    Rect           currentRect = uvs[i];
                    currentRect.x        *= sourceTexture.width;
                    currentRect.width    *= sourceTexture.width;
                    currentRect.y        *= sourceTexture.height;
                    currentRect.height   *= sourceTexture.height;
                    currentMeta.rect      = currentRect;
                    currentMeta.name      = spritesImportData[i].name;
                    currentMeta.alignment = (int)spritesImportData[i].alignment;
                    currentMeta.pivot     = spritesImportData[i].pivot;
                    currentMeta.border    = spritesImportData[i].border;
                    spritesheetMeta[i]    = currentMeta;
                }
            }
            texImporter.spritesheet = spritesheetMeta;

            // Read the texture importer settings
            texImporter.ReadTextureSettings(texImporterSettings);

            // Disable Read/Write
            texImporterSettings.readable = false;

            // Re-set the texture importer settings
            texImporter.SetTextureSettings(texImporterSettings);

            // Save and Reimport the asset
            AssetDatabase.SaveAssets();
            SPTools.DoAssetReimport(assetPath, ImportAssetOptions.ForceUpdate | ImportAssetOptions.ForceSynchronousImport);

            // Return success
            return(true);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Creates a blank atlas texture.
        /// </summary>
        /// <returns><c>true</c>, if blank texture was created, <c>false</c> otherwise.</returns>
        /// <param name="path">Asset Path.</param>
        /// <param name="alphaTransparency">If set to <c>true</c> alpha transparency.</param>
        public static bool CreateBlankTexture(string path, bool alphaTransparency)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(false);
            }

            // Prepare blank texture
            Texture2D texture = new Texture2D(1, 1, TextureFormat.ARGB32, false);

            // Create the texture asset
            byte[]       bytes  = texture.EncodeToPNG();
            FileStream   stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
            BinaryWriter writer = new BinaryWriter(stream);

            for (int i = 0; i < bytes.Length; i++)
            {
                writer.Write(bytes[i]);
            }
            writer.Close();
            stream.Close();

            // Clear the read-only flag in texture file attributes
            if (!SPTools.RemoveReadOnlyFlag(path))
            {
                return(false);
            }

            // Import the texture asset
            SPTools.DoAssetReimport(path, ImportAssetOptions.ForceUpdate);

            // Get the asset texture importer
            TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;

            if (textureImporter == null)
            {
                return(false);
            }

            TextureImporterSettings settings = new TextureImporterSettings();

            textureImporter.ReadTextureSettings(settings);

            //TextureImporterPlatformSettings
            settings.spriteMode = 2;
            settings.readable   = false;
            //settings.maxTextureSize = 4096;
            settings.wrapMode  = TextureWrapMode.Clamp;
            settings.npotScale = TextureImporterNPOTScale.ToNearest;
            //settings.textureFormat = TextureImporterFormat.ARGB32;
            settings.filterMode          = FilterMode.Trilinear;
            settings.aniso               = 4;
            settings.alphaIsTransparency = alphaTransparency;

            textureImporter.SetTextureSettings(settings);
            textureImporter.textureType = TextureImporterType.Sprite;

            TextureImporterPlatformSettings tips = new TextureImporterPlatformSettings();

            tips.maxTextureSize     = 4096;
            tips.format             = TextureImporterFormat.Automatic;
            tips.textureCompression = TextureImporterCompression.Uncompressed;
            tips.overridden         = true;

            textureImporter.SetPlatformTextureSettings(tips);

            AssetDatabase.SaveAssets();
            SPTools.DoAssetReimport(path, ImportAssetOptions.ForceUpdate | ImportAssetOptions.ForceSynchronousImport);

            return(true);
        }