Q&A
What is This?
Several of my posts, such as the Monster Inspector 1, were inspired by questions asked on Unity Answers. Below are some of the more interesting questions I have answered that haven’t been turned into a post (yet).
The Questions
Note: The questions and answers have been paraphrased here for brevity/clarity. Follow the links for more details.
How can I register a function to be called when a message is logged to the Debug console?
Question: How can I get a copy of the string that is logged to the Debug console?
Answer: You can add a callback to Application.logMessageReceived
. The signature should look like void YourFunctionName(string logString, string stackTrace, LogType type)
. The logString
parameter is the one that holds the actual message that was passed to Debug.Log
.
How can I persist data in a custom UIElements EditorWindow?
Question: I’m writing a custom UIElements EditorWindow where I have multiple fields to which I can input some values (TextField, Vector2Fields and such), but the values doesn’t get persisted the same way as in IMGUI and every time I restart Unity or change Play/Edit mode the values get reset.
Answer: To persist simple fields like an IntegerField
, you can use the view-data-key
attribute (UXML) or viewDataKey
(C#). For compound fields like Vector2Field
, you need to set the viewDataKey
of children elements also.
How can I detect mouseEnter, mouseLeave, mouseHover, etc. events with UIElements?
Question: I know that you can attach lets say a click event to a button via button.clicked += OnButtonClickEvent;
. But how do I do something similar for the mouseEnter, mouseLeave, mouseHover events on a panel or button?
Answer: You can use VisualElement.RegisterCallback<T>(EventCallback<T> callback)
, like button.RegisterCallback<MouseEnterEvent>(HandleMouseEnter)
. Includes demo with a label that responds to several mouse events.
How can I control an Editor Window that I didn’t write, like the Tilemap Editor?
Question: I’m using the Tile Palette, and I want to add custom hotkeys to speed up my work flow.
Answer: You can read the source code for the window you want to control to find the properties you want to change. If they are public, you’re good; if they’re private, you can still use System.Reflection
to modify them. Includes demo that sets two hotkeys to interact with the Tilemap window – one using public properties to change the active tilemap, and the other using Reflection to change the active palette.2
How can I determine if a Sprite Mask is showing a Sprite or not?
Question: What I want to get is to show specific enemies with my cursor, kind of like the Lens of Truth in the Legend of Zelda, and do some other stuff (play sounds, particle effects, etc) at the moment the sprite becomes visible using my “Lens of Truth”.
Answer: You can use colliders/triggers for your sprites and sprite mask. Includes demo with a spotlight that logs to the console upon revealing a hidden image.
How can I create a save/load system for player data?
Question: How can I create a save/load system for player inventory?
Answer: You can use a combination of a serializable class to hold the data you want to save, a serializer/deserializer, and a stream to (de)serialize. Includes demo that saves and loads a custom inventory class.
How can I change the label of a PropertyField in a PropretyDrawer using UIElements?
Question: I want to be able to create my own label for a PropertyField, but when using a custom PropertyDrawer the “label” argument of the PropertyField constructor seems to be entirely ignored. Further I can’t seem to find any “label” or “text” fields or the like.
Answer: The label argument does indeed seem to be ignored, but as a hack, you can use EditorCoroutineUtility.StartCoroutine
to set the label. Includes demo that demonstrates the problem and workaround.
The End?
Let me know if you’d like to see any of the answers above get turned into a more in-depth tutorial.
-
My username on Unity Answers is exploringunity. Here’s my answer to a question on using UIElements for a custom inspector, which inspired the Monster Inspector post. ↩︎
-
Since you have access to the source file, you could also just open it up and change
private
topublic
, but that would be boring. ↩︎