【Unity】Imageの簡易設定ツール
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEditor; | |
using UnityEngine; | |
using UnityEngine.UI; | |
using System.Linq; | |
using System.Collections.Generic; | |
using System.IO; | |
using System; | |
//================================================================================ | |
/// <summary> | |
/// uGUIのImageの設定ツール | |
/// </summary> | |
//================================================================================ | |
public class uGUIImageSetter : EditorWindow | |
{ | |
//================================================================================ | |
/* 定数 */ | |
#region -- 定数 | |
const string TOOL_NAME = "uGUIImageSetter"; | |
const int PREV_VALUE_DEFAULT = 40; | |
#endregion | |
//================================================================================ | |
/* フィールド */ | |
#region -- フィールド | |
/// <summary> | |
/// 設定するフォルダ | |
/// </summary> | |
DefaultAsset m_TargetFolder; | |
/// <summary> | |
/// フォルダ内のスプライト一覧 | |
/// </summary> | |
List<Sprite> m_SetSprites = new List<Sprite>(); | |
/// <summary> | |
/// 置き換えウィンドウのスクロールの位置の保持 | |
/// </summary> | |
Vector2 m_ScrollPos = Vector2.zero; | |
/// <summary> | |
/// 検索する名前 | |
/// </summary> | |
string m_FindName = ""; | |
/// <summary> | |
/// ページ内表示数を変更する? | |
/// </summary> | |
bool m_IsChangePrevValue = false; | |
/// <summary> | |
/// ページ内表示数 | |
/// </summary> | |
int m_PrevValue = PREV_VALUE_DEFAULT; | |
/// <summary> | |
/// 現在のページ数 | |
/// </summary> | |
int m_CurrentPage = 1; | |
/// <summary> | |
/// ページの最大数 | |
/// </summary> | |
int m_MaxPage; | |
#endregion | |
//================================================================================ | |
/* 標準関数 */ | |
#region -- 標準関数 | |
//================================================================================ | |
/// <summary> | |
/// メイン描画 | |
/// </summary> | |
void OnGUI() { | |
if (CommonEditorTools.DrawHeader("設定")) { | |
CommonEditorTools.BeginContents(false); | |
EditorGUILayout.HelpBox("対象フォルダ", MessageType.None); | |
m_TargetFolder = EditorGUILayout.ObjectField("", m_TargetFolder, typeof(DefaultAsset), false) as DefaultAsset; | |
EditorGUILayout.BeginHorizontal(); | |
{ | |
if (GUILayout.Button("選択オブジェのフォルダを設定")) { | |
Sprite sprite = GetSelectSprite (); | |
if (sprite != null) { | |
string path = AssetDatabase.GetAssetPath (sprite); | |
path = GetFolderName(path, "/"); | |
DefaultAsset folder = AssetDatabase.LoadAssetAtPath (path, typeof(DefaultAsset)) as DefaultAsset; | |
m_TargetFolder = folder; | |
} | |
} | |
// フォルダが設定されていなかったら更新させない | |
EditorGUI.BeginDisabledGroup(m_TargetFolder == null); | |
{ | |
if (GUILayout.Button("更新")) { | |
GUI.changed = true; | |
} | |
} | |
EditorGUI.EndDisabledGroup(); | |
} | |
EditorGUILayout.EndHorizontal(); | |
// 変更があった場合のみスプライトを更新 | |
if (GUI.changed) { | |
UpdateSprites(); | |
} | |
CommonEditorTools.EndContents(); | |
} | |
if (CommonEditorTools.DrawHeader("Sprite一覧")) { | |
CommonEditorTools.BeginContents(true); | |
// 対象フォルダが設定されていたら | |
if (m_TargetFolder != null) { | |
// 検索 | |
EditorGUILayout.BeginVertical(GUI.skin.box); | |
{ | |
EditorGUILayout.HelpBox("検索", MessageType.None); | |
EditorGUILayout.BeginHorizontal(); | |
{ | |
m_FindName = EditorGUILayout.TextField(m_FindName); | |
if (GUI.changed) { | |
if (m_FindName != "") { | |
FindNameSprites(m_FindName); | |
} else { | |
UpdateSprites(); | |
} | |
} | |
} | |
EditorGUILayout.EndHorizontal(); | |
} | |
EditorGUILayout.EndVertical(); | |
// ページ内表示数 -> 大量の画像を並べると表示がちらつくため40個程度の表示に絞る | |
EditorGUILayout.BeginVertical(GUI.skin.box); | |
{ | |
m_IsChangePrevValue = EditorGUILayout.ToggleLeft("Page内表示数を変更する?", m_IsChangePrevValue); | |
EditorGUI.BeginDisabledGroup(!m_IsChangePrevValue); | |
{ | |
m_PrevValue = EditorGUILayout.IntSlider(m_PrevValue, 1, m_SetSprites.Count); | |
} | |
EditorGUI.EndDisabledGroup(); | |
} | |
EditorGUILayout.EndVertical(); | |
m_MaxPage = Mathf.CeilToInt((float)m_SetSprites.Count / (float)m_PrevValue); | |
// ページ表示 | |
EditorGUILayout.BeginHorizontal(GUI.skin.box); | |
{ | |
EditorGUILayout.HelpBox("page : " + m_CurrentPage + " / " + m_MaxPage, MessageType.None); | |
EditorGUI.BeginDisabledGroup(m_CurrentPage == 1); | |
if (GUILayout.Button("◀", GUILayout.Width(20), GUILayout.Height(20))) { | |
m_CurrentPage--; | |
} | |
EditorGUI.EndDisabledGroup(); | |
EditorGUI.BeginDisabledGroup(m_CurrentPage == m_MaxPage); | |
if (GUILayout.Button("▶", GUILayout.Width(20), GUILayout.Height(20))) { | |
m_CurrentPage++; | |
} | |
EditorGUI.EndDisabledGroup(); | |
} | |
EditorGUILayout.EndVertical(); | |
// 対象フォルダにスプライトがあったら | |
if (m_SetSprites != null) { | |
m_ScrollPos = EditorGUILayout.BeginScrollView(m_ScrollPos, GUI.skin.box); | |
ContentsListUpReplace(); | |
EditorGUILayout.EndScrollView(); | |
} else { | |
EditorGUILayout.HelpBox("対象フォルダにSpriteがありません", MessageType.Warning); | |
} | |
} else { | |
EditorGUILayout.HelpBox("対象フォルダを設定してください", MessageType.Info); | |
} | |
CommonEditorTools.EndContents(); | |
} | |
} | |
#endregion | |
//================================================================================ | |
/* Public関数 */ | |
#region -- Public関数 | |
//================================================================================ | |
/// <summary> | |
/// メニューのWindowに追加 | |
/// </summary> | |
[MenuItem("Custom/uGUI/" + TOOL_NAME)] | |
public static void OpenWindow() { | |
EditorWindow.GetWindow<uGUIImageSetter>(TOOL_NAME); | |
} | |
#endregion | |
//================================================================================ | |
/* Private関数 */ | |
#region -- Private関数 | |
//================================================================================ | |
/// <summary> | |
/// 置き換えウィンドウのListを列挙する関数 | |
/// </summary> | |
/// <param name="title"></param> | |
/// <param name="list"></param> | |
private void ContentsListUpReplace() { | |
// リストが空じゃなければ | |
if (m_SetSprites.Count > 0) { | |
// 開始,終了箇所を計算 | |
int startCount = (m_CurrentPage - 1) * m_PrevValue; | |
int endCount = startCount + m_PrevValue; | |
if (endCount > m_SetSprites.Count) { | |
endCount = m_SetSprites.Count; | |
} | |
// リストを表示 | |
for (int i = startCount; i < endCount; i++) { | |
EditorGUILayout.BeginHorizontal(GUI.skin.box); | |
{ | |
// Spriteを編集させない | |
EditorGUI.BeginDisabledGroup(true); | |
Sprite spriteField = EditorGUILayout.ObjectField ("", m_SetSprites[i], typeof(Sprite), false, GUILayout.Width (50), GUILayout.Height (50)) as Sprite; | |
EditorGUI.EndDisabledGroup(); | |
EditorGUILayout.BeginVertical(GUI.skin.box); | |
{ | |
EditorGUILayout.HelpBox(m_SetSprites[i].name, MessageType.None); | |
if (GUILayout.Button("設定")) { | |
if (Selection.activeObject != null) { | |
Image replaceImage = Selection.activeGameObject.GetComponent<Image>(); | |
// Imageが付いてなかったら付ける | |
if (replaceImage == null) { | |
replaceImage = Selection.activeGameObject.AddComponent<Image>(); | |
} | |
Undo.RecordObject(replaceImage, TOOL_NAME + " - Set Sprite"); | |
replaceImage.sprite = spriteField; | |
// Sceneの再描画のためオブジェクトをOn/Off | |
replaceImage.gameObject.SetActive(false); | |
replaceImage.gameObject.SetActive(true); | |
} | |
} | |
} | |
EditorGUILayout.EndVertical(); | |
} | |
EditorGUILayout.EndHorizontal(); | |
} | |
} | |
} | |
//================================================================================ | |
/// <summary> | |
/// 選択オブジェのSpriteを取得 | |
/// </summary> | |
/// <returns>The select sprite name.</returns> | |
private Sprite GetSelectSprite() { | |
// 選択されていなかったら | |
if (Selection.activeGameObject == null) { | |
return null; | |
} | |
GameObject selectObj = Selection.activeGameObject; | |
// Imageのオブジェクトじゃなかったら | |
if (!selectObj.GetComponent<Image>()) { | |
return null; | |
} | |
Image selectImage = selectObj.GetComponent<Image> (); | |
// Spriteがなかったら | |
if (selectImage.sprite == null) { | |
return null; | |
} | |
return selectImage.sprite; | |
} | |
//================================================================================ | |
/// <summary> | |
/// パスからフォルダの名前を取得 | |
/// </summary> | |
/// <returns>The folder name.</returns> | |
/// <param name="_path">Path.</param> | |
/// <param name="_key">Key.</param> | |
private string GetFolderName(string _path, string _key) { | |
int findNum = _path.LastIndexOf (_key); | |
string folderName = _path.Substring (0, findNum); | |
return folderName; | |
} | |
//================================================================================ | |
/// <summary> | |
/// 対象Spriteを更新 | |
/// </summary> | |
private void UpdateSprites() { | |
m_SetSprites.Clear(); | |
string texPath = AssetDatabase.GetAssetPath (m_TargetFolder); | |
// フォルダ内の画像を一括で取得 | |
string folderPath = Application.dataPath + texPath.Replace("Assets", ""); | |
DirectoryInfo dir = new DirectoryInfo(folderPath); | |
FileInfo[] fileInfo = dir.GetFiles ("*.png"); | |
foreach (var file in fileInfo) { | |
Sprite sprite = AssetDatabase.LoadAssetAtPath (texPath + "/" + file.Name, typeof(Sprite)) as Sprite; | |
m_SetSprites.Add(sprite); | |
} | |
m_CurrentPage = 1; | |
m_PrevValue = PREV_VALUE_DEFAULT; | |
if (m_PrevValue > m_SetSprites.Count) { | |
m_PrevValue = m_SetSprites.Count; | |
} | |
} | |
//================================================================================ | |
/// <summary> | |
/// 指定名を含むSpriteを設定 | |
/// </summary> | |
/// <param name="_name"></param> | |
/// <returns></returns> | |
private void FindNameSprites(string _name) { | |
List<Sprite> sprites = new List<Sprite>(); | |
UpdateSprites(); | |
foreach (var sprite in m_SetSprites) { | |
// 指定文字列が含まれているか | |
if (sprite.name.IndexOf(_name, StringComparison.OrdinalIgnoreCase) > 0) { | |
sprites.Add(sprite); | |
} | |
} | |
m_SetSprites = sprites; | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment