File size: 3,012 Bytes
18a519f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | /*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
using System;
using System.IO;
using System.Linq;
using UnityEngine;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine.SceneManagement;
namespace Microsoft.Unity.VisualStudio.Editor
{
[Serializable]
internal class FileUsage
{
public string Path;
public string[] GameObjectPath;
}
internal static class UsageUtility
{
internal static void ShowUsage(string json)
{
try
{
var usage = JsonUtility.FromJson<FileUsage>(json);
ShowUsage(usage.Path, usage.GameObjectPath);
}
catch (Exception)
{
// ignore malformed request
}
}
internal static void ShowUsage(string path, string[] gameObjectPath)
{
path = FileUtility.MakeRelativeToProjectPath(path);
if (path == null)
return;
path = FileUtility.NormalizeWindowsToUnix(path);
var extension = Path.GetExtension(path).ToLower();
EditorUtility.FocusProjectWindow();
switch (extension)
{
case ".unity":
ShowSceneUsage(path, gameObjectPath);
break;
default:
var asset = AssetDatabase.LoadMainAssetAtPath(path);
Selection.activeObject = asset;
EditorGUIUtility.PingObject(asset);
break;
}
}
private static void ShowSceneUsage(string scenePath, string[] gameObjectPath)
{
var scene = SceneManager.GetSceneByPath(scenePath.Replace(Path.DirectorySeparatorChar, '/'));
if (!scene.isLoaded)
{
var result = EditorUtility.DisplayDialogComplex("Show Usage",
$"Do you want to open \"{Path.GetFileName(scenePath)}\"?",
"Open Scene",
"Cancel",
"Open Scene (additive)");
switch (result)
{
case 0:
EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo();
scene = EditorSceneManager.OpenScene(scenePath, OpenSceneMode.Single);
break;
case 1:
return;
case 2:
scene = EditorSceneManager.OpenScene(scenePath, OpenSceneMode.Additive);
break;
}
}
ShowSceneUsage(scene, gameObjectPath);
}
private static void ShowSceneUsage(Scene scene, string[] gameObjectPath)
{
if (gameObjectPath == null || gameObjectPath.Length == 0)
return;
var go = scene.GetRootGameObjects().FirstOrDefault(g => g.name == gameObjectPath[0]);
if (go == null)
return;
for (var ni = 1; ni < gameObjectPath.Length; ni++)
{
var transform = go.transform;
for (var i = 0; i < transform.childCount; i++)
{
var child = transform.GetChild(i);
var childgo = child.gameObject;
if (childgo.name == gameObjectPath[ni])
{
go = childgo;
break;
}
}
}
Selection.activeObject = go;
EditorGUIUtility.PingObject(go);
}
}
}
|