using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System;
public class ScriptFinder : EditorWindow
{
static ScriptFinder window;
static string scriptValue = "";
static string oldScriptValue;
static List<Type> components;
static List<string> componentNames;
static List<GameObject> sceneObjects;
static int selectedIndex = 0;
static int prevIndex = 0;
static Vector2 scrollValue = Vector2.zero;
//public
// Use this for initialization
[MenuItem("EditorUtility/Script Finder")]
static void OpenScriptFinder()
{
//EditorUtility.FocusProjectWindow();
window = (ScriptFinder)EditorWindow.GetWindow(typeof(ScriptFinder));
/*
Assembly _asm = Assembly.GetAssembly(typeof(AnimationComponent));
AssemblyName _asmName = _asm.GetName();
Debug.Log(_asmName.FullName);
*/
Assembly _assembly = Assembly.Load("Assembly-CSharp");
components = new List<Type>();
componentNames = new List<string>();
foreach (Type type in _assembly.GetTypes())
{
if (type.IsClass)
{
if (type.BaseType.FullName.Contains("MonoBehaviour"))
{
components.Add(type);
componentNames.Add(type.Name);
// Debug.Log(type.Name);
}
else
{
if (!type.BaseType.FullName.Contains("System"))
{
Type _type = type.BaseType;
components.Add(_type);
componentNames.Add(type.Name);
// Debug.Log(type.Name);
}
}
}
}
prevIndex = selectedIndex;
SelectScript(components[selectedIndex]);
}
static void SelectScript(Type _type)
{
Debug.Log("Selected Type: " + _type);
sceneObjects = new List<GameObject>();
// Debug.Log(GameObject.FindSceneObjectsOfType(typeof(GameObject)).Length);
foreach (UnityEngine.Object _obj in GameObject.FindSceneObjectsOfType(_type))
{
Debug.Log(_obj.name);
sceneObjects.Add(GameObject.Find(_obj.name));
}
Selection.objects = sceneObjects.ToArray();
}
void OnGUI()
{
selectedIndex = EditorGUILayout.Popup(selectedIndex, componentNames.ToArray());
if (selectedIndex != prevIndex)
{
SelectScript(components[selectedIndex]);
}
scrollValue = EditorGUILayout.BeginScrollView(scrollValue);
foreach (GameObject _obj in sceneObjects)
{
if (GUILayout.Button(_obj.name, GUIStyle.none))
{
Selection.activeObject = _obj;
EditorGUIUtility.PingObject(_obj);
}
}
EditorGUILayout.EndScrollView();
prevIndex = selectedIndex;
}
}
结果是黑色的,我希望它是白色的:
另一个小问题,我在这一行收到警告:
foreach (UnityEngine.Object _obj in GameObject.FindSceneObjectsOfType(_type))
右侧:
GameObject.FindSceneObjectsOfType(_type
带有绿色下划线,警告信息为:
严重性代码描述项目文件行抑制状态 警告 CS0618“Object.FindSceneObjectsOfType(Type)”已过时:“警告请改用 Object.FindObjectsByType。”
如何修复警告?我尝试改用 Object 但 Object 没有任何 FindObjectsByType 属性。
解决方案是在OnGUI方法的顶部添加这一行:
GUI.color = Color.white;
并删除 GUIStyle.none 部分。
结果是:
关于上线小警告:
foreach (UnityEngine.Object _obj in GameObject.FindSceneObjectsOfType(_type))
这里的解决方法是把右边改成:
FindObjectsOfType
线路:
foreach (UnityEngine.Object _obj in FindObjectsOfType(_type))
修改后的完整代码:
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System;
public class ScriptFinder : EditorWindow
{
static ScriptFinder window;
static List<Type> components;
static List<string> componentNames;
static List<GameObject> sceneObjects;
static int selectedIndex = 0;
static int prevIndex = 0;
static Vector2 scrollValue = Vector2.zero;
[MenuItem("EditorUtility/Script Finder")]
static void OpenScriptFinder()
{
window = (ScriptFinder)EditorWindow.GetWindow(typeof(ScriptFinder));
Assembly _assembly = Assembly.Load("Assembly-CSharp");
components = new List<Type>();
componentNames = new List<string>();
foreach (Type type in _assembly.GetTypes())
{
if (type.IsClass)
{
if (type.BaseType.FullName.Contains("MonoBehaviour"))
{
components.Add(type);
componentNames.Add(type.Name);
}
else
{
if (!type.BaseType.FullName.Contains("System"))
{
Type _type = type.BaseType;
components.Add(_type);
componentNames.Add(type.Name);
}
}
}
}
prevIndex = selectedIndex;
SelectScript(components[selectedIndex]);
}
static void SelectScript(Type _type)
{
Debug.Log("Selected Type: " + _type);
sceneObjects = new List<GameObject>();
foreach (UnityEngine.Object _obj in FindObjectsOfType(_type))
{
Debug.Log(_obj.name);
sceneObjects.Add(GameObject.Find(_obj.name));
}
Selection.objects = sceneObjects.ToArray();
}
void OnGUI()
{
GUI.color = Color.white;
selectedIndex = EditorGUILayout.Popup(selectedIndex, componentNames.ToArray());
if (selectedIndex != prevIndex)
{
SelectScript(components[selectedIndex]);
}
scrollValue = EditorGUILayout.BeginScrollView(scrollValue);
foreach (GameObject _obj in sceneObjects)
{
if (GUILayout.Button(_obj.name))//, GUIStyle.none))
{
Selection.activeObject = _obj;
EditorGUIUtility.PingObject(_obj);
}
}
EditorGUILayout.EndScrollView();
prevIndex = selectedIndex;
}
}