Skip to content

Instantly share code, notes, and snippets.

@shanecelis
Last active May 7, 2019 14:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shanecelis/b012b573139ae83d40d93f6b402ab492 to your computer and use it in GitHub Desktop.
Save shanecelis/b012b573139ae83d40d93f6b402ab492 to your computer and use it in GitHub Desktop.
Show the `ToString()` of a MonoBehavior in the preview GUI. https://twitter.com/shanecelis/status/1123263828983201794
/* Original code[1] Copyright (c) 2019 Shane Celis[2]
Licensed under the MIT License[3]
This comment generated by code-cite[4].
[1]: https://gist.github.com/shanecelis/b012b573139ae83d40d93f6b402ab492
[2]: https://github.com/shanecelis
[3]: https://opensource.org/licenses/MIT
[4]: https://github.com/shanecelis/code-cite
*/
// #define USE_RICHSTRING
using UnityEngine;
using UnityEditor;
/** PreviewGuiEditor shows the `ToString()` of a MonoBehavior in the preview GUI
as demonstrated in this tweet[1]. Will work with any component. Just add an
attribute to this editor like so:
```
[CustomEditor(typeof(YourType))]
```
Will also work with any component that implements IRichString. Advise
placing the interface into a `Scripts` folder rather than an `Editor`
folder.
Modeled after Unity's EventSystemEditor[2].
[1]: https://twitter.com/shanecelis/status/1123263828983201794
[2]: https://bitbucket.org/Unity-Technologies/ui/raw/0651862509331da4e85f519de88c99d0529493a5/UnityEditor.UI/EventSystem/EventSystemEditor.cs
*/
// [CustomEditor(typeof(YourType))]
[CustomEditor(typeof(Objective), true)]
public class PreviewGUIEditor : Editor {
/** Update every 15th frame. */
private const int updateOnFrame = 15;
private GUIStyle _previewLabelStyle;
protected GUIStyle previewLabelStyle {
get {
if (_previewLabelStyle == null) {
_previewLabelStyle = new GUIStyle("PreOverlayLabel") {
richText = false,
alignment = TextAnchor.UpperLeft,
fontStyle = FontStyle.Normal
};
// Try to get a fixed-width font on macOS.
var font = Font.CreateDynamicFontFromOSFont("Monaco", 12);
// Failing that, try to get a fixed-width font on Windows.
if (font == null)
font = Font.CreateDynamicFontFromOSFont("Lucida Console", 12);
// XXX What fixed-width font should I request if we're on Linux?
if (font != null)
_previewLabelStyle.font = font;
// Debug.Log("Fonts: \n" + string.Join("\n", Font.GetOSInstalledFontNames()));
}
return _previewLabelStyle;
}
}
public override bool HasPreviewGUI() {
return Application.isPlaying;
}
public override bool RequiresConstantRepaint() {
// Only repaint on the nth frame.
return Application.isPlaying && Time.frameCount % updateOnFrame == 0;
}
public override void OnPreviewGUI(Rect rect, GUIStyle background) {
// Cast to more specialized type if necessary.
#if USE_RICHSTRING
string str;
var richString = target as IRichString;
if (richString != null) {
str = richString.ToRichString();
previewLabelStyle.richText = true;
} else if (target != null) {
str = target.ToString();
previewLabelStyle.richText = false;
} else {
// No string available to preview.
return;
}
#else
string str = target.ToString();
#endif
GUI.Label(rect, str, previewLabelStyle);
}
}
#if USE_RICHSTRING
/** This interface provides a string that is marked up with Unity's rich string
format[3], e.g., <b> for bold, <i> for italics, etc.
May need to copy and paste this interface into `IRichString.cs` in a
`Scripts` folder (not `Editor`).
[3]: https://docs.unity3d.com/Manual/StyledText.html
*/
public interface IRichString {
string ToRichString();
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment