Skip to content

Instantly share code, notes, and snippets.

@YuukiTsuchida
Last active July 5, 2016 01:24
Show Gist options
  • Save YuukiTsuchida/f845d280b939a341591c to your computer and use it in GitHub Desktop.
Save YuukiTsuchida/f845d280b939a341591c to your computer and use it in GitHub Desktop.
PackngTag Setting Window
using UnityEngine;
using UnityEditor;
namespace EditorExtension
{
namespace Scope
{
public class ContentsScope : GUI.Scope
{
public ContentsScope()
{
GUILayout.BeginHorizontal();
EditorGUILayout.BeginHorizontal("AS TextArea", GUILayout.MinHeight(10f));
GUILayout.BeginVertical();
}
protected override void CloseScope()
{
GUILayout.EndVertical();
EditorGUILayout.EndHorizontal();
GUILayout.EndHorizontal();
}
}
}
}
using UnityEngine;
using UnityEditor;
using UnityEditor.Sprites;
using System.Linq;
using System.Collections.Generic;
namespace EditorExtension
{
public class PackingTagWindow : EditorWindow
{
private class Item
{
public string texturePath;
public bool delete;
public Item( string path )
{
texturePath = path;
delete = false;
}
}
private class SelectionSprite
{
public Object selectedObject;
public string assetsPath;
public SelectionSprite( Object obj, string path )
{
selectedObject = obj;
assetsPath = path;
}
}
private Vector2 scrollPosition = Vector2.zero;
private Vector2 selectedSpriteScrollPosition = Vector2.zero;
private int selectedIndex_ = 0;
private Dictionary<string, List<Item> > textureList = new Dictionary<string, List<Item> >();
private List<string> popupList = new List<string>();
private string packingTag_ = string.Empty;
[MenuItem("Tool/PackingTagWindow")]
public static void ShowWindow()
{
var window = GetWindow<PackingTagWindow>();
window.title = "PackingTagWindow";
window.Show();
}
public void OnFocus()
{
Init();
Repaint();
}
public void OnGUI()
{
GUILayout.Label( "SETTING", "PreToolbar" );
List<SelectionSprite> selectionSprite = null;
int itemCount = 0;
using( new EditorExtension.Scope.ContentsScope() )
{
GUILayout.Space(2f);
int currentIndex = EditorGUILayout.Popup( "PackingTag", selectedIndex_, popupList.ToArray() );
if( currentIndex != selectedIndex_ )
{
selectedIndex_ = currentIndex;
packingTag_ = string.Empty;
}
if( popupList[selectedIndex_] == "New" )
{
packingTag_ = EditorGUILayout.TextField( "New Packing Tag Name", packingTag_ );
itemCount = 0;
}
else
{
packingTag_ = popupList[selectedIndex_];
itemCount = textureList[ popupList[selectedIndex_] ].Count;
}
selectionSprite = GetSelectionSprite();
using( new EditorGUI.DisabledGroupScope( selectionSprite.Count == 0 ) )
{
if( GUILayout.Button( "SET" ) )
{
foreach( var sprite in selectionSprite )
{
TextureImporter textureImporter = AssetImporter.GetAtPath( sprite.assetsPath ) as TextureImporter;
textureImporter.spritePackingTag = packingTag_;
EditorUtility.SetDirty( textureImporter );
AssetDatabase.ImportAsset( sprite.assetsPath, ImportAssetOptions.ForceUpdate );
}
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
Packer.RebuildAtlasCacheIfNeeded( EditorUserBuildSettings.activeBuildTarget, true );
Init();
return;
}
}
}
if( packingTag_ == string.Empty )
{
EditorGUILayout.HelpBox( "Empty Pacing Tag", MessageType.Warning );
}
EditorGUILayout.Space();
GUILayout.Label( "Sprite List", "PreToolbar" );
using( var scrollView = new GUILayout.ScrollViewScope( scrollPosition, GUILayout.MinHeight( 100 ) ) )
{
scrollPosition = scrollView.scrollPosition;
GUIStyle labelStyle = GUI.skin.GetStyle( "AppToolbar" );
labelStyle.fontSize = 15;
for( int i = 0; i < itemCount; ++i )
{
Item item = textureList[ popupList[selectedIndex_] ][i];
if( i % 2 == 0 )
{
GUI.backgroundColor = new Color( 0.7f, 0.7f, 0.7f );
}
else
{
GUI.backgroundColor = Color.white;
}
if( GUILayout.Button( item.texturePath, labelStyle, GUILayout.MaxWidth( position.width ) ) )
{
EditorGUIUtility.PingObject( AssetDatabase.LoadAssetAtPath( item.texturePath, typeof(Texture2D) ) );
}
}
labelStyle.normal.textColor = Color.white;
labelStyle.fontSize = 0;
}
GUI.backgroundColor = Color.white;
EditorGUILayout.Space();
GUILayout.Label( "New Sprite", "PreToolbar" );
using( var scrollView = new GUILayout.ScrollViewScope( selectedSpriteScrollPosition, GUILayout.MinHeight( 100 ) ) )
{
selectedSpriteScrollPosition = scrollView.scrollPosition;
GUIStyle labelStyle = GUI.skin.GetStyle( "AppToolbar" );
labelStyle.fontSize = 15;
labelStyle.normal.textColor = Color.red;
for( int i = 0; i < selectionSprite.Count; ++i )
{
if( i % 2 == 0 )
{
GUI.backgroundColor = new Color( 0.7f, 0.7f, 0.7f );
}
else
{
GUI.backgroundColor = Color.white;
}
GUILayout.Label( selectionSprite[i].assetsPath, labelStyle, GUILayout.MaxWidth( position.width ) );
}
GUI.backgroundColor = Color.white;
labelStyle.normal.textColor = Color.white;
labelStyle.fontSize = 0;
}
using( new GUILayout.HorizontalScope() )
{
if( !string.IsNullOrEmpty( packingTag_ ) )
{
foreach( string atlasName in Packer.atlasNames )
{
if( atlasName.IndexOf( packingTag_ ) != -1 )
{
Rect rect = EditorGUILayout.GetControlRect();
rect.height = position.height - rect.y;
GUI.Label( rect, Packer.GetTexturesForAtlas( atlasName )[0], "AnimationEventBackground" );
}
}
}
}
// GUILayout.Label( UnityEditor.Sprites.Packer.GetTexturesForAtlas( UnityEditor.Sprites.Packer.atlasNames[0] )[0] );
}
// 情報を取得して初期化を行う
private void Init()
{
textureList.Clear();
popupList.Clear();
selectedIndex_ = 0;
foreach( var textureGUID in GetTexture2D() )
{
string texturePath = AssetDatabase.GUIDToAssetPath( textureGUID );
TextureImporter textureImporter = TextureImporter.GetAtPath( texturePath ) as TextureImporter;
if( !textureList.ContainsKey( textureImporter.spritePackingTag ) )
{
textureList.Add( textureImporter.spritePackingTag, new List<Item>() );
popupList.Add( textureImporter.spritePackingTag );
if( packingTag_ == textureImporter.spritePackingTag )
{
Debug.Log( textureImporter.spritePackingTag );
Debug.Log( texturePath );
selectedIndex_ = popupList.Count - 1;
}
}
textureList[textureImporter.spritePackingTag].Add( new Item( texturePath ) );
}
popupList.Add( "New" );
textureList.Add( "New", new List<Item>() );
}
private IEnumerable<string> GetTexture2D()
{
string[] objects = AssetDatabase.FindAssets( "t:texture2D" );
return objects.Where( (string textureGUID ) =>
{
string texturePath = AssetDatabase.GUIDToAssetPath( textureGUID );
TextureImporter textureImporter = TextureImporter.GetAtPath( texturePath ) as TextureImporter;
if( textureImporter == null )
{
return false;
}
return !string.IsNullOrEmpty( textureImporter.spritePackingTag );
} );
}
private List<SelectionSprite> GetSelectionSprite()
{
List<SelectionSprite> list = new List<SelectionSprite>();
foreach( Object selectedObject in Selection.objects.OfType<Texture2D>() )
{
string path = AssetDatabase.GetAssetPath( selectedObject );
TextureImporter textureImporter = AssetImporter.GetAtPath( path ) as TextureImporter;
// Unity側がサポートしてないテクスチャがある場合nullになるため
// ここで回避
if( textureImporter == null )
{
continue;
}
if( textureImporter.textureType == TextureImporterType.Sprite )
{
if( textureList[ popupList[selectedIndex_] ].Find( x => x.texturePath == path ) == null )
{
list.Add( new SelectionSprite( selectedObject, path ) );
}
}
}
return list;
}
public void OnSelectionChange()
{
Repaint();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment