Rename Window - Hitomilras/com.rastleks.utilities GitHub Wiki

Rename Window is useful for creating custom editors.

For example, you want to add "R" button, that will rename something in custom window. It might be helpful if you want to give callback of renaming something only once, instead of getting this after deleting or adding any character.

Code example:

Editor script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(RenameWindowExample))]
public class RenameWindowExampleEditor : Editor
{

    private Rect renameButtonRect;

    public override void OnInspectorGUI()
    {
        serializedObject.Update();

        EditorGUILayout.BeginHorizontal();

        EditorGUILayout.PropertyField(serializedObject.FindProperty("exampleString"));

        if (GUILayout.Button("R", GUILayout.Width(22)))
            RenameWindow.RenameString(serializedObject.FindProperty("exampleString").stringValue, OnStringRenamed);

        EditorGUILayout.EndHorizontal();

        serializedObject.ApplyModifiedProperties();
    }

    private void OnStringRenamed(bool wasRenamed, string renamedSting)
    {
        serializedObject.Update();
        serializedObject.FindProperty("exampleString").stringValue = renamedSting;
        serializedObject.ApplyModifiedProperties();
    }

}

Runtime script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RenameWindowExample : MonoBehaviour
{

    public string exampleString = "rename me!";

}