SimpleSpritePackerEditor.SPTools.RemoveReadOnlyFlag C# (CSharp) Method

RemoveReadOnlyFlag() public static method

Removes the read only flag from the asset.
public static RemoveReadOnlyFlag ( string path ) : bool
path string Path.
return bool
        public static bool RemoveReadOnlyFlag(string path)
        {
            if (string.IsNullOrEmpty(path))
                return false;

            // Clear the read-only flag in texture file attributes
            if (System.IO.File.Exists(path))
            {
                #if !UNITY_4_1 && !UNITY_4_0 && !UNITY_3_5
                if (!AssetDatabase.IsOpenForEdit(path))
                {
                    Debug.LogError(path + " is not editable. Did you forget to do a check out?");
                    return false;
                }
                #endif
                System.IO.FileAttributes texPathAttrs = System.IO.File.GetAttributes(path);
                texPathAttrs &= ~System.IO.FileAttributes.ReadOnly;
                System.IO.File.SetAttributes(path, texPathAttrs);

                return true;
            }

            // Default
            return false;
        }

Usage Example

Beispiel #1
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);
        }
All Usage Examples Of SimpleSpritePackerEditor.SPTools::RemoveReadOnlyFlag