using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
public class FindReferencesToThis : ScriptableObject
{
[MenuItem("CONTEXT/Component/Find references to this")]
private static void FindReferences(MenuCommand data)
{
Object context = data.context;
if (context)
{
var comp = context as Component;
if (comp)
FindReferencesTo(comp);
}
}
[MenuItem("Assets/Find references to this")]
private static void FindReferencesToAsset(MenuCommand data)
{
var selected = Selection.activeObject;
if (selected)
FindReferencesTo(selected);
}
private static void FindReferencesTo(Object to)
{
var referencedBy = new List<Object>();
var allObjects = Object.FindObjectsOfType<GameObject>();
for (int j = 0; j < allObjects.Length; j++)
{
var go = allObjects[j];
if (PrefabUtility.GetPrefabType(go) == PrefabType.PrefabInstance)
{
if (PrefabUtility.GetPrefabParent(go) == to)
{
Debug.Log(string.Format("referenced by {0}, {1}", go.name, go.GetType()), go);
referencedBy.Add(go);
}
}
var components = go.GetComponents<Component>();
for (int i = 0; i < components.Length; i++)
{
var c = components[i];
if (!c) continue;
var so = new SerializedObject(c);
var sp = so.GetIterator();
while (sp.NextVisible(true))
if (sp.propertyType == SerializedPropertyType.ObjectReference)
{
if (sp.objectReferenceValue == to)
{
Debug.Log(string.Format("referenced by {0}, {1}", c.name, c.GetType()), c);
referencedBy.Add(c.gameObject);
}
// 선택된 오브젝트가 텍스쳐일 경우 매트리얼에서 MainTexture 이름 비교
// 쉐이더에 링크된 텍스쳐가 여러개일 경우 ShaderUtil로 프로퍼티를 다 받아와서 비교해야 될 듯..
System.Type cToType = to.GetType();
if (true == cToType.Name.Equals("Texture2D"))
{
if (null != sp.objectReferenceValue)
{
System.Type cSpType = sp.objectReferenceValue.GetType();
if (true == cSpType.Name.Equals("Material"))
{
Material mat = sp.objectReferenceValue as Material;
if(null != mat.mainTexture && true == to.name .Equals(mat.mainTexture.name))
{
Debug.Log("[c.gameObject ] " + c.gameObject);
referencedBy.Add(c.gameObject);
}
}
}
}
}
}
}
if (referencedBy.Any())
Selection.objects = referencedBy.ToArray();
else Debug.Log("no references in scene");
}
}
List<Texture> allTexture = new List<Texture>(); Shader shader = obj.renderer.sharedMaterial.shader; for(int i=0; i<ShaderUtil.GetPropertyCount(shader); i++) { if(ShaderUtil.GetPropertyType(shader, i) == ShaderUtil.ShaderPropertyType.TexEnv) { Texture texture = obj.renderer.sharedMaterial.GetTexture(ShaderUtil.GetPropertyName(shader, i)); allTexture.Add(texture); } }