Skip to content

Instantly share code, notes, and snippets.

@gamebox777
Last active December 5, 2019 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gamebox777/052a100ede6de5a16862db397cbc0015 to your computer and use it in GitHub Desktop.
Save gamebox777/052a100ede6de5a16862db397cbc0015 to your computer and use it in GitHub Desktop.
【unity】オブジェクトにラベルを表示するスクリプト【】c#
///
///著作権放棄:自由に使ってね
///使い方詳細は以下ページに記述しています
///http://www.unitygamebox.com/entry/2019/12/04/%E3%80%90unity%E3%80%91%E3%82%AA%E3%83%96%E3%82%B8%E3%82%A7%E3%82%AF%E3%83%88%E3%81%AB%E3%83%A9%E3%83%99%E3%83%AB%E3%82%92%E8%A1%A8%E7%A4%BA%E3%81%99%E3%82%8B%E3%80%90%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97
///
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 使い方:オブジェクトにこのコンポーネントをAddする
/// ※そのまま使うのではなく、自分で表示したい項目をカスタマイズする想定
/// </summary>
public class DebugObjectLabel : MonoBehaviour
{
private Camera mCamera;
private Color mBackColor = new Color(0,0,0,64);
public Vector3 AnchorOffset = new Vector3(0f, 0.6f, 0f);
private GUIStyle mStyle = null;
string mText;
private Vector2 mSize;
private void Start()
{
int FontSize = 12;
bool Bold = false;
mCamera = Camera.main;
mStyle = new GUIStyle();
Texture2D whiteTex = new Texture2D(1, 1);
whiteTex.SetPixel(0, 0, Color.white);
mStyle.normal.background = whiteTex;
mStyle.normal.textColor = Color.white ;
mStyle.fontSize = FontSize;
mStyle.fontStyle = Bold ? FontStyle.Bold : FontStyle.Normal;
mStyle.fixedWidth = 0;
mStyle.fixedHeight = 0;
mStyle.padding = new RectOffset(6, 6, 5, 5);
mStyle.wordWrap = true;
mStyle.alignment = TextAnchor.UpperLeft;
}
void OnGUI()
{
if (mCamera == null) { return; }
mText = "POS:" + transform.position.ToString() + "\n";
mText += "ROT:" + transform.rotation.ToString();
mSize = mStyle.CalcSize(new GUIContent(mText));
Vector2 guiPosition = mCamera.WorldToScreenPoint(transform.position - AnchorOffset);
guiPosition.y = Screen.height - guiPosition.y;
GUI.backgroundColor = mBackColor;
if (!string.IsNullOrEmpty(mText)) GUI.Label(new Rect(guiPosition.x, guiPosition.y, mSize.x, mSize.y), mText, mStyle);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment