浏览代码

Find And Replace continued

/main
Thomas ICHÉ 6 年前
当前提交
a39cae53
共有 1 个文件被更改,包括 170 次插入12 次删除
  1. 182
      Editor/FindAndReplace/FindAndReplaceWindow.cs

182
Editor/FindAndReplace/FindAndReplaceWindow.cs


using System.Collections;
using System.Reflection;
using System;
using System.Linq;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

GetWindow<FindAndReplaceWindow>();
}
static readonly Dictionary<string, Type> s_assemblyTypes = GetTypes();
private static Dictionary<string,Type> GetTypes()
{
Dictionary<string, Type> all = new Dictionary<string, Type>();
foreach(var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
foreach(Type t in assembly.GetTypes())
{
if(typeof(Component).IsAssignableFrom(t) && !all.ContainsKey(t.Name))
all.Add(t.Name, t);
}
}
return all;
}
public enum SearchBy
{
Name,

[SerializeField]
Material materialSearch;
enum SearchOp
{
Find,
Add,
Refine
}
void SearchControlsGUI()
{

{
case SearchBy.Name:
nameSearch = EditorGUILayout.TextField(Contents.nameSearch, nameSearch);
SearchButtonsGUI(searchBy, nameSearch);
SearchButtonsGUI(searchBy, tagSearch);
SearchButtonsGUI(searchBy, layerSearch);
SearchButtonsGUI(searchBy, componentSearch);
SearchButtonsGUI(searchBy, meshSearch);
SearchButtonsGUI(searchBy, materialSearch);
using (new GUILayout.HorizontalScope())
{
GUILayout.Button("Find",GUILayout.Height(32));
GUILayout.Button("Refine",GUILayout.Height(32));
}
GUILayout.FlexibleSpace();
GUILayout.Label("Replace By", EditorStyles.boldLabel);

}
void SearchButtonsGUI(SearchBy by, object criteria)
{
using (new GUILayout.HorizontalScope())
{
if (GUILayout.Button("Find", GUILayout.Height(32)))
Search(SearchOp.Find, by, criteria);
if (GUILayout.Button("Add", GUILayout.Height(32)))
Search(SearchOp.Add, by, criteria);
if (GUILayout.Button("Refine", GUILayout.Height(32)))
Search(SearchOp.Refine, by, criteria);
}
}
void Search(SearchOp op, SearchBy by, object criteria)
{
List<GameObject> query = new List<GameObject>();
switch(by)
{
case SearchBy.Name:
foreach(var go in FindObjectsOfType<GameObject>())
{
if (go.name.Contains((string)criteria))
query.Add(go);
}
break;
case SearchBy.Tag:
query.AddRange(GameObject.FindGameObjectsWithTag((string)criteria));
break;
case SearchBy.Layer:
foreach (var go in FindObjectsOfType<GameObject>())
{
if (go.layer == LayerMask.NameToLayer((string)criteria))
query.Add(go);
}
break;
case SearchBy.ComponentType:
if(s_assemblyTypes.ContainsKey((string)criteria))
{
Type t = s_assemblyTypes[(string)criteria];
if( typeof(Component).IsAssignableFrom(t))
{
Component[] components = (Component[])FindObjectsOfType(t);
if(components != null)
{
foreach(var c in components)
{
if (!query.Contains(c.gameObject))
query.Add(c.gameObject);
}
}
}
}
break;
case SearchBy.Mesh:
Mesh mesh = (Mesh)criteria;
foreach (var go in FindObjectsOfType<GameObject>())
{
MeshFilter filter = go.GetComponent<MeshFilter>();
if (filter != null && filter.sharedMesh == mesh)
{
query.Add(go);
}
}
break;
case SearchBy.Material:
Material mat = (Material)criteria;
foreach (var go in FindObjectsOfType<GameObject>())
{
Renderer renderer = go.GetComponent<Renderer>();
if (renderer != null)
{
if(renderer.sharedMaterials.Contains(mat))
{
query.Add(go);
}
}
}
break;
}
switch (op)
{
case SearchOp.Find:
searchResults = query;
break;
case SearchOp.Add:
foreach(var item in query)
{
if (!searchResults.Contains(item))
searchResults.Add(item);
}
break;
case SearchOp.Refine:
List<GameObject> refined = new List<GameObject>();
foreach (var item in searchResults)
{
if (query.Contains(item))
refined.Add(item);
}
searchResults = refined;
break;
}
}
[SerializeField]
List<GameObject> searchResults= new List<GameObject>();
Vector2 scroll;

GUILayout.Space(8);
GUILayout.Label("Search Results", EditorStyles.boldLabel);
using (new GUILayout.HorizontalScope())
{
GUILayout.Label("Search Results", EditorStyles.boldLabel);
GUILayout.FlexibleSpace();
if(GUILayout.Button("From Selection"))
{
searchResults = Selection.gameObjects.ToList();
}
if(GUILayout.Button("Select"))
{
Selection.objects = searchResults.ToArray();
}
}
using (new GUILayout.ScrollViewScope(scroll, EditorStyles.helpBox))
scroll = GUILayout.BeginScrollView(scroll, EditorStyles.helpBox);
GameObject toRemove = null;
using (new GUILayout.HorizontalScope(EditorStyles.helpBox))
using (new GUILayout.HorizontalScope(EditorStyles.textField))
GUILayout.Label(obj.name);
GUILayout.Button("X", GUILayout.Width(32));
GUILayout.Label(obj.name, EditorStyles.label);
if(GUILayout.Button("X", GUILayout.Width(32)))
{
toRemove = obj;
}
if (toRemove != null)
searchResults.Remove(toRemove);
GUILayout.EndScrollView();
}
static class Contents

正在加载...
取消
保存