FindObjects

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Removed Javascript version because it's a waste of space. Updated C# version to use FindObjectsOfTypeAll. Updated shortcut.)
Line 10: Line 10:
  
 
==Usage==
 
==Usage==
Place this script in ''YourProject/Assets/Editor'' and the menu items will appear in the GameObject menu once it is compiled. Please note that you have to push return after entering a search keyword, otherwise it will not be registered when you push Find.
+
Place this script in ''YourProject/Assets/Editor'' and the menu items will appear in the GameObject menu once it is compiled.
 
+
==JavaScript - FindObjects.js==
+
<javascript>//search types
+
enum SearchType {
+
Name,
+
Component
+
}
+
 
+
class FindObjects extends ScriptableWizard {
+
//query parameters
+
var searchFor : String = "";
+
var searchBy : SearchType = SearchType.Name;
+
var caseSensitive : boolean = false;
+
var wholeWord : boolean = false;
+
var inSelectionOnly : boolean = false;
+
//stored parameters and results for Find Next
+
static var lastSearch : String = "";
+
static var lastSearchType : SearchType = SearchType.Name;
+
static var lastSearchWhole : boolean = false;
+
static var foundItems : ArrayList;
+
static var foundIndex : int = -1;
+
+
//Menu item starts wizard
+
@MenuItem ("GameObject/Find... %f")
+
+
static function FindMenuItem() {
+
ScriptableWizard.DisplayWizard("Find", FindObjects, "Find", "");
+
}
+
//Main function
+
function OnWizardCreate() {
+
//set static records
+
lastSearch = searchFor;
+
lastSearchType = searchBy;
+
lastSearchWhole = wholeWord;
+
//search space and results
+
var allObjects : Object[];
+
foundItems = new ArrayList();
+
+
if (inSelectionOnly)
+
allObjects = Selection.objects;
+
else
+
allObjects = FindObjectsOfType(GameObject);
+
+
if (searchBy == SearchType.Name) {//name comparison
+
if (wholeWord) {
+
if (caseSensitive) {
+
for (var anObject : GameObject in allObjects) {
+
if (anObject.name.ToLower().Equals(lastSearch.ToLower())) {
+
foundItems.Add(anObject);
+
}
+
}
+
} else {
+
for (var anInsensitiveObject : GameObject in allObjects) {
+
if (anInsensitiveObject.name.Equals(lastSearch)) {
+
foundItems.Add(anInsensitiveObject);
+
}
+
}
+
}
+
if (foundItems.Count == 0) {
+
Debug.Log("No active objects were found with the name \"" + lastSearch + "\"");
+
foundIndex = -1;
+
} else {
+
foundIndex = 0;
+
SelectObject(0);
+
AnnounceResult();
+
}
+
} else {
+
if (caseSensitive) {
+
for (var anObject : GameObject in allObjects) {
+
if (anObject.name.IndexOf(lastSearch) > -1) {
+
foundItems.Add(anObject);
+
}
+
}
+
} else {
+
for (var anInsensitiveObject : GameObject in allObjects) {
+
if (anInsensitiveObject.name.ToLower().IndexOf(lastSearch.ToLower()) > -1) {
+
foundItems.Add(anInsensitiveObject);
+
}
+
}
+
}
+
if (foundItems.Count == 0) {
+
Debug.Log("No active objects were found with names containing \"" + lastSearch + "\"");
+
foundIndex = -1;
+
} else {
+
foundIndex = 0;
+
SelectObject(0);
+
AnnounceResult();
+
}
+
}
+
} else { //component comparison
+
for (var objectByType : GameObject in allObjects) {
+
if (objectByType.GetComponent(lastSearch)) {
+
foundItems.Add(objectByType);
+
}
+
}
+
if (foundItems.Count == 0) {
+
Debug.Log("No active objects were found with attached component \"" + lastSearch + "\"");
+
foundIndex = -1;
+
} else {
+
foundIndex = 0;
+
SelectObject(0);
+
AnnounceResult();
+
}
+
+
}
+
}
+
function OnWizardUpdate() {
+
//Make sure there is a search string
+
if (searchFor.Equals("")) {
+
errorString = "Enter a search and push enter";
+
isValid = false;
+
} else {
+
errorString = "";
+
isValid = true;
+
}
+
//make it obvious that you need an exact match for a Component search
+
if (searchBy == SearchType.Name) {
+
helpString = "";
+
} else {
+
if (!caseSensitive || !wholeWord) {
+
caseSensitive = wholeWord = true;
+
}
+
helpString = "Component searches always require an exact match";
+
}
+
}
+
//Next Result menu item
+
@MenuItem ("GameObject/Next Result %g")
+
 
+
static function NextResultMenuItem() {
+
if (++foundIndex >= foundItems.Count)
+
foundIndex = 0;
+
SelectObject(foundIndex);
+
AnnounceResult();
+
}
+
//Next is only available if there was a previous successful search
+
@MenuItem ("GameObject/Next Result %g", true)
+
 
+
static function ValidateNextResult() : boolean {
+
return (foundIndex > -1);
+
}
+
//Previous Result menu item
+
@MenuItem ("GameObject/Previous Result #%g")
+
 
+
static function PreviousResultMenuItem() {
+
if (--foundIndex < 0)
+
foundIndex = foundItems.Count-1;
+
SelectObject(foundIndex);
+
AnnounceResult();
+
}
+
//Find Next is only available if there was a previous successful search
+
@MenuItem ("GameObject/Previous Result #%g", true)
+
 
+
static function ValidatePreviousResult() : boolean {
+
return (foundIndex > -1);
+
}
+
//tool for setting the selection by index in search results
+
static function SelectObject(newSelection : int) {
+
var newSelectionArray : Array = new Array();
+
newSelectionArray.Push(foundItems[newSelection]);
+
Selection.objects = newSelectionArray;
+
}
+
//Identifies the current search result
+
static function AnnounceResult() {
+
if (lastSearchType == SearchType.Component)
+
Debug.Log("Object " + (foundIndex+1) + " of " + foundItems.Count + " with attached component \"" + lastSearch + "\"");
+
else if (lastSearchWhole)
+
Debug.Log("Object " + (foundIndex+1) + " of " + foundItems.Count + " with the name \"" + lastSearch + "\"");
+
else
+
Debug.Log("Object " + (foundIndex+1) + " of " + foundItems.Count + " with name containing \"" + lastSearch + "\"");
+
}
+
} </javascript>
+
  
 
==C# - FindObjects.cs==
 
==C# - FindObjects.cs==
Line 208: Line 37:
 
 
 
//Menu item starts wizard
 
//Menu item starts wizard
[MenuItem ("GameObject/Find... %f")]
+
[MenuItem ("GameObject/Find... %&f")]
 
   
 
   
 
static void FindMenuItem() {
 
static void FindMenuItem() {
Line 226: Line 55:
 
allObjects = Selection.objects;
 
allObjects = Selection.objects;
 
else
 
else
allObjects = FindObjectsOfType(typeof(GameObject));
+
allObjects = FindObjectsOfTypeAll(typeof(GameObject));
 
 
 
  if (searchBy == SearchType.Name) {//name comparison
 
  if (searchBy == SearchType.Name) {//name comparison
 
if (wholeWord) {
 
if (wholeWord) {
 
if (caseSensitive) {
 
if (caseSensitive) {
foreach (GameObject anObject in allObjects) {
+
foreach (GameObject anObject in allObjects)
if (anObject.name.ToLower().Equals(lastSearch.ToLower())) {
+
if (anObject.name.ToLower().Equals(lastSearch.ToLower()))
 
foundItems.Add(anObject);
 
foundItems.Add(anObject);
}
 
}
 
 
} else {
 
} else {
foreach (GameObject anInsensitiveObject in allObjects) {
+
foreach (GameObject anObject in allObjects)
if (anInsensitiveObject.name.Equals(lastSearch)) {
+
if (anObject.name.Equals(lastSearch))
foundItems.Add(anInsensitiveObject);
+
foundItems.Add(anObject);
}
+
}
+
 
}
 
}
 
if (foundItems.Count == 0) {
 
if (foundItems.Count == 0) {
Line 253: Line 78:
 
} else {
 
} else {
 
if (caseSensitive) {
 
if (caseSensitive) {
foreach (GameObject anObject in allObjects) {
+
foreach (GameObject anObject in allObjects)
if (anObject.name.IndexOf(lastSearch) > -1) {
+
if (anObject.name.IndexOf(lastSearch) > -1)
 
foundItems.Add(anObject);
 
foundItems.Add(anObject);
}
 
}
 
 
} else {
 
} else {
foreach (GameObject anInsensitiveObject in allObjects) {
+
foreach (GameObject anObject in allObjects)
if (anInsensitiveObject.name.ToLower().IndexOf(lastSearch.ToLower()) > -1) {
+
if (anObject.name.ToLower().IndexOf(lastSearch.ToLower()) > -1)
foundItems.Add(anInsensitiveObject);
+
foundItems.Add(anObject);
}
+
}
+
 
}
 
}
 
if (foundItems.Count == 0) {
 
if (foundItems.Count == 0) {
Line 275: Line 96:
 
}
 
}
 
} else { //component comparison
 
} else { //component comparison
foreach (GameObject objectByType in allObjects) {
+
foreach (GameObject objectByType in allObjects)
if (objectByType.GetComponent(lastSearch)) {
+
if (objectByType.GetComponent(lastSearch))
 
foundItems.Add(objectByType);
 
foundItems.Add(objectByType);
}
 
}
 
 
if (foundItems.Count == 0) {
 
if (foundItems.Count == 0) {
Debug.Log("No active objects were found with attached component \"" + lastSearch + "\"");
+
Debug.Log( "No active objects were found with attached " +
 +
"component \"" + lastSearch + "\"");
 
foundIndex = -1;
 
foundIndex = -1;
 
} else {
 
} else {
Line 312: Line 132:
 
//Next Result menu item
 
//Next Result menu item
 
[MenuItem ("GameObject/Next Result %g")]
 
[MenuItem ("GameObject/Next Result %g")]
 
+
 
static void NextResultMenuItem() {
 
static void NextResultMenuItem() {
 
if (++foundIndex >= foundItems.Count)
 
if (++foundIndex >= foundItems.Count)
Line 321: Line 141:
 
//Next is only available if there was a previous successful search
 
//Next is only available if there was a previous successful search
 
[MenuItem ("GameObject/Next Result %g", true)]
 
[MenuItem ("GameObject/Next Result %g", true)]
 
+
 
static bool ValidateNextResult() {
 
static bool ValidateNextResult() {
 
return (foundIndex > -1);
 
return (foundIndex > -1);
Line 327: Line 147:
 
//Previous Result menu item
 
//Previous Result menu item
 
[MenuItem ("GameObject/Previous Result #%g")]
 
[MenuItem ("GameObject/Previous Result #%g")]
 
+
 
static void PreviousResultMenuItem() {
 
static void PreviousResultMenuItem() {
 
if (--foundIndex < 0)
 
if (--foundIndex < 0)
Line 336: Line 156:
 
//Find Next is only available if there was a previous successful search
 
//Find Next is only available if there was a previous successful search
 
[MenuItem ("GameObject/Previous Result #%g", true)]
 
[MenuItem ("GameObject/Previous Result #%g", true)]
 
+
 
static bool ValidatePreviousResult() {
 
static bool ValidatePreviousResult() {
 
return (foundIndex > -1);
 
return (foundIndex > -1);
Line 349: Line 169:
 
static void AnnounceResult() {
 
static void AnnounceResult() {
 
if (lastSearchType == SearchType.Component)
 
if (lastSearchType == SearchType.Component)
Debug.Log("Object " + (foundIndex+1) + " of " + foundItems.Count + " with attached component \"" + lastSearch + "\"");
+
Debug.Log( "Object " + (foundIndex+1) + " of " + foundItems.Count +
 +
" with attached component \"" + lastSearch + "\"");
 
else if (lastSearchWhole)
 
else if (lastSearchWhole)
Debug.Log("Object " + (foundIndex+1) + " of " + foundItems.Count + " with the name \"" + lastSearch + "\"");
+
Debug.Log( "Object " + (foundIndex+1) + " of " + foundItems.Count +
 +
" with the name \"" + lastSearch + "\"");
 
else
 
else
Debug.Log("Object " + (foundIndex+1) + " of " + foundItems.Count + " with name containing \"" + lastSearch + "\"");
+
Debug.Log( "Object " + (foundIndex+1) + " of " + foundItems.Count +
 +
" with name containing \"" + lastSearch + "\"");
 
}
 
}
 
}</csharp>
 
}</csharp>

Revision as of 15:17, 14 September 2009

Author: Daniel Brauer
Thanks to NCarter for help along the way!

Description

This editor script adds a Find function to the GameObject menu. It allows you to search active objects by name or by attached component type. You can search for partial names with or without case sensitivity. Both name and type searches can be restricted to the current selection. Search results can be traversed with Next/Previous Result. Information about search results is given via the console.

Usage

Place this script in YourProject/Assets/Editor and the menu items will appear in the GameObject menu once it is compiled.

C# - FindObjects.cs

<csharp>using UnityEngine; using UnityEditor; using System.Collections;

public class FindObjects : ScriptableWizard { //search types public enum SearchType { Name, Component } //query parameters public string searchFor = ""; public SearchType searchBy = SearchType.Name; public bool caseSensitive = false; public bool wholeWord = false; public bool inSelectionOnly = false; //stored parameters and results for Find Next static string lastSearch = ""; static SearchType lastSearchType = SearchType.Name; static bool lastSearchWhole = false; static ArrayList foundItems; static int foundIndex = -1;

//Menu item starts wizard [MenuItem ("GameObject/Find... %&f")]

static void FindMenuItem() { ScriptableWizard.DisplayWizard("Find", typeof(FindObjects), "Find", ""); } //Main function void OnWizardCreate() { //set static records lastSearch = searchFor; lastSearchType = searchBy; lastSearchWhole = wholeWord; //search space and results Object[] allObjects; foundItems = new ArrayList();

if (inSelectionOnly) allObjects = Selection.objects; else allObjects = FindObjectsOfTypeAll(typeof(GameObject));

		if (searchBy == SearchType.Name) {//name comparison

if (wholeWord) { if (caseSensitive) { foreach (GameObject anObject in allObjects) if (anObject.name.ToLower().Equals(lastSearch.ToLower())) foundItems.Add(anObject); } else { foreach (GameObject anObject in allObjects) if (anObject.name.Equals(lastSearch)) foundItems.Add(anObject); } if (foundItems.Count == 0) { Debug.Log("No active objects were found with the name \"" + lastSearch + "\""); foundIndex = -1; } else { foundIndex = 0; SelectObject(0); AnnounceResult(); } } else { if (caseSensitive) { foreach (GameObject anObject in allObjects) if (anObject.name.IndexOf(lastSearch) > -1) foundItems.Add(anObject); } else { foreach (GameObject anObject in allObjects) if (anObject.name.ToLower().IndexOf(lastSearch.ToLower()) > -1) foundItems.Add(anObject); } if (foundItems.Count == 0) { Debug.Log("No active objects were found with names containing \"" + lastSearch + "\""); foundIndex = -1; } else { foundIndex = 0; SelectObject(0); AnnounceResult(); } } } else { //component comparison foreach (GameObject objectByType in allObjects) if (objectByType.GetComponent(lastSearch)) foundItems.Add(objectByType); if (foundItems.Count == 0) { Debug.Log( "No active objects were found with attached " + "component \"" + lastSearch + "\""); foundIndex = -1; } else { foundIndex = 0; SelectObject(0); AnnounceResult(); }

} } void OnWizardUpdate() { //Make sure there is a search string if (searchFor.Equals("")) { errorString = "Enter a search and push enter"; isValid = false; } else { errorString = ""; isValid = true; } //make it obvious that you need an exact match for a Component search if (searchBy == SearchType.Name) { helpString = ""; } else { if (!caseSensitive || !wholeWord) { caseSensitive = wholeWord = true; } helpString = "Component searches always require an exact match"; } } //Next Result menu item [MenuItem ("GameObject/Next Result %g")]

static void NextResultMenuItem() { if (++foundIndex >= foundItems.Count) foundIndex = 0; SelectObject(foundIndex); AnnounceResult(); } //Next is only available if there was a previous successful search [MenuItem ("GameObject/Next Result %g", true)]

static bool ValidateNextResult() { return (foundIndex > -1); } //Previous Result menu item [MenuItem ("GameObject/Previous Result #%g")]

static void PreviousResultMenuItem() { if (--foundIndex < 0) foundIndex = foundItems.Count-1; SelectObject(foundIndex); AnnounceResult(); } //Find Next is only available if there was a previous successful search [MenuItem ("GameObject/Previous Result #%g", true)]

static bool ValidatePreviousResult() { return (foundIndex > -1); } //tool for setting the selection by index in search results static void SelectObject(int newSelection) { Object[] newSelectionArray = new Object[1]; newSelectionArray[0] = foundItems[newSelection] as Object; Selection.objects = newSelectionArray; } //Identifies the current search result static void AnnounceResult() { if (lastSearchType == SearchType.Component) Debug.Log( "Object " + (foundIndex+1) + " of " + foundItems.Count + " with attached component \"" + lastSearch + "\""); else if (lastSearchWhole) Debug.Log( "Object " + (foundIndex+1) + " of " + foundItems.Count + " with the name \"" + lastSearch + "\""); else Debug.Log( "Object " + (foundIndex+1) + " of " + foundItems.Count + " with name containing \"" + lastSearch + "\""); } }</csharp>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox