ボタンの色を変更するエディタ拡張。
もくじ
ボタンの色を変更するスクリプト
ボタンを選択して、予め指定した色に変更をします。
ちなみにRGB値を指定したい場合 255fで割ります。
ボタンの色を変更するスクリプト
using UnityEngine;
using UnityEditor;
using UnityEngine.UI;
public class ButtonColorChange : MonoBehaviour {
[MenuItem("UITools/ボタンの色を変更", false, 1)]
static void ChangeButtonColor(MenuCommand command)
{
GameObject obj = Selection.activeGameObject;
var button = obj.GetComponent<Button>();
var colors = button.colors;
colors.normalColor = new Color(165f / 255f, 220f / 255f, 192f / 255f, 255f / 255f);
colors.highlightedColor = new Color(165f / 255f, 220f / 255f, 192f / 255f, 255f / 255f);
colors.pressedColor = new Color(165f / 255f, 220f / 255f, 192f / 255f, 255f / 255f);
colors.disabledColor = new Color(165f / 255f, 220f / 255f, 192f / 255f, 255f / 255f);
button.colors = colors;
Debug.Log(colors + " ボタンの色を変更しました。", button.gameObject);
}
}
ボタンのサイズを変更するスクリプト
using UnityEngine;
using UnityEditor;
using UnityEngine.UI;
public class ButtonSizeChange : MonoBehaviour
{
[MenuItem("UITools/ボタンのサイズを変更", false, 1)]
static void ChangeButtonColor(MenuCommand command)
{
foreach (GameObject obj in Selection.gameObjects)
{
RectTransform button = obj.GetComponent<RectTransform>();
Undo.RegisterCompleteObjectUndo(button, "Undo Size Change"); //Undo
button.sizeDelta = new Vector2(256, 64);
}
}
}
※ foreach (GameObject obj in Selection.gameObjects)
で複数選択に対応。