Created
April 24, 2018 15:38
-
-
Save abeldantas/f551213daa5cbd053c7c0ceb590434c5 to your computer and use it in GitHub Desktop.
Find Monobehaviour In Scene
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections.Generic; | |
using System.Linq; | |
using UnityEditor; | |
using UnityEngine.SceneManagement; | |
using UnityEngine.UI; | |
public class FindMonobehaviourInScene | |
{ | |
[MenuItem( "Editor Utils/Select Script in Scene #&s" )] public static void SelectScriptInScene() | |
{ | |
try | |
{ | |
var script = (MonoScript)Selection.GetFiltered( typeof(MonoScript), SelectionMode.Assets ).First(); | |
var sceneObjs = SceneManager.GetActiveScene().GetRootGameObjects(); | |
foreach ( var sceneObj in sceneObjs ) | |
{ | |
if ( sceneObj.GetComponent( script.GetClass() ) ) | |
{ | |
Selection.activeGameObject = sceneObj; | |
return; | |
} | |
if ( !sceneObj.GetComponentInChildren( script.GetClass() ) ) | |
{ | |
continue; | |
} | |
Selection.activeGameObject = sceneObj.GetComponentInChildren( script.GetClass() ).gameObject; | |
return; | |
} | |
Debug.Log( script.GetClass() + " not found in this scene" ); | |
} | |
catch | |
{ | |
Debug.Log( "Can't find script, is it a monobehaviour?" ); | |
} | |
} | |
[MenuItem( "Editor Utils/Select Text in Scene" )] public static void SelectTextComponentWithString() | |
{ | |
// TODO: Make this dynamic | |
const string find = "String you want to find"; // Hardcoded string to find | |
var sceneObjs = SceneManager.GetActiveScene().GetRootGameObjects(); | |
var textsFound = new List<GameObject>(); | |
foreach ( var sceneObj in sceneObjs ) | |
{ | |
var transforms = sceneObj.GetComponentsInChildren<Transform>( true ); | |
foreach ( var t in transforms ) | |
{ | |
var texts = t.GetComponentsInChildren<Text>(); | |
foreach ( var text in texts ) | |
{ | |
if ( text.text.Contains( find ) ) | |
{ | |
Selection.activeGameObject = text.gameObject; | |
textsFound.Add( text.gameObject ); | |
} | |
} | |
} | |
} | |
if ( textsFound.Count > 0 ) | |
{ | |
textsFound = textsFound.Distinct().ToList(); | |
Debug.Log( string.Format( "Found {0} texts in this scene", textsFound.Count ) ); | |
foreach ( var textFound in textsFound ) | |
{ | |
var text = textFound.GetComponent<Text>(); | |
Debug.Log( text.text, text.gameObject ); | |
} | |
} | |
else | |
{ | |
Debug.Log( "No texts found in this scene" ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment