Exemplo n.º 1
0
    internal static void SetSearchFilter(this EditorWindow window, int[] instanceIDs, string title)
    {
        if (window == null)
        {
            WindowX.xClearDefinitionCache();
            window = WindowX.Hierarchy;
        }

        if (instanceIDs.Length == 0)
        {
            window.xInvoke(
                "SetSearchFilter", null, null,
                new object[] { "Hierarchy2tempfixemptysearch", SearchableEditorWindow.SearchMode.All, false });
            window.xSetSearchFilterTerm("iso:" + title);
            return;
        }

/*#if UNITY_4_6
 *      Debug.Log("Before");
 *
 *      //var sf = typeof(SearchableEditorWindow).xInvoke("CreateFilter", null, null, "iso:" +title, SearchableEditorWindow.SearchMode.All);
 *      window.xInvoke("SetSearchFilter", null, null, "iso:" + title, SearchableEditorWindow.SearchMode.All, false);
 *      window.xSetField("m_HasSearchFilterFocus", true);
 *
 *      var treeView        = window.xGetField("m_TreeView");
 *      var ds              = treeView.xGetProperty("data");
 *
 *
 *
 *
 *
 *
 *      window.Repaint();
 *
 *      Debug.Log("After");*/

#if UNITY_4_5 || UNITY_4_6 || UNITY_5
        //var treeViewSrcT    = "UnityEditor.TreeViewDataSource".xGetTypeByName("UnityEditor");
        var treeViewItemT = "UnityEditor.TreeViewItem".xGetTypeByName("UnityEditor");
        var treeView      = WindowX.Hierarchy.xGetField("m_TreeView");
        var treeViewData  = treeView.xGetProperty("data");
        var rootItem      = treeViewData.xGetField("m_RootItem");
        var children      = rootItem.GetChildrenTreeItem(treeViewItemT, true);
        var expandIds     = treeViewData.xInvoke("GetExpandedIDs"); //save the expand state to restore

        foreach (var t in children)                                 // expand all children
        {
            if (t != null)
            {
                treeViewData.xInvoke("SetExpandedWithChildren", null, null, t, true);
            }
        }

        //Debug.Log("ids :: " + instanceIDs.Length);

        var children1    = (IList)treeViewData.xInvoke("GetVisibleRows");
        var childrenList = treeViewItemT.xNewListT();
        for (var i = 0; i < children1.Count; i++)
        {
            var child = children1[i];

            if (instanceIDs.Contains((int)child.xGetField("m_ID", treeViewItemT)))
            {
                child.xSetField("m_Depth", 0, treeViewItemT);
                childrenList.Add(child);
            }
        }

        // restore the expand state for children
        treeViewData.xInvoke("SetExpandedIDs", null, null, expandIds);

        window.xInvoke(
            "SetSearchFilter", null, null, new object[] { "iso:" + title, SearchableEditorWindow.SearchMode.All, false });
        treeViewData.xSetField("m_VisibleRows", childrenList.xToListT(treeViewItemT));
        treeView.xSetField("m_AllowRenameOnMouseUp", false);
        treeView.xInvoke("Repaint");
#else
        var TBaseProjectWindow = "UnityEditor.BaseProjectWindow".xGetTypeByName("UnityEditor");
        var TFilteredHierarchy = "UnityEditor.FilteredHierarchy".xGetTypeByName("UnityEditor");

        //window.SetSearchFilter("iso:" + title);

        var instIDsParams = new object[] { instanceIDs };
        var fh            = window.xGetField("m_FilteredHierarchy", TBaseProjectWindow);
        var sf            = (SearchFilter)fh.xGetField("m_SearchFilter", TFilteredHierarchy);

        sf.ClearSearch();
        sf.referencingInstanceIDs = instanceIDs;
        fh.xInvoke("SetResults", TFilteredHierarchy, null, instIDsParams);

        var arr  = (object[])fh.xGetProperty("results", TFilteredHierarchy, null);   //(FilteredHierarchyType.GetProperty("results").GetValue(fh, null));
        var list = new List <int>();

        //patch
        var nMissing = 0;
        foreach (var t in arr)
        {
            if (t == null)
            {
                nMissing++;
                continue;
            }
            var id = (int)t.xGetField("instanceID");
            if (!list.Contains(id))
            {
                list.Add(id);
            }
        }

        if (nMissing > 0)
        {
            Debug.LogWarning("Filtered result may not be correct, missing " + nMissing + " results, please help report it to [email protected]");
        }
        instanceIDs = list.ToArray();

        //reapply
        sf.ClearSearch();
        sf.referencingInstanceIDs = instanceIDs;
        fh.xInvoke("SetResults", TFilteredHierarchy, null, new object[] { instanceIDs });
        window.Repaint();
#endif
    }
Exemplo n.º 2
0
    internal static void SetSearchFilter(EditorWindow window, int[] instanceIDs, string title)
    {
        if (window == null)
        {
            WindowX.xClearDefinitionCache();
            window = WindowX.Hierarchy;
        }

        var goList = new List <GameObject>();

        for (var i = 0; i < instanceIDs.Length; i++)
        {
            var go = (GameObject)EditorUtility.InstanceIDToObject(instanceIDs[i]);
            if (go != null)
            {
                goList.Add(go);
            }
        }

        if (instanceIDs.Length == 0)
        {
            window.xInvoke("SetSearchFilter", null, null, new object[] { "Hierarchy2tempfixemptysearch", SearchableEditorWindow.SearchMode.All, false });
            window.xSetSearchFilterTerm("iso:" + title);
            return;
        }

        var TBaseProjectWindow = "UnityEditor.BaseProjectWindow".xGetTypeByName("UnityEditor");
        var TFilteredHierarchy = "UnityEditor.FilteredHierarchy".xGetTypeByName("UnityEditor");

        window.xSetSearchFilterTerm("iso:" + title);

        var instIDsParams = new object[] { instanceIDs };
        var fh            = window.xGetField("m_FilteredHierarchy", TBaseProjectWindow);
        var sf            = (SearchFilter)fh.xGetField("m_SearchFilter", TFilteredHierarchy);

        sf.ClearSearch();
        sf.referencingInstanceIDs = instanceIDs;
        fh.xInvoke("SetResults", TFilteredHierarchy, null, instIDsParams);

        var arr  = (object[])fh.xGetProperty("results", TFilteredHierarchy, null);   //(FilteredHierarchyType.GetProperty("results").GetValue(fh, null));
        var list = new List <int>();

        //patch
        var nMissing = 0;

        foreach (var t in arr)
        {
            if (t == null)
            {
                nMissing++;
                continue;
            }
            var id = (int)t.xGetField("instanceID");
            if (!list.Contains(id))
            {
                list.Add(id);
            }
        }

        if (nMissing > 0)
        {
            Debug.LogWarning("Filtered result may not be correct, missing " + nMissing + " results, please help report it to [email protected]");
        }
        instanceIDs = list.ToArray();

        //reapply
        sf.ClearSearch();
        sf.referencingInstanceIDs = instanceIDs;
        fh.xInvoke("SetResults", TFilteredHierarchy, null, instanceIDs);
        window.Repaint();
    }