【Unity】UIのRayCast Targetをオフにして生成する

2021-07-26

UnityでUIの要素、ImageTextを生成するとRaycast Targetにチェックが入った状態で生成される。 しかし表示のみでタップ操作に反応しないUI要素にはチェックしないほうが負荷が低くてよい。 外れた状態で生成されるようにするにはどうするか。

エディタスクリプトを使う方法

次のページで紹介されているエディタスクリプトを配置することで、外した状態で作成される: 【Unity】ボタン以外の UI の raycastTarget をデフォルトで false にするエディタ拡張「GraphicDefaultValueWriter」紹介 - コガネブログ

エディタでの非実行状態で EditorApplication.hierarchyWindowChanged でシーンの変更を監視して、新しく生成されたGraphicに対して操作に反応しないものはRaycast Targetをオフにしている。

プリセットを使う方法

Unityにプリセットという仕組みがあって、コンポーネントの初期設定を保存しておける: 【Unity】uGUI のオブジェクト作成時に Raycast Target をデフォルトでオフにする方法 - コガネブログ

これを使ってRaycast Targetを外した状態をプリセットとして保存してデフォルトに指定すると、外れた状態で生成されるようになる。

(余談:TextのColorが反映されない。 FontSizeは反映されるが作成されるRectTransformの縦横サイズはデフォルトなのでWrapとTruncateに引っかかり表示されなくて焦る)

プリセットを使う方法の問題点

Imageオブジェクトの場合は外れて欲しいんだけど、プリセットはコンポーネントに対して効くようで、Scroll ViewInput Fieldを生成した場合にも反映されてしまう。 Raycast Targetのチェックが外れているとタップしてもフォーカスされずに操作できなくなってしまうので困る。

プリセットマネージャ の”Adding filters”で対処できるのか?、と思ったが 書式がわからず、作成しようとしているオブジェクトの種類でフィルタできるのかも不明で断念…。

対処法

メニューでScroll Viewなどを選択した場合の処理を乗っ取ってやればいけるかな、と試してみたらできた:

public class MyMenuOptions {
[MenuItem("GameObject/UI/Scroll View")]
public static void CreateScrollView(MenuCommand menuCommand) {
...

メニュー項目が同名だとワーニングが出てしまう( Cannot add menu item 'GameObject/UI/Scroll View' for method 'MenuOptions.AddScrollView because a menu item with the same name already exists.) これを回避するには、メニューを一段深くしてやる必要がある (【Unity】ボタン作成時のカスタム拡張 - 浮遊島)。

ソース

乗っ取ったメニュー内で最初はプレハブからでも生成しようかと思ったが、 UGUIのメニュー部分を参考にすればプレハブを用意せずに作成できた (privateで外部から呼び出せないためコピペで対応):

using UnityEditor;
using UnityEditor.Experimental.SceneManagement;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class MyMenuOptions
{
[MenuItem("GameObject/UI/Input Field/Create", false, 2036)] // <= 2037?
public static void AddInputField(MenuCommand menuCommand)
{
GameObject go = DefaultControls.CreateInputField(GetStandardResources());
foreach (var graphic in go.GetComponentsInChildren<Graphic>())
graphic.raycastTarget = graphic.gameObject == go;
PlaceUIElementRoot(go, menuCommand);
}

[MenuItem("GameObject/UI/Scroll View/Create", false, 2062)]
public static void CreateScrollView(MenuCommand menuCommand) {
var go = DefaultControls.CreateScrollView(GetStandardResources());
// Scroll ViewのルートにあるImageのRaycast Targetのみを有効に、その他はオフにする
foreach (var graphic in go.GetComponentsInChildren<Graphic>())
graphic.raycastTarget = graphic.gameObject == go;
PlaceUIElementRoot(go, menuCommand);
}

// 以下は UGUI のソースから拝借
// https://github.com/Unity-Technologies/uGUI/blob/2019.1/UnityEditor.UI/UI/MenuOptions.cs

private const string kUILayerName = "UI";

private const string kStandardSpritePath = "UI/Skin/UISprite.psd";
private const string kBackgroundSpritePath = "UI/Skin/Background.psd";
private const string kInputFieldBackgroundPath = "UI/Skin/InputFieldBackground.psd";
private const string kKnobPath = "UI/Skin/Knob.psd";
private const string kCheckmarkPath = "UI/Skin/Checkmark.psd";
private const string kDropdownArrowPath = "UI/Skin/DropdownArrow.psd";
private const string kMaskPath = "UI/Skin/UIMask.psd";

static private DefaultControls.Resources s_StandardResources;

static private DefaultControls.Resources GetStandardResources()
{
if (s_StandardResources.standard == null)
{
s_StandardResources.standard = AssetDatabase.GetBuiltinExtraResource<Sprite>(kStandardSpritePath);
s_StandardResources.background = AssetDatabase.GetBuiltinExtraResource<Sprite>(kBackgroundSpritePath);
s_StandardResources.inputField = AssetDatabase.GetBuiltinExtraResource<Sprite>(kInputFieldBackgroundPath);
s_StandardResources.knob = AssetDatabase.GetBuiltinExtraResource<Sprite>(kKnobPath);
s_StandardResources.checkmark = AssetDatabase.GetBuiltinExtraResource<Sprite>(kCheckmarkPath);
s_StandardResources.dropdown = AssetDatabase.GetBuiltinExtraResource<Sprite>(kDropdownArrowPath);
s_StandardResources.mask = AssetDatabase.GetBuiltinExtraResource<Sprite>(kMaskPath);
}
return s_StandardResources;
}

private static void SetPositionVisibleinSceneView(RectTransform canvasRTransform, RectTransform itemTransform)
{
SceneView sceneView = SceneView.lastActiveSceneView;

// Couldn't find a SceneView. Don't set position.
if (sceneView == null || sceneView.camera == null)
return;

// Create world space Plane from canvas position.
Vector2 localPlanePosition;
Camera camera = sceneView.camera;
Vector3 position = Vector3.zero;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRTransform, new Vector2(camera.pixelWidth / 2, camera.pixelHeight / 2), camera, out localPlanePosition))
{
// Adjust for canvas pivot
localPlanePosition.x = localPlanePosition.x + canvasRTransform.sizeDelta.x * canvasRTransform.pivot.x;
localPlanePosition.y = localPlanePosition.y + canvasRTransform.sizeDelta.y * canvasRTransform.pivot.y;

localPlanePosition.x = Mathf.Clamp(localPlanePosition.x, 0, canvasRTransform.sizeDelta.x);
localPlanePosition.y = Mathf.Clamp(localPlanePosition.y, 0, canvasRTransform.sizeDelta.y);

// Adjust for anchoring
position.x = localPlanePosition.x - canvasRTransform.sizeDelta.x * itemTransform.anchorMin.x;
position.y = localPlanePosition.y - canvasRTransform.sizeDelta.y * itemTransform.anchorMin.y;

Vector3 minLocalPosition;
minLocalPosition.x = canvasRTransform.sizeDelta.x * (0 - canvasRTransform.pivot.x) + itemTransform.sizeDelta.x * itemTransform.pivot.x;
minLocalPosition.y = canvasRTransform.sizeDelta.y * (0 - canvasRTransform.pivot.y) + itemTransform.sizeDelta.y * itemTransform.pivot.y;

Vector3 maxLocalPosition;
maxLocalPosition.x = canvasRTransform.sizeDelta.x * (1 - canvasRTransform.pivot.x) - itemTransform.sizeDelta.x * itemTransform.pivot.x;
maxLocalPosition.y = canvasRTransform.sizeDelta.y * (1 - canvasRTransform.pivot.y) - itemTransform.sizeDelta.y * itemTransform.pivot.y;

position.x = Mathf.Clamp(position.x, minLocalPosition.x, maxLocalPosition.x);
position.y = Mathf.Clamp(position.y, minLocalPosition.y, maxLocalPosition.y);
}

itemTransform.anchoredPosition = position;
itemTransform.localRotation = Quaternion.identity;
itemTransform.localScale = Vector3.one;
}

private static void PlaceUIElementRoot(GameObject element, MenuCommand menuCommand)
{
GameObject parent = menuCommand.context as GameObject;
bool explicitParentChoice = true;
if (parent == null)
{
parent = GetOrCreateCanvasGameObject();
explicitParentChoice = false;

// If in Prefab Mode, Canvas has to be part of Prefab contents,
// otherwise use Prefab root instead.
PrefabStage prefabStage = PrefabStageUtility.GetCurrentPrefabStage();
if (prefabStage != null && !prefabStage.IsPartOfPrefabContents(parent))
parent = prefabStage.prefabContentsRoot;
}
if (parent.GetComponentsInParent<Canvas>(true).Length == 0)
{
// Create canvas under context GameObject,
// and make that be the parent which UI element is added under.
GameObject canvas = CreateNewUI();
canvas.transform.SetParent(parent.transform, false);
parent = canvas;
}

// Setting the element to be a child of an element already in the scene should
// be sufficient to also move the element to that scene.
// However, it seems the element needs to be already in its destination scene when the
// RegisterCreatedObjectUndo is performed; otherwise the scene it was created in is dirtied.
SceneManager.MoveGameObjectToScene(element, parent.scene);

Undo.RegisterCreatedObjectUndo(element, "Create " + element.name);

if (element.transform.parent == null)
{
Undo.SetTransformParent(element.transform, parent.transform, "Parent " + element.name);
}

GameObjectUtility.EnsureUniqueNameForSibling(element);

// We have to fix up the undo name since the name of the object was only known after reparenting it.
Undo.SetCurrentGroupName("Create " + element.name);

GameObjectUtility.SetParentAndAlign(element, parent);
if (!explicitParentChoice) // not a context click, so center in sceneview
SetPositionVisibleinSceneView(parent.GetComponent<RectTransform>(), element.GetComponent<RectTransform>());

Selection.activeGameObject = element;
}

// Helper methods

private static GameObject CreateNewUI()
{
// Root for the UI
var root = new GameObject("Canvas");
root.layer = LayerMask.NameToLayer(kUILayerName);
Canvas canvas = root.AddComponent<Canvas>();
canvas.renderMode = RenderMode.ScreenSpaceOverlay;
root.AddComponent<CanvasScaler>();
root.AddComponent<GraphicRaycaster>();

// Works for all stages.
StageUtility.PlaceGameObjectInCurrentStage(root);
bool customScene = false;
PrefabStage prefabStage = PrefabStageUtility.GetCurrentPrefabStage();
if (prefabStage != null)
{
root.transform.SetParent(prefabStage.prefabContentsRoot.transform, false);
customScene = true;
}

Undo.RegisterCreatedObjectUndo(root, "Create " + root.name);

// If there is no event system add one...
// No need to place event system in custom scene as these are temporary anyway.
// It can be argued for or against placing it in the user scenes,
// but let's not modify scene user is not currently looking at.
if (!customScene)
CreateEventSystem(false);
return root;
}

private static void CreateEventSystem(bool select)
{
CreateEventSystem(select, null);
}

private static void CreateEventSystem(bool select, GameObject parent)
{
StageHandle stage = parent == null ? StageUtility.GetCurrentStageHandle() : StageUtility.GetStageHandle(parent);
var esys = stage.FindComponentOfType<EventSystem>();
if (esys == null)
{
var eventSystem = new GameObject("EventSystem");
if (parent == null)
StageUtility.PlaceGameObjectInCurrentStage(eventSystem);
else
GameObjectUtility.SetParentAndAlign(eventSystem, parent);
esys = eventSystem.AddComponent<EventSystem>();
eventSystem.AddComponent<StandaloneInputModule>();

Undo.RegisterCreatedObjectUndo(eventSystem, "Create " + eventSystem.name);
}

if (select && esys != null)
{
Selection.activeGameObject = esys.gameObject;
}
}

// Helper function that returns a Canvas GameObject; preferably a parent of the selection, or other existing Canvas.
private static GameObject GetOrCreateCanvasGameObject()
{
GameObject selectedGo = Selection.activeGameObject;

// Try to find a gameobject that is the selected GO or one if its parents.
Canvas canvas = (selectedGo != null) ? selectedGo.GetComponentInParent<Canvas>() : null;
if (IsValidCanvas(canvas))
return canvas.gameObject;

// No canvas in selection or its parents? Then use any valid canvas.
// We have to find all loaded Canvases, not just the ones in main scenes.
Canvas[] canvasArray = StageUtility.GetCurrentStageHandle().FindComponentsOfType<Canvas>();
for (int i = 0; i < canvasArray.Length; i++)
if (IsValidCanvas(canvasArray[i]))
return canvasArray[i].gameObject;

// No canvas in the scene at all? Then create a new one.
return CreateNewUI();
}

private static bool IsValidCanvas(Canvas canvas)
{
if (canvas == null || !canvas.gameObject.activeInHierarchy)
return false;

// It's important that the non-editable canvas from a prefab scene won't be rejected,
// but canvases not visible in the Hierarchy at all do. Don't check for HideAndDontSave.
if (EditorUtility.IsPersistent(canvas) || (canvas.hideFlags & HideFlags.HideInHierarchy) != 0)
return false;

if (StageUtility.GetStageHandle(canvas.gameObject) != StageUtility.GetCurrentStageHandle())
return false;

return true;
}
}
  • Input Field の場合は MenuItem 属性の3番目の引数に2036を、Scroll Viewには2062を与えることで デフォルトと同じ メニューの優先度になる。
    • Unity2020だと、Input Field2037?
  • しかしこれで作られるGameObjectに含まれるImageやTextはプリセットは無視してデフォルトのものが作られる (GetBuiltinExtraResourceを使用しているためだと思うが)

参考