블로그 이미지
Every unexpected event is a path to learning for you.

카테고리

분류 전체보기 (2737)
Unity3D (817)
Programming (474)
Server (33)
Unreal (4)
Gamebryo (56)
Tip & Tech (228)
협업 (58)
3DS Max (3)
Game (12)
Utility (136)
Etc (96)
Link (32)
Portfolio (19)
Subject (90)
iOS,OSX (53)
Android (14)
Linux (5)
잉여 프로젝트 (2)
게임이야기 (3)
Memories (20)
Interest (38)
Thinking (38)
한글 (30)
PaperCraft (5)
Animation (408)
Wallpaper (2)
재테크 (18)
Exercise (3)
나만의 맛집 (3)
냥이 (10)
육아 (16)
Total
Today
Yesterday
04-25 09:56

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");

    }

}









  1. List<Texture> allTexture = new List<Texture>();
  2. Shader shader = obj.renderer.sharedMaterial.shader;
  3. for(int i=0; i<ShaderUtil.GetPropertyCount(shader); i++) {
  4. if(ShaderUtil.GetPropertyType(shader, i) == ShaderUtil.ShaderPropertyType.TexEnv) {
  5. Texture texture = obj.renderer.sharedMaterial.GetTexture(ShaderUtil.GetPropertyName(shader, i));
  6. allTexture.Add(texture);
  7. }
  8. }


반응형
Posted by blueasa
, |