Unity3D Editor: tips and tricks

I decided to briefly describe in one document some ways to expand the editor, allowing us to make working with it more convenient.

The following points are mentioned in the publication:

  1. Display of the icon and text above the object in the scene;
  2. Display text or icons in the Project window;
  3. Templates for created scripts;
  4. Opening and creating a project through the explorer context menu;
  5. Adding event subscribers to the inspector.


I will warn in advance that in order to reduce the code in some scripts I made all fields public. In real projects, this is not worth doing.

Displaying icons and text over an object in a scene



The name of the object or icon can be displayed using the built-in editor tools: Gizmo and Icon Display Controls .

The icon can also be displayed using the Gizmos.DrawIcon method. The icon file should be located in the “Assets / Gizmos” folder.

public class IconExample : MonoBehaviour
{
    void OnDrawGizmos()
    {
        Gizmos.DrawIcon(transform.position, "Icon.png", true);
    }
} 

If you want to display your text, then you can use the following code:

public static class GizmosUtils
{
    public static void DrawText(GUISkin guiSkin, string text, Vector3 position, Color? color = null, int fontSize = 0, float yOffset = 0)
    {
#if UNITY_EDITOR
        var prevSkin = GUI.skin;
        if (guiSkin == null)
            Debug.LogWarning("editor warning: guiSkin parameter is null");
        else
            GUI.skin = guiSkin;
        GUIContent textContent = new GUIContent(text);
        GUIStyle style = (guiSkin != null) ? new GUIStyle(guiSkin.GetStyle("Label")) : new GUIStyle();
        if (color != null)
            style.normal.textColor = (Color)color;
        if (fontSize > 0)
            style.fontSize = fontSize;
        Vector2 textSize = style.CalcSize(textContent);
        Vector3 screenPoint = Camera.current.WorldToScreenPoint(position);
        if (screenPoint.z > 0) // проверка, необходимая чтобы текст не был виден, если  камера направлена в противоположную сторону относительно объекта
        {
            var worldPosition = Camera.current.ScreenToWorldPoint(new Vector3(screenPoint.x - textSize.x * 0.5f, screenPoint.y + textSize.y * 0.5f + yOffset, screenPoint.z));
            UnityEditor.Handles.Label(worldPosition, textContent, style);
        }
        GUI.skin = prevSkin;
#endif
    }
} 


public class GizmosTextExample : MonoBehaviour
{
    public float yOffset = 16;
    public Color textColor = Color.cyan;
    public int fontSize = 12;
    private void OnDrawGizmos()
    {
        GizmosUtils.DrawText(GUI.skin, "Custom text", transform.position, textColor, fontSize, yOffset);
    }
}


Display text or icons in the Project window



To do this, use the delegate EditorApplication.projectWindowItemOnGUI.
Below is an example of a little edited code found at the link: Extending the editor: Project window .

[InitializeOnLoad]
internal class CustomProjectWindow
{
    static readonly  Color labelColor = new Color(0.75f, 0.75f, 0.75f, 1.0f);
    static CustomProjectWindow()
    {
        EditorApplication.projectWindowItemOnGUI += OnProjectWindowGUI;
    }
    static void OnProjectWindowGUI(string pGUID, Rect pDrawingRect)
    {
        string assetpath = AssetDatabase.GUIDToAssetPath(pGUID);
        string extension = Path.GetExtension(assetpath);
        bool icons = pDrawingRect.height > 20;
        if (icons || assetpath.Length == 0)
            return;
        GUIStyle labelStyle = new GUIStyle(EditorStyles.label);
        Vector2 labelSize = labelStyle.CalcSize(new GUIContent(extension));
        Rect newRect = pDrawingRect;
        newRect.width += pDrawingRect.x;
        newRect.x = newRect.width - labelSize.x - 4;
        Color prevGuiColor = GUI.color;
        GUI.color = labelColor;
        GUI.Label(newRect, extension, labelStyle);
        GUI.color = prevGuiColor;
    }
}

Display text or icon in a Hierarchy window

Here, I’ll probably just leave a link to an article in which there is already a good example: “Tidying up the Hierarchy View” .

Scripting Templates



When creating a new script in the Project window, you can select one of the existing script templates for it.
If you want some specific code to be written when creating a new script, then these templates can be edited in advance or you can create your own.

Templates are usually located in the following folders:
Windows: \ Program Files \ Unity \ Editor \ Data \ Resources \ ScriptTemplates
OS X: /Applications/Unity/Unity.app/Contents/Resources/ScriptTemplates I will

describe the parameters in the template name "81-C # Script- NewBehaviourScript.cs ":
" 81 "- the serial number of the template in the context menu;
“C # Script” - display name in the context menu;
"NewBehaviourScript" - the default name of the created script.

Opening and creating a project through the explorer context menu



In Windows Explorer, you can add items to create or open an existing Unity3D project.

Ready-made registry files can be downloaded from the link (Tip # 63 and Tip # 71):
Tip of the day

Adding Event Subscribers in the Inspector



A little off topic, but I would like to describe a rather useful innovation.
In Unity 4.6, the inspector finally added the ability to assign event subscribers. Now you can transfer the objects to the list of subscribers and select the methods that we want to execute when this event is called.
To do this, it is enough to declare an event with the UnityEvent type or its successor in the script.

Example code that creates objects after a specified period of time:

public class InvokeRepeating : MonoBehaviour
{
    public UnityEvent onInvoke;
    public float repeatTime = 3f;
    public float startTime = 1f;
    void Start()
    {
        InvokeRepeating("Execute", startTime, repeatTime);
    }
    void Execute()
    {
        onInvoke.Invoke();
    }
}


public class CreateObject : MonoBehaviour
{
    public Transform Prefab;
    public void Execute()
    {
        Instantiate(Prefab, transform.position, transform.rotation);
    }
}


Farewell words

That, in general, is all that I wanted to write. I hope this publication will be useful to you.

Also popular now: