Unity3D tips and tricks

1. Variables.

When the project is small it is not topical, but when the project is large and working on it a lot of people can forget that it is responsible for one or another variable that is set in the editor. Helps correct naming only partially, since too short. You can certainly write a Custom Editor and do there tips for each type, but each class is too unprofitable. The following method allows you to solve this problem. To do this, we make two classics:

TooltipAttribute.cs

using UnityEngine;

public class TooltipAttribute : PropertyAttribute
{
    public readonly string text;
    
    public TooltipAttribute(string text)
    {
        this.text = text;
    }
}

TooltipDrawer.cs

#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;

[CustomPropertyDrawer(typeof(TooltipAttribute))]
public class TooltipDrawer : PropertyDrawer
{
    public override void OnGUI(Rect position, SerializedProperty prop, GUIContent label) {
        var atr = (TooltipAttribute) attribute;
        var content = new GUIContent(label.text, atr.text);
        EditorGUI.PropertyField(position, prop, content);
    }
}
#endif

Now, if we need to in the standard inspector was a clue to the correct variable to us - just do this:

[Tooltip("The original color of the object")]
    public Color color;

    [Tooltip("The speed of the object, if negative - is in reverse")]
    public float speed;

Tooltip appears when you hover.

To not add garbage in a release code we can do so:

#if UNITY_EDITOR
    [Tooltip("The speed of the object, if negative - is in reverse")]
#endif
    public float speed;

 

2. Nullable types

Sometimes you need to check the value of the variable.

public class Character : MonoBehaviour 
{
    Vector3 targetPosition;
 
    void MoveTowardsTargetPosition()
    {
        if(targetPosition != Vector3.zero)
        {
            //Move towards the target position!
        }
    }
 
    public void SetTargetPosition(Vector3 newPosition)
    {
        targetPosition = newPosition;
    }
}

But what if our hero must come to the point (0, 0, 0)? Then, the following code does not fit.

It is necessary to use "nullable type." Simply add a '?' at the end of the same type and to check for any use HasValue, and to get the value - Value.

public class Character : MonoBehaviour 
{
    //Notice the added "?"
    Vector3? targetPosition;
 
    void MoveTowardsTargetPosition()
    {
        if (targetPosition.HasValue)
        {
            //Move towards the target position!
            //use targetPosition.Value for the actual value
        }
    }
 
    public void SetTargetPosition(Vector3 newPosition)
    {
        targetPosition = newPosition;
    }
}

3. Personal Log.

This reception can be useful for debugging AI, at least in my case it was so. The meaning of the reception is very simple - just not to dig a huge total log we start each unit individually, in our case it is a string (string localLog). All the important events in the life of the monster we write there, but you just have to see the monster highlight in the editor. Code to be displayed in the inspector personal log:

using UnityEngine;
using System.Collections;
using UnityEditor;

[CustomEditor(typeof(Monster))]
public class MonsterEditor : Editor {
    Vector2 scrollPos = new Vector2(0, Mathf.Infinity);

    public override void OnInspectorGUI()
    {
        serializedObject.Update();
        Monster monster = (Monster)target;

        if (Application.isPlaying) {
            scrollPos = GUILayout.BeginScrollView (
                scrollPos, GUILayout.Height (250));
            
            GUILayout.Label (monster.localLog);
            
            GUILayout.EndScrollView ();

            if (GUILayout.Button ("Clear"))
                monster.localLog = "";
        }

        serializedObject.ApplyModifiedProperties();
        DrawDefaultInspector();
    }
}

Now we see everything that monster thinking about us and other monsters. Such is the simple reception facilitates debugging.

I would be glad if someone would help.

 

All data posted on the site represents accessible information that can be browsed and downloaded for free from the web.

http://habrahabr.ru/post/239175/

 

User replies

No replies yet