Casting to dialogue

Just a quick tip, instead of

Dialogue dialogue = EditorUtility.InstanceIDToObject(instanceID) as Dialogue;
if (dialogue != null)
{
ShowEditorWindow();
return true;
}

one can inline it all including casting, which for some makes it more readable

if(EditorUtility.InstanceIDToObject(instanceID) is Dialogue dialogue)
{
ShowEditorWindow();
return true;
}

1 Like

A very nice tip, thank you.

Also, you don’t have to cast here.
if (EditorUtility.InstanceIDToObject(instanceID).GetType() == typeof(Dialogue))

1 Like

FWIW, shorter still and arguably even easier to read as it makes the “filtering out” up-front and more explicit:

if (!(EditorUtility.InstanceIDToObject(instanceID) is Dialogue) return false;
ShowEditorWindow();
return true;
1 Like

You can also use my preferred way the is not Type ParameterName

        [OnOpenAsset]
        public static bool OnOpenAsset(int instanceID, int line, int column)
        {
            if (EditorUtility.InstanceIDToObject(instanceID) is not Dialogue) //is not Dialogue dialogue if you need to use the parameter later
                return false;

            ShowEditorWindow();
            return true;
        }

Privacy & Terms