うにてぃブログ

主にUnityとC#に関する記事を書いていきます

【Unity】TreeView の Override メソッド一覧

BuildRoot
/// <summary>
/// Root となる TreeViewItem を返す
/// </summary>
protected abstract TreeViewItem BuildRoot();
BuildRows
/// <summary>
/// 表示させたい要素を返す
/// </summary>
protected virtual IList<TreeViewItem> BuildRows(TreeViewItem root)
{
    if (this.m_DefaultRows == null)
        this.m_DefaultRows = new List<TreeViewItem>(100);
    this.m_DefaultRows.Clear();
    if (this.hasSearch)
        this.m_DataSource.SearchFullTree(this.searchString, this.m_DefaultRows);
    else
        this.AddExpandedRows(root, (IList<TreeViewItem>) this.m_DefaultRows);
    return (IList<TreeViewItem>) this.m_DefaultRows;
}
GetRows
/// <summary>
/// 現在の要素一覧を取得する
/// </summary>
public virtual IList<TreeViewItem> GetRows()
{
      return !this.isInitialized ? (IList<TreeViewItem>) null : this.m_TreeView.data.GetRows();
}
OnGUI
/// <summary>
/// TreeView の描画処理
/// </summary>
public virtual void OnGUI(Rect rect)
{
    if (!this.ValidTreeView())
        return;
    this.m_TreeView.OnEvent();
    if (this.showBorder)
        rect = this.m_GUI.DoBorder(rect);
    if (this.m_MultiColumnHeader != null)
        this.TreeViewWithMultiColumnHeader(rect);
    else
        this.m_TreeView.OnGUI(rect, this.m_TreeViewKeyControlID);
    this.CommandEventHandling();
}
SelectionChanged
/// <summary>
/// 要素を選択するたびに呼び出させる
/// </summary>
protected virtual void SelectionChanged(IList<int> selectedIds)
{
}
SingleClickedItem
/// <summary>
/// 要素のクリックイベントが発行されたタイミングで呼び出される
/// </summary>
protected virtual void SingleClickedItem(int id)
{
}
DoubleClickedItem
/// <summary>
/// 要素のダブルクリックイベントが発行されたタイミングで呼び出される
/// </summary>
protected virtual void DoubleClickedItem(int id)
{
}
ContextClickedItem
/// <summary>
/// 要素を右クリックした際
/// </summary>
protected virtual void ContextClickedItem(int id)
{
}

中身を実装する場合は以下のように書く

protected override void ContextClickedItem(int id)
{
    var ev = Event.current;
    ev.Use();
    
    var menu = new GenericMenu();
    menu.AddItem(...);
    menu.ShowAsContext();
}
ContextClicked
/// <summary>
/// 要素以外の場所で右クリックした際
/// </summary>
protected virtual void ContextClicked()
{
}
ExpandedStateChanged
/// <summary>
/// expanded もしくは collapsed した際に呼び出される
/// </summary>
protected virtual void ExpandedStateChanged()
{
}
SearchChanged
/// <summary>
/// searchString の 値が変わったタイミングで呼び出される
/// フィルター処理は別で行うので、その他の処理を行う場合
/// </summary>
protected virtual void SearchChanged(string newSearch)
{
}
        
KeyEvent
/// <summary>
/// TreeView で KeyEvent を発行した際に呼び出させる
/// </summary>
protected virtual void KeyEvent()
{
}
GetDescendantsThatHaveChildren
/// <summary>
/// すべての子のIDを返す
/// </summary>
protected virtual IList<int> GetDescendantsThatHaveChildren(int id)
{
    HashSet<int> intSet = new HashSet<int>();
    TreeViewUtility.GetParentsBelowItem(this.FindItem(id), intSet);
    return (IList<int>) intSet.ToArray<int>();
}
CanMultiSelect
/// <summary>
/// 複数選択を有効にするか
/// </summary>
protected virtual bool CanMultiSelect(TreeViewItem item)
{
    return true;
}
CanRename
/// <summary>
/// TreeViewItem.displayName の変更を許可するか
/// </summary>
protected virtual bool CanRename(TreeViewItem item)
{
    return false;
}
RenameEnded
/// <summary>
/// 名前変更後の処理
/// </summary>
protected virtual void RenameEnded(TreeView.RenameEndedArgs args)
{
}
CanStartDrag
/// <summary>
/// ドラッグアンドドロップを許可するか
/// </summary>
protected virtual bool CanStartDrag(TreeView.CanStartDragArgs args)
{
    return false;
}
SetupDragAndDrop
/// <summary>
/// ドラッグアンドドロップ処理を記述する
/// </summary>
protected virtual void SetupDragAndDrop(TreeView.SetupDragAndDropArgs args)
{
}

処理例

protected override void SetupDragAndDrop(TreeView.SetupDragAndDropArgs args)
{
    DragAndDrop.PrepareStartDrag();
    DragAndDrop.objectReferences = ...;
    DragAndDrop.SetGenericData(...);
    DragAndDrop.StartDrag(...)
}
HandleDragAndDrop
/// <summary>
/// ドラッグアンドドロップ時のアイコン選択
/// </summary>
protected virtual DragAndDropVisualMode HandleDragAndDrop(TreeView.DragAndDropArgs args)
{
    return DragAndDropVisualMode.None;
}
CanBeParent
/// <summary>
/// 要素が Parent になれるかどうか
/// </summary>
protected virtual bool CanBeParent(TreeViewItem item)
{
    return true;
}
CanChangeExpandedState
/// <summary>
/// 要素が expanded もしくは collapsed できるかどうか
/// </summary>
protected virtual bool CanChangeExpandedState(TreeViewItem item)
{
    return !this.m_TreeView.isSearching && item.hasChildren;
}
DoesItemMatchSearch
/// <summary>
/// TreeView.searchString に値を入れて検索した際の検索処理
/// </summary>
protected virtual bool DoesItemMatchSearch(TreeViewItem item, string search)
{
    return item.displayName.IndexOf(search, StringComparison.OrdinalIgnoreCase) >= 0;
}
RowGUI
/// <summary>
/// 1行の表示
/// Multi Header を利用した際には処理を書く必要がある
/// </summary>
protected virtual void RowGUI(TreeView.RowGUIArgs args)
{
    this.m_GUI.DefaultRowGUI(args);
}
BeforeRowsGUI
/// <summary>
/// RowGUI の前に呼び出される
/// </summary>
protected virtual void BeforeRowsGUI()
{
    if (!this.showAlternatingRowBackgrounds)
        return;
    this.m_GUI.DrawAlternatingRowBackgrounds();
}
AfterRowsGUI
/// <summary>
/// RowGUI の後に呼び出される
/// </summary>
protected virtual void AfterRowsGUI()
{
}
RefreshCustomRowHeights
/// <summary>
/// GetCustomRowHeight が override されている場合に RowのRectを更新する
/// </summary>
protected virtual void RefreshCustomRowHeights()
{
    if (!this.m_OverriddenMethods.hasGetCustomRowHeight)
        throw new InvalidOperationException("Only call RefreshCustomRowHeights if you have overridden GetCustomRowHeight to customize the height of each row.");
    this.m_GUI.RefreshRowRects(this.GetRows());
}
GetCustomRowHeight
/// <summary>
/// 各行の高さを変更できる
/// </summary>
protected virtual float GetCustomRowHeight(int row, TreeViewItem item)
{
    return this.rowHeight;
}
GetRenameRect
/// <summary>
/// Rename 時の Rect を変更できる
/// </summary>
protected virtual Rect GetRenameRect(Rect rowRect, int row, TreeViewItem item)
{
    return this.m_GUI.DefaultRenameRect(rowRect, row, item);
}
CommandEventHandling
/// <summary>
/// Command Eventの処理
/// </summary>
protected virtual void CommandEventHandling()
{
    Event current = Event.current;
    if (current.type != UnityEngine.EventType.ExecuteCommand && current.type != UnityEngine.EventType.ValidateCommand)
        return;
    bool flag = current.type == UnityEngine.EventType.ExecuteCommand;
    if (this.HasFocus() && current.commandName == "SelectAll")
    {
        if (flag)
            this.SelectAllRows();
        current.Use();
        GUIUtility.ExitGUI();
    }
    if (!(current.commandName == "FrameSelected"))
        return;
    if (flag)
    {
        if (this.hasSearch)
            this.searchString = string.Empty;
        if (this.HasSelection())
            this.FrameItem(this.GetSelection()[0]);
    }
    current.Use();
    GUIUtility.ExitGUI();
}