Без опису
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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