private void DrawReferences(AssetReferenceData data)
        {
            // 参照が無い場合はFoldoutではなくButtonで表示する
            // If there is no reference, display with Button instead of Foldout.
            if (data.References == null)
            {
                GUILayout.BeginHorizontal();
                GUILayout.Space(16f);
                if (GUILayout.Button(data.ListItem, labelStyle))
                {
                    Selection.activeObject = data.Asset;
                    EditorGUIUtility.PingObject(Selection.activeObject);
                }
                GUILayout.EndHorizontal();
                return;
            }

            bool prevFoldout = data.IsFoldout;

            if (data.IsFoldout = EditorGUILayout.Foldout(data.IsFoldout, data.ListItem, true))
            {
                foreach (var assetData in data.References)
                {
                    // 削除されたか、そもそもアセットではない
                    // It was deleted or not an asset.
                    if (assetData.Asset == null)
                    {
                        GUILayout.BeginHorizontal();
                        GUILayout.Space(32f);
                        GUILayout.Label(string.Format("<i>(Missing)</i>{0}", assetData.Path), labelStyle);
                        GUILayout.EndHorizontal();
                        continue;
                    }
                    DrawAssetData(assetData);
                }
            }

            // アセットをクリックした時にProjectビューで選択状態にする
            // Select asset in project view when clicking asset.
            if (prevFoldout != data.IsFoldout)
            {
                Selection.activeObject = data.Asset;
                EditorGUIUtility.PingObject(Selection.activeObject);
            }
        }
Example #2
0
        private static void FindByCommand(Process p, string applicationDataPathWithoutAssets, string path,
                                          string[] eol, AssetReferenceData assetData, List <string> excludeExtentionList = null)
        {
            foreach (var line in p.StandardOutput.ReadToEnd().Split(eol, StringSplitOptions.None))
            {
                if (line == null || line.Trim() == "")
                {
                    continue;
                }

                // 出力不要な拡張子なら出力しない
                // Do not output if extensions that do not require output.
                var extension = Path.GetExtension(line);
                if (excludeExtentionList != null)
                {
                    if (excludeExtentionList.Contains(extension))
                    {
                        continue;
                    }
                }

                var projectFile = Path.Combine(Application.dataPath, line);
                // metaの中に参照を握っているケース
                if (extension == ".meta")
                {
                    var assetPath = projectFile.Replace(".meta", "").Replace(applicationDataPathWithoutAssets, "");
#if UNITY_EDITOR_WIN
                    assetPath = assetPath.Replace("\\", "/");
#endif
                    if (!string.Equals(assetPath, path, StringComparison.OrdinalIgnoreCase))
                    {
                        assetData.AddReference(assetPath);
                    }

                    continue;
                }

                assetData.AddReference(projectFile.Replace(applicationDataPathWithoutAssets, ""));
            }
        }
Example #3
0
        /// <summary>
        /// OSコマンドで検索を実行
        /// Execute search with OS command.
        /// </summary>
        public static Result FindReferencesByCommand(Result.SearchType searchType, List <string> excludeExtentionList)
        {
            CommandInfo commandInfo = searchType.Command();

            string[] eol    = { commandInfo.NewLine };
            Result   result = new Result();
            string   applicationDataPathWithoutAssets = Application.dataPath.Replace("Assets", "");

            try
            {
                // パスを取得し、Projectビュー内の順番と同じになるようにソートする
                // Get the path, Sort so that it is the same as the order in the project view.
                List <AssetPath> paths = new List <AssetPath>();
                for (int i = 0; i < Selection.assetGUIDs.Length; ++i)
                {
                    string guid      = Selection.assetGUIDs[i];
                    var    assetPath = new AssetPath
                    {
                        GUID = guid,
                        Path = AssetDatabase.GUIDToAssetPath(guid),
                    };

                    bool isDirectory = File.GetAttributes(assetPath.Path).Equals(FileAttributes.Directory);
                    if (!isDirectory)
                    {
                        paths.Add(assetPath);
                    }
                    else
                    {
                        // ディレクトリを選択した場合は中のファイルも全て対象にする
                        // When directory is selected, all the files in the target are also targeted.
                        var includeFilePaths = Directory.GetFiles(assetPath.Path, "*.*", SearchOption.AllDirectories).Where(x => !x.EndsWith(".meta"));
                        foreach (string path in includeFilePaths)
                        {
                            guid = AssetDatabase.AssetPathToGUID(path);
                            if (string.IsNullOrEmpty(guid))
                            {
                                continue;
                            }
                            assetPath = new AssetPath
                            {
                                GUID = guid,
                                Path = path
                            };
                            paths.Add(assetPath);
                        }
                    }
                }
                paths.Sort((a, b) => a.Path.CompareTo(b.Path));

                // アセット毎の参照情報の作成
                // Create reference information for each asset.
                int assetCount = paths.Count;
                for (int i = 0; i < assetCount; ++i)
                {
                    string guid      = paths[i].GUID;
                    string path      = paths[i].Path;
                    string fileName  = Path.GetFileName(path);
                    var    assetData = new AssetReferenceData(path);

                    float  progress     = i / (float)assetCount;
                    string progressText = string.Format("{0}% : {1}", (int)(progress * 100f), fileName);
                    if (EditorUtility.DisplayCancelableProgressBar(ProgressBarTitle, progressText, progress))
                    {
                        // キャンセルしたら現時点での結果を返す
                        // On canceled, return current result.
                        EditorUtility.ClearProgressBar();
                        return(result);
                    }

                    var    p         = new Process();
                    string arguments = string.Format(commandInfo.Arguments, Application.dataPath, guid);
                    arguments                         += searchType.AppendArguments(excludeExtentionList);
                    p.StartInfo.FileName               = commandInfo.Command;
                    p.StartInfo.Arguments              = arguments;
                    p.StartInfo.CreateNoWindow         = true;
                    p.StartInfo.UseShellExecute        = false;
                    p.StartInfo.RedirectStandardOutput = true;
                    p.StartInfo.WorkingDirectory       = Application.dataPath;
                    p.Start();
                    p.WaitForExit();

                    FindByCommand(p, applicationDataPathWithoutAssets, path, eol, assetData, excludeExtentionList);

                    p.Close();

                    assetData.Apply();
                    result.Assets.Add(assetData);
                }
                EditorUtility.ClearProgressBar();
                result.Type = searchType;
                return(result);
            }
            catch (System.Exception e)
            {
                UnityEngine.Debug.LogError(e);
                EditorUtility.ClearProgressBar();
            }
            return(null);
        }
        /// <summary>
        /// OSコマンドで検索を実行
        /// Execute search with OS command.
        /// </summary>
        public static Result FindReferencesByCommand(string command, List <string> excludeExtentionList = null)
        {
            Result result = new Result();
            string applicationDataPathWithoutAssets = Application.dataPath.Replace("Assets", "");

            try
            {
                // パスを取得し、Projectビュー内の順番と同じになるようにソートする
                // Get the path, Sort so that it is the same as the order in the project view.
                List <AssetPath> paths = new List <AssetPath>();
                for (int i = 0; i < Selection.assetGUIDs.Length; ++i)
                {
                    string guid      = Selection.assetGUIDs[i];
                    var    assetPath = new AssetPath
                    {
                        GUID = guid,
                        Path = AssetDatabase.GUIDToAssetPath(guid),
                    };

                    bool isDirectory = File.GetAttributes(assetPath.Path).Equals(FileAttributes.Directory);
                    if (!isDirectory)
                    {
                        paths.Add(assetPath);
                    }
                    else
                    {
                        // ディレクトリを選択した場合は中のファイルも全て対象にする
                        // When directory is selected, all the files in the target are also targeted.
                        var includeFilePaths = Directory.GetFiles(assetPath.Path, "*.*", SearchOption.AllDirectories).Where(x => !x.EndsWith(".meta"));
                        foreach (string path in includeFilePaths)
                        {
                            guid = AssetDatabase.AssetPathToGUID(path);
                            if (string.IsNullOrEmpty(guid))
                            {
                                continue;
                            }
                            assetPath = new AssetPath
                            {
                                GUID = guid,
                                Path = path
                            };
                            paths.Add(assetPath);
                        }
                    }
                }
                paths.Sort((a, b) => a.Path.CompareTo(b.Path));

                // アセット毎の参照情報の作成
                // Create reference information for each asset.
                int assetCount = paths.Count;
                for (int i = 0; i < assetCount; ++i)
                {
                    string guid      = paths[i].GUID;
                    string path      = paths[i].Path;
                    string fileName  = Path.GetFileName(path);
                    var    assetData = new AssetReferenceData(path);

                    float  progress     = i / (float)assetCount;
                    string progressText = string.Format("{0}% : {1}", (int)(progress * 100f), fileName);
                    if (EditorUtility.DisplayCancelableProgressBar(ProgressBarTitle, progressText, progress))
                    {
                        // キャンセルしたら現時点での結果を返す
                        // On canceled, return current result.
                        EditorUtility.ClearProgressBar();
                        return(result);
                    }

#if UNITY_EDITOR_OSX
                    // bash実行
                    // Execution by bash.
                    var    p   = new Process();
                    string cmd = string.Format(command, Application.dataPath, guid);
                    p.StartInfo.FileName               = "/bin/bash";
                    p.StartInfo.Arguments              = "-c \" " + cmd + " \"";
                    p.StartInfo.UseShellExecute        = false;
                    p.StartInfo.RedirectStandardOutput = true;
                    p.Start();
                    p.WaitForExit();
                    foreach (var line in p.StandardOutput.ReadToEnd().Split(new[] { Environment.NewLine }, StringSplitOptions.None))
                    {
                        if (string.IsNullOrWhiteSpace(line))
                        {
                            continue;
                        }

                        // 出力不要な拡張子なら出力しない
                        // Do not output if extensions that do not require output.
                        var extension = Path.GetExtension(line);
                        if (excludeExtentionList != null)
                        {
                            if (excludeExtentionList.Contains(extension))
                            {
                                continue;
                            }
                        }

                        // metaの中に参照を握っているケース
                        if (extension == ".meta")
                        {
                            var assetPath = line.Replace(".meta", "").Replace(applicationDataPathWithoutAssets, "");
                            if (!string.Equals(assetPath, path, StringComparison.OrdinalIgnoreCase))
                            {
                                assetData.AddReference(assetPath);
                            }
                            continue;
                        }

                        assetData.AddReference(line.Replace(applicationDataPathWithoutAssets, ""));
                    }
                    p.Close();
#elif UNITY_EDITOR_WIN
                    // FindStr実行
                    // Execution "findstr" command by windowsOS.
                    var    p   = new Process();
                    string cmd = string.Format(command, Application.dataPath, guid);
                    p.StartInfo.FileName               = "findstr.exe";
                    p.StartInfo.Arguments              = cmd;
                    p.StartInfo.CreateNoWindow         = true;
                    p.StartInfo.UseShellExecute        = false;
                    p.StartInfo.RedirectStandardOutput = true;
                    p.Start();

                    List <string> assetPathList = new List <string>();
                    while (p.StandardOutput.Peek() >= 0)
                    {
                        string line = p.StandardOutput.ReadLine();
                        UnityEngine.Debug.Log(line);

                        // 「ドライブ:ファイルパス:行数:行内容」 の形式で出力されるので、
                        // ドライブからファイルパスの部分だけ取り出す
                        // Output format is 「DiscDrive:FilePath:LineNumber:LineContent」
                        // Pick out 「DiscDrive:FilePath」
                        int driveIndex = line.IndexOf(':');
                        if (driveIndex < 0)
                        {
                            continue;
                        }
                        int pathEndIndex = line.IndexOf(':', driveIndex + 1);
                        if (pathEndIndex < 0)
                        {
                            continue;
                        }
                        string formatedPath = line.Substring(0, pathEndIndex);

                        // 出力不要な拡張子なら出力しない
                        // Do not output if extensions that do not require output.
                        if (excludeExtentionList != null)
                        {
                            var extension = Path.GetExtension(formatedPath);
                            if (excludeExtentionList.Contains(extension))
                            {
                                continue;
                            }
                        }

                        // 重複排除
                        // Deduplication.
                        string assetPath = formatedPath.Replace(applicationDataPathWithoutAssets, "");
                        if (assetPathList.Contains(assetPath))
                        {
                            continue;
                        }
                        assetPathList.Add(assetPath);

                        assetData.AddReference(assetPath);
                    }
                    p.WaitForExit();
                    p.Close();
#endif

                    assetData.Apply();
                    result.Assets.Add(assetData);
                }
                EditorUtility.ClearProgressBar();
                return(result);
            }
            catch (System.Exception e)
            {
                UnityEngine.Debug.LogError(e);
                EditorUtility.ClearProgressBar();
            }
            return(null);
        }