Skip to content

Instantly share code, notes, and snippets.

@nkjzm
Last active February 6, 2020 16:16
Show Gist options
  • Save nkjzm/2961705cb185ca6749e25860da8999a9 to your computer and use it in GitHub Desktop.
Save nkjzm/2961705cb185ca6749e25860da8999a9 to your computer and use it in GitHub Desktop.
描画負荷がかからない透明なuGUI Image
using UnityEditor;
namespace UnityEngine.UI
{
/// <summary>
/// 描画しない透明Imageコンポーネント
/// </summary>
[AddComponentMenu("UI/NonRenderImage", 14)]
public class NoRenderImage : Graphic
{
protected override void OnPopulateMesh(VertexHelper vh) { vh.Clear(); }
#if UNITY_EDITOR
[UnityEditor.CustomEditor(typeof(NoRenderImage))]
class NonRenderImageEditor : UnityEditor.Editor
{
public override void OnInspectorGUI() { }
private static Vector2 s_ImageElementSize = new Vector2(100f, 100f);
[MenuItem("GameObject/UI/NoRender Image", false, 2003)]
public static void CreateNoRenderImage()
{
// 選択状態のGameObjectを取得する
var parent = Selection.activeGameObject?.transform;
// 親や祖先にCanvasが存在しない場合
if (parent == null || parent.GetComponentInParent<Canvas>() == null)
{
// 新規Canvasの生成
var canvas = new GameObject("Canvas");
canvas.transform.SetParent(parent);
// Canvasの初期化
canvas.AddComponent<Canvas>().renderMode = RenderMode.ScreenSpaceOverlay;
canvas.AddComponent<CanvasScaler>();
canvas.AddComponent<GraphicRaycaster>();
// 親の付け替え
parent = canvas.transform;
}
var go = new GameObject("NoRenderImage");
// RectTransformの初期化
var rectTransform = go.AddComponent<RectTransform>();
// 親コンポーネントの指定 (nullの場合はルートになるので問題ない)
rectTransform.SetParent(parent);
rectTransform.sizeDelta = s_ImageElementSize;
rectTransform.anchoredPosition = Vector2.zero;
// 生成したGameObjectを選択状態にする
Selection.activeGameObject = go;
// コンポーネントの追加
go.AddComponent<NoRenderImage>();
}
}
#endif
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment