📚EditPivotObject in Unity Editor

public class EditPivotObject : EditorWindow
{
    GameObject[] selectedObjects;
    PivotDirection convertPivotDirection;
    private ScrollView m_RightPane;
    private int m_SelectedIndex;

    [MenuItem("MyRoom/Edit Pivot")]
    public static void ShowWindow()
    {
        var window = EditorWindow.GetWindow(typeof(EditPivotObject), false, "Edit Pivot");
        window.maxSize = new Vector2(200, 180);
        window.minSize = new Vector2(200, 180);
    }

    private void CreateGUI()
    {
        var splitView = new TwoPaneSplitView(0, 100, TwoPaneSplitViewOrientation.Horizontal);
        rootVisualElement.Add(splitView);

        var leftPane = new ListView();
        splitView.Add(leftPane);
        m_RightPane = new ScrollView(ScrollViewMode.VerticalAndHorizontal);
        splitView.Add(m_RightPane);

        var pivotTypeList = Enum.GetNames(typeof(PivotDirection)).ToList();

        leftPane.makeItem = () => new Label();
        leftPane.bindItem = (item, index) =>
            {
                var label = (Label)item;
                label.text = pivotTypeList[index];
                label.style.unityTextAlign = TextAnchor.MiddleCenter;
            };
        leftPane.itemsSource = pivotTypeList;
        leftPane.itemHeight = 30;

        leftPane.onSelectionChange += OnIndexChange;

        leftPane.selectedIndex = m_SelectedIndex;

        leftPane.onSelectionChange += (items) => { m_SelectedIndex = leftPane.selectedIndex; };
    }

    void OnIndexChange(IEnumerable<object> selectedItems)
    {
        selectedObjects = Selection.gameObjects;
        
        m_RightPane.Clear();

        VisualElement convertBtn = new Button(() =>
        {
            foreach (var rootObj in selectedObjects)
            {
                AutoBoundPivot.SetChildObjByPivotDirection((PivotDirection)m_SelectedIndex, rootObj);
            }
        });
        convertBtn.Add(new Label("Convert"));

        VisualElement rollbackBtn = new Button(() =>
        {
            foreach (var rootObj in selectedObjects)
            {
                AutoBoundPivot.RollbackRootObject(rootObj);
            }
        });
        rollbackBtn.Add(new Label("Rollback"));

        m_RightPane.Add(convertBtn);
        m_RightPane.Add(rollbackBtn);
    }

    private void OnSelectionChange()
    {
        selectedObjects = Selection.gameObjects;
    }
}

Last updated