void LoadFileSettings() { if (currentSelectedSettings == null) { return; } if (Selection.objects.Length == 0) { return; } // No need to load with multiple selections without multiobject support if (Selection.objects.Length > 1) { return; } if (!IsObjectValidTexture(Selection.activeObject)) { return; } FileSettings = LoadSavedFileSettingsForObject(Selection.activeObject); fileSettingsLoaded = true; }
void DrawApplyButton() { Color defaultBg = GUI.backgroundColor; GUI.backgroundColor = Color.green; if (GUILayout.Button("Apply")) { foreach (var obj in Selection.objects) { if (IsObjectValidTexture(obj)) { // Load currently set fileSettings for multi-select. Otherwise use saved fileSettings SpriteFileSettings settings = Selection.objects.Length == 1 ? FileSettings : LoadSavedFileSettingsForObject(obj); if (!settings.SlicingOptions.IsValid()) { Debug.LogWarning("Skipping ApplyingTextureSettings to object due to invalid " + "Slicing Options. Object: " + obj.name); continue; } string path = AssetDatabase.GetAssetPath(obj); var selectedTexture = AssetDatabase.LoadAssetAtPath <Texture2D> (path); SpriteSettingsUtility.ApplySpriteSettings(selectedTexture, currentSelectedSettings, settings); } } if (currentSelectedSettings.PackOnApply) { UnityEditor.Sprites.Packer.RebuildAtlasCacheIfNeeded( EditorUserBuildSettings.activeBuildTarget, true); } } GUI.backgroundColor = defaultBg; }
void DrawFileSpecificSettings() { if (Selection.objects.Length > 1) { string slicingInfo = ""; foreach (var obj in Selection.objects) { if (IsObjectValidTexture(obj)) { FileSettings = LoadSavedFileSettingsForObject(obj); slicingInfo += "\n" + obj.name + ": " + FileSettings.SlicingOptions.ToDisplayString(); } } EditorGUILayout.HelpBox("File Settings do not support Multiple-Object editing. " + " The following settings will be used:\n" + slicingInfo, MessageType.Info); return; } DrawSlicingOptions(); EditorGUILayout.Space(); EditorGUILayout.LabelField("Override Settings", EditorStyles.boldLabel); if (FileSettings.SlicingOptions.ImportMode != SpriteImportMode.None) { DrawPivotOverride(); } DrawPackingTagOverride(); }
public static SpriteFileSettings GetFileSettings(string path, TextAsset slicingOptionsDataFile) { if (slicingOptionsDataFile != null) { string[] entries = slicingOptionsDataFile.text.Split( new string[] { "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries); string entry = entries.FirstOrDefault(x => x.StartsWith(Path.GetFileName(path))); if (!string.IsNullOrEmpty(entry)) { try { // Strip filename int firstIndex = entry.IndexOf(',') + 1; int lastIndex = entry.Length - 1; var slicingData = entry.Substring(firstIndex, lastIndex - firstIndex + 1); return(SpriteFileSettings.FromString(slicingData)); } catch (SystemException e) { Debug.LogError(string.Format("Encountered error in saved slicing options file. Entry: " + entry + "\n Error: " + e)); } } } return(new SpriteFileSettings()); }
public static void ApplySpriteSettings(Texture2D texture, SpriteSettings prefs, SpriteFileSettings fileSettings) { if (prefs == null) return; string path = AssetDatabase.GetAssetPath (texture); var importer = AssetImporter.GetAtPath(path) as TextureImporter; // When we have text file data SpriteSlicingOptions slicingOptions = fileSettings.SlicingOptions; if (slicingOptions.ImportMode == SpriteImportMode.Multiple) { // Clamp cellSize to texture width and height slicingOptions.CellSize.x = Mathf.Min (texture.width, slicingOptions.CellSize.x); slicingOptions.CellSize.y = Mathf.Min (texture.height, slicingOptions.CellSize.y); SpriteMetaData[] spriteSheet; spriteSheet = SpriteSlicer.CreateSpriteSheetForTexture (AssetDatabase.LoadAssetAtPath(path, typeof(Texture2D)) as Texture2D, slicingOptions); // If we don't do this it won't update the new sprite meta data importer.spriteImportMode = SpriteImportMode.Single; importer.spriteImportMode = SpriteImportMode.Multiple; importer.spritesheet = spriteSheet; } else if (slicingOptions.ImportMode == SpriteImportMode.Single) { importer.spriteImportMode = SpriteImportMode.Single; } else if (slicingOptions.ImportMode == SpriteImportMode.None) { // Do nothing for None mode for now. } else { throw new System.NotSupportedException ("Encountered unsupported SpriteImportMode:" + slicingOptions.ImportMode); } TextureImporterSettings settings = new TextureImporterSettings(); importer.ReadTextureSettings(settings); importer.textureType = TextureImporterType.Advanced; settings.filterMode = prefs.FilterMode; settings.wrapMode = prefs.WrapMode; settings.mipmapEnabled = prefs.GenerateMipMaps; settings.textureFormat = prefs.TextureFormat; settings.maxTextureSize = prefs.MaxSize; settings.spritePixelsPerUnit = prefs.PixelsPerUnit; settings.spriteExtrude = (uint)Mathf.Clamp(prefs.ExtrudeEdges, 0, 32); settings.spriteMeshType = prefs.SpriteMeshType; // Settings also store Sprite Alignment for Single spritemode settings.spriteAlignment = (int)slicingOptions.Pivot; if (slicingOptions.Pivot == SpriteAlignment.Custom) { settings.spritePivot = slicingOptions.CustomPivot; } importer.spritePackingTag = fileSettings.PackingTag; importer.SetTextureSettings(settings); #if UNITY_5_0 importer.SaveAndReimport(); #else AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate); #endif EditorUtility.SetDirty(texture); WriteSpriteFileSettings (path, prefs.SpritesheetDataFile, texture.name + ".png", fileSettings); }
static void WriteSpriteFileSettings(string path, string dataFileName, string key, SpriteFileSettings spriteFileSettings) { string textAssetPath = Path.GetDirectoryName(path) + "/" + dataFileName; var spriteSheetDataFile = AssetDatabase.LoadAssetAtPath(textAssetPath, typeof(TextAsset)) as TextAsset; string newEntry = string.Concat (key, ", ", spriteFileSettings.ToString ()); // Create new file if none exists if (spriteSheetDataFile == null) { File.WriteAllText (textAssetPath, newEntry); } else { string existing = File.ReadAllText (textAssetPath); if (existing.Contains (key)) { string[] entries = spriteSheetDataFile.text.Split( new string[] { "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < entries.Length; i++) { if (entries[i].Contains(key)) { entries[i] = newEntry; } } File.WriteAllLines (textAssetPath, entries); } else { File.AppendAllText (textAssetPath, "\n" + newEntry); } } AssetDatabase.ImportAsset(textAssetPath, ImportAssetOptions.ForceUpdate); if (spriteSheetDataFile != null) { EditorUtility.SetDirty(spriteSheetDataFile); } }
void LoadFileSettings() { if (currentSelectedSettings == null) { return; } if (Selection.objects.Length == 0) { return; } // No need to load with multiple selections without multiobject support if (Selection.objects.Length > 1) { return; } if (!IsObjectValidTexture (Selection.activeObject)) { return; } FileSettings = LoadSavedFileSettingsForObject (Selection.activeObject); fileSettingsLoaded = true; }
void DrawFileSpecificSettings() { if (Selection.objects.Length > 1) { string slicingInfo = ""; foreach (var obj in Selection.objects) { if (IsObjectValidTexture (obj)) { FileSettings = LoadSavedFileSettingsForObject (obj); slicingInfo += "\n" + obj.name + ": " + FileSettings.SlicingOptions.ToDisplayString (); } } EditorGUILayout.HelpBox ("File Settings do not support Multiple-Object editing. " + " The following settings will be used:\n" + slicingInfo, MessageType.Info); return; } DrawSlicingOptions (); EditorGUILayout.Space(); EditorGUILayout.LabelField ("Override Settings", EditorStyles.boldLabel); if (FileSettings.SlicingOptions.ImportMode != SpriteImportMode.None) { DrawPivotOverride (); } DrawPackingTagOverride (); }
public static void ApplySpriteSettings(Texture2D texture, SpriteSettings prefs, SpriteFileSettings fileSettings) { if (prefs == null) { return; } string path = AssetDatabase.GetAssetPath(texture); var importer = AssetImporter.GetAtPath(path) as TextureImporter; // When we have text file data SpriteSlicingOptions slicingOptions = fileSettings.SlicingOptions; if (slicingOptions.ImportMode == SpriteImportMode.Multiple) { // Clamp cellSize to texture width and height slicingOptions.CellSize.x = Mathf.Min(texture.width, slicingOptions.CellSize.x); slicingOptions.CellSize.y = Mathf.Min(texture.height, slicingOptions.CellSize.y); SpriteMetaData[] spriteSheet; spriteSheet = SpriteSlicer.CreateSpriteSheetForTexture(AssetDatabase.LoadAssetAtPath(path, typeof(Texture2D)) as Texture2D, slicingOptions); // If we don't do this it won't update the new sprite meta data importer.spriteImportMode = SpriteImportMode.Single; importer.spriteImportMode = SpriteImportMode.Multiple; importer.spritesheet = spriteSheet; } else if (slicingOptions.ImportMode == SpriteImportMode.Single) { importer.spriteImportMode = SpriteImportMode.Single; } else if (slicingOptions.ImportMode == SpriteImportMode.None) { // Do nothing for None mode for now. } else { throw new System.NotSupportedException("Encountered unsupported SpriteImportMode:" + slicingOptions.ImportMode); } TextureImporterSettings settings = new TextureImporterSettings(); importer.ReadTextureSettings(settings); importer.textureType = TextureImporterType.Advanced; settings.filterMode = prefs.FilterMode; settings.wrapMode = prefs.WrapMode; settings.mipmapEnabled = prefs.GenerateMipMaps; settings.textureFormat = prefs.TextureFormat; settings.maxTextureSize = prefs.MaxSize; settings.spritePixelsPerUnit = prefs.PixelsPerUnit; settings.spriteExtrude = (uint)Mathf.Clamp(prefs.ExtrudeEdges, 0, 32); settings.spriteMeshType = prefs.SpriteMeshType; // Settings also store Sprite Alignment for Single spritemode settings.spriteAlignment = (int)slicingOptions.Pivot; if (slicingOptions.Pivot == SpriteAlignment.Custom) { settings.spritePivot = slicingOptions.CustomPivot; } importer.spritePackingTag = fileSettings.PackingTag; importer.SetTextureSettings(settings); #if UNITY_5_0 importer.SaveAndReimport(); #else AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate); #endif EditorUtility.SetDirty(texture); WriteSpriteFileSettings(path, prefs.SpritesheetDataFile, texture.name + ".png", fileSettings); }
static void WriteSpriteFileSettings(string path, string dataFileName, string key, SpriteFileSettings spriteFileSettings) { string textAssetPath = Path.GetDirectoryName(path) + "/" + dataFileName; var spriteSheetDataFile = AssetDatabase.LoadAssetAtPath(textAssetPath, typeof(TextAsset)) as TextAsset; string newEntry = string.Concat(key, ", ", spriteFileSettings.ToString()); // Create new file if none exists if (spriteSheetDataFile == null) { File.WriteAllText(textAssetPath, newEntry); } else { string existing = File.ReadAllText(textAssetPath); if (existing.Contains(key)) { string[] entries = spriteSheetDataFile.text.Split( new string[] { "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < entries.Length; i++) { if (entries[i].Contains(key)) { entries[i] = newEntry; } } File.WriteAllLines(textAssetPath, entries); } else { File.AppendAllText(textAssetPath, "\n" + newEntry); } } AssetDatabase.ImportAsset(textAssetPath, ImportAssetOptions.ForceUpdate); if (spriteSheetDataFile != null) { EditorUtility.SetDirty(spriteSheetDataFile); } }