4.Unity经验:Unity3d中查找一个脚本被挂在哪些预设上面

用一个脚本函数可以获取到选择的脚本文件被哪些预设和场景引用

using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;

public class GetReference
{
    [MenuItem("Assets/Tools/GetFileReference")]
    static void GetFileReference()
    {
        string target = "";
        if (Selection.activeObject != null)
        {
            target = AssetDatabase.GetAssetPath(Selection.activeObject);
        }
        if (string.IsNullOrEmpty(target))
        {
            return;
        }
        string[] files = Directory.GetFiles(Application.dataPath, "*.prefab", SearchOption.AllDirectories);
        string[] scene = Directory.GetFiles(Application.dataPath, "*unity", SearchOption.AllDirectories);

        List fileList = new List();
        for (int i = 0; i < files.Length; i++)
        {
            string[] source = AssetDatabase.GetDependencies(new string[] { files[i].Replace(Application.dataPath, "Assets") });
            for (int j = 0; j < source.Length; j++)
            {
                if (source[j] == target)
                {
                    fileList.Add(AssetDatabase.LoadMainAssetAtPath(files[i].Replace(Application.dataPath, "Assets")));
                }
            }
        }
        for (int i = 0; i < scene.Length; i++)
        {
            string[] source = AssetDatabase.GetDependencies(new string[] { scene[i].Replace(Application.dataPath, "Assets")});
            for (int j = 0; j < source.Length; j++)
            {
                if (source[j] == target)
                {
                    fileList.Add(AssetDatabase.LoadMainAssetAtPath(scene[i].Replace(Application.dataPath, "Assets")));
                }
            }
        }
        Selection.objects = fileList.ToArray();
    }
}
 
  

4.Unity经验:Unity3d中查找一个脚本被挂在哪些预设上面_第1张图片

 

你可能感兴趣的:(Unity经验记录)