EditorWindow OnBecameVisible and OnBecameInvisible

OnBecameVisible and OnBecameInvisible are in the Unity docs as methods of the Renderer and MonoBehaviour classes. It turns out that they work for EditorWindows, too!

They get called when a docked window changes visibility; meaning that when a docked window is hidden by a sibling window within that dock, OnBecameInvisible gets called. Similarly, when the window becomes visible again, of course, OnBecameVisible is called.

using UnityEditor;
using UnityEngine;


public class OnBecameVisibleEditorWindow : EditorWindow
{
	private void OnBecameVisible()
	{
		Debug.Log("Window is visible");
	}

	private void OnBecameInvisible()
	{
		Debug.Log("Window is invisible");
	}


	[MenuItem("Tools/OnBecameVisible Window")]
	public static void Init_ToolsMenu()
	{
		EditorWindow window = EditorWindow.GetWindow<OnBecameVisibleEditorWindow>();
		window.title = "OnBecameVisible";
		window.Show();
	}
}

There is a little gotcha, though. It seems that both methods get called when docking and undocking the window. Not sure why, really. It’s just another Unity quirk!

EditorWindow Inspector Lock Icon

The Inspector and Project windows both have lock icons beside their context menu buttons, like this:

unitytips_showbutton01

I’m going to demonstrate how to make one for your own EditorWindow. Simply implement the method ShowButton(Rect rect), and handle a button in there. Some code:

using UnityEditor;
using UnityEngine;


public class ShowButtonEditorWindow : EditorWindow
{
	private GUIStyle m_IconStyle = new GUIStyle();


	private void OnEnable()
	{
		//NOTE: this is the little pac man from the game view tab
		Texture2D icon = EditorGUIUtility.FindTexture("UnityEditor.GameView");
		m_IconStyle.normal.background = icon;
	}

	private void ShowButton(Rect rect)
	{
		if (GUI.Button(rect, GUIContent.none, m_IconStyle))
		{
			Debug.Log("Icon clicked: " + rect);
		}

		//NOTE: you /could/ add extra buttons like this, but
		//		unity doesn't really make room for that, so
		//		there will be some anomalies
		//rect.x -= 16.0f;

		//if (GUI.Button(rect, GUIContent.none, m_IconStyle))
		//{
		//	Debug.Log("Icon clicked");
		//}
	}


	[MenuItem("Tools/Show Button Window")]
	public static void Init_ToolsMenu()
	{
		EditorWindow window = EditorWindow.GetWindow<ShowButtonEditorWindow>();
		window.title = "Show Button";
		window.Show();
	}
}

This will create a button that has the little Pac Man icon from the Game View window that, when clicked, simply spits out a debug log message.

unitytips_showbutton02

The code that goes inside the body of ShowButton is completely up to you. It’s probably best to stick to the GUI functions and avoid all of the layout stuff.

I’d like to add some notes here about the Rect that is passed to ShowButton. It is created by the Unity Editor’s internal code, and is intended to be used as-is without modification. The x and y are automatically set for you and the dimensions are 16×16. You could technically change the values of the Rect to whatever you want. It’s not really advantageous to do so since it’s possible that the button will not fit correctly into the titlebar. The example code I’ve listed has a commented out section that demonstrates how to make a second button. If you are curious, uncomment that section and open the window. Resize the window to its smallest. Notice the overlap?

unitytips_showbutton03

I can’t recommend doing this, especially if you’re making a tool that will go on the Asset Store. It’s your window, however, so if you need this for whatever reason, it’s available.

EditorWindow Custom Context Menu

Context-clicking an EditorWindow tab shows the context menu for that window. The standard context menu has the items: Maximize, Close Tab, and Add Tab. Some of the built-in windows have extra items in their context menus, such as the Inspector which also has: Normal, Debug, and Lock.

unitytips_custommenu03

I’m going to show you how to add your own custom menu items to your window’s context menu. It’s really simple. You just have to implement the IHasCustomMenu interface like so.

using UnityEditor;
using UnityEngine;


public class CustomMenuEditorWindow : EditorWindow, IHasCustomMenu
{
	private GUIContent m_MenuItem1 = new GUIContent("Menu Item 1");
	private GUIContent m_MenuItem2 = new GUIContent("Menu Item 2");

	private bool m_Item2On = false;


	//Implement IHasCustomMenu.AddItemsToMenu
	public void AddItemsToMenu(GenericMenu menu)
	{
		menu.AddItem(m_MenuItem1, false, MenuItem1Selected);
		menu.AddItem(m_MenuItem2, m_Item2On, MenuItem2Selected);

		//NOTE: do not show the menu after adding items,
		//		Unity will do that after adding the default
		//		items: maximize, close tab, add tab >
	}

	private void MenuItem1Selected()
	{
		Debug.Log("Menu Item 1 selected");
	}

	private void MenuItem2Selected()
	{
		m_Item2On = !m_Item2On;

		Debug.Log("Menu Item 2 is " + m_Item2On);
	}


	[MenuItem("Tools/Custom Menu Window")]
	public static void Init_ToolsMenu()
	{
		EditorWindow window = EditorWindow.GetWindow<CustomMenuEditorWindow>();
		window.title = "Custom Menu";
		window.Show();
	}
}

 

The method of note is AddItemsToMenu(GenericMenu menu). Inside the method body, add the menu items you need in order from top to bottom. If you need to, take a moment to read up on GenericMenu.

Here are the results of the code above.

unitytips_custommenu01  unitytips_custommenu02

Context-clicking the window’s tab will show the menu as will clicking the little icon on the right over there. If an item should be checked, just pass a value of true to menu.AddItem() as the on parameter. See Menu Item 2 in the example.

JumpTo is on the Asset Store!

As of October 16th, 2014, JumpTo is available for purchase through the Unity Asset Store!

Here is a direct Asset Store Link

Link to the Unity Forum thread

Posted on 2014-10-19, 6:27 pm By
Categories: JumpTo Tags:
Sooooooo… JumpTo has competition

Okay, I’ve put up a page with a features video for JumpTo. Began writing a quick how-to document to distribute as a PDF with the assembly.

And then I decided to take another look on the Asset Store to see if there was anything even similar. And, yeah, there is! There wasn’t when I started working on JumpTo. That’s why I even bothered. But there it is. With a few other features, even. Not happy right now. I shouldn’t be surprised, really. The other night I was telling a friend that it seemed like such an obviously missing feature in Unity. So of course someone else would make a favorites tool!

But, ya know what? I’m going to submit JumpTo to the Asset Store anyway. I’ve worked too damned hard on it not to try and release it.

Posted on 2014-10-07, 5:57 pm By
Categories: JumpTo Tags:
JumpTo submission on hold due to major bug

I thought JumpTo was pretty much ready to be submitted to the Unity Asset Store for approval. I had been working on a video to show some of JumpTo’s features, and, while recording the window using CamStudio, I ran into a major showstopping bug.

Some set of circumstances causes the Hierarchy Links to reload from the save file when the scene is saved. Or, if no Links were saved for that scene, the Hierarchy Links will clear. So far, it’s difficult to reproduce. But the bug goes against the expected workflow while using JumpTo. Saving the scene should never cause the user to lose their JumpTo Links!

So, I’m delaying submission to the Asset Store until the issue is definitely fixed.

Posted on 2014-10-04, 5:38 pm By
Categories: JumpTo Tags:
JumpTo Logo

I’ve created a logo for JumpTo.

jumpto_logo

It’s pretty simple, and I don’t know if it says “Unity Object shortcut” or not. But, it’s what I came up with. The pale blue cube should be recognizable as the Unity prefab icon. It’s paired with the yellow star, which is common web browser iconography for a favorited/bookmarked item.

Created with Inkscape as an SVG. I used the cube tool and the star tool along with some extra polygon shapes (for the blue background) and gradients. By no means am I a vector graphics artist or an expert with Inkscape. In fact, the Imp Rock logo was pretty much my first effort with the software. So, don’t laugh. Or do. I don’t care.

I needed a logo for the Unity Asset Store imagery. Hopefully, I’ll be submitting JumpTo soon. Still haven’t really gotten to those tutorials and feature descriptions, though, and I want to have those up first.

Also added an icon to JumpTo’s EditorWindow tab. It’s a little yellow star tabicon. I thought it would help the window fit in with the standard tabs a little better.

jumpto_tabwithicon

 

Posted on 2014-09-28, 3:33 pm By
Categories: JumpTo Tags:
Web Page Woes

Imp Rock is not my first web page adventure. Though, this is true only because I’m counting the Geocities page I had way back around 1995 or ’96. I had to actually write HTML by hand for that thing! And it was crap! Things are different now. Way different. Like, I-have-no-idea-what-I’m-doing different.

Enter WordPress. Now, I’ve tinkered with a WordPress blog before. That was a few years ago and it never went anywhere. Mainly it was just to see what it was about. I never really got past the part where you look at different themes to find one that you like and then make a few posts.

For Imp Rock, I have a few needs that need to be met by the theme, and for the life of me I couldn’t just find one. “Okay, ” I thought, “not a problem. I’ll just find a theme that’s tolerable and bend it to my will using my programming prowess.” Well, that hard and lean programming prowess quickly turned to lumpy jelly because I had no idea how WordPress worked under the hood. Never really looked at CSS before. And PHP? That was a minefield that I’ve never had the heart to cross.

After several days of tutorials, codex pages, and just randomly running into information that was relevant, I’ve got…something. It’s close enough to what I wanted that I can actually get some work done. CSS isn’t too bad, but I’m still avoiding that PHP minefield for now.

Over the next few days, I’m looking to put up a page about JumpTo, along with a feature showcase and a sweet, sweet help section. While the web content is being created, I’d like to get the Unity Asset Store publishing process started.

Posted on 2014-09-21, 2:51 pm By
Categories: Imp Rock Tags: ,
EditorWindow Maximize/Unmaximize

Unity does a weird thing with docked windows when using Maximize, then Unmaximize (uncheck Maximize). When a docked window is maximized, either through the context menu or the space bar, Unity calls OnFocus and OnLostFocus a few times in a row ending with the window focused. When the window is restored, a new instance of the window gets created but never actually becomes visible.

Using Debug logs, what I’m seeing is this sequence

  1. OnEnable on new window
  2. OnFocus on new window
  3. OnLostFocus on old window
  4. OnLostFocus on new window
  5. OnFocus on old window
  6. OnFocus on old window again
  7. OnDisable on new window
  8. OnDestroy on new window
  9. OnFocus on old window

As far as I can tell, the UnityEditor.WindowLayout.Unmaximize() function is the culprit. It’s loading some kind of serialized copy of the window stored as an editor layout. I guess it was created when the window was maximized in order to preserve the window’s dimensions and docked position. Then, when it restores from the layout, it has to deserialize it from the layout file which essentially makes a new instance. It then has to destroy that new instance.

You might be asking, so what? Well, if you have a window that tries to preserve its data by using ScriptableObjects for when the window gets closed and reopened, it matters. Because when the new window instance is created in the background it “steals” the data references and then silently kills them by orphaning those references. So, the old window, the one you are left with has a bunch of dead ScriptableObject references that don’t actually point to your data.

I ran into this with JumpTo. Gonna have to refactor a good bit to work around it.

The Birth of Imp Rock

Welcome to improck.com, home of Imp Rock, which is made from the phrase “improved processes.” Currently, Imp Rock is composed of myself, Dean Lackey, and no one else. For the longest time, I have wanted to focus my career on building software tools that help to streamline the process of working in game development technologies. That’s what Imp Rock is about.

Wish me luck!

Posted on 2014-09-13, 5:50 am By
Categories: Imp Rock Tags: ,

Previous Page