SHARE
TWEET

Untitled

a guest Jan 23rd, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Diagnostics.CodeAnalysis;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using JetBrains.Annotations;
  7. using UnityEngine.UIElements;
  8.  
  9. // ReSharper disable once CheckNamespace
  10. namespace UnityEditor
  11. {
  12.     [PublicAPI]
  13.     [SuppressMessage("ReSharper", "InconsistentNaming")]
  14.     [SuppressMessage("ReSharper", "StringLiteralTypo")]
  15.     [SuppressMessage("ReSharper", "CommentTypo")]
  16.     public static class EditorWindowExtensions
  17.     {
  18.         /// <summary>
  19.         ///     Loads the visual tree.
  20.         ///     <para>
  21.         ///         This method loads the visual tree and associated style sheet using default Unity conventions,
  22.         ///         that is, UXML and USS files are expected to be located next to this instance script asset.
  23.         ///     </para>
  24.         ///     <para>
  25.         ///         Declared instance fields that derive from <see cref="VisualElement" /> will be assigned
  26.         ///         the matching element instance found when searching the visual tree.
  27.         ///     </para>
  28.         /// </summary>
  29.         /// <param name="window">
  30.         ///     Source window.
  31.         /// </param>
  32.         /// <exception cref="ArgumentNullException">
  33.         ///     <paramref name="window" /> is <c>null</c>.
  34.         /// </exception>
  35.         /// <exception cref="FileNotFoundException">
  36.         ///     Associated UMXL or USS file could not be found.
  37.         /// </exception>
  38.         /// <exception cref="InvalidOperationException">
  39.         ///     A declared instance field deriving from <see cref="VisualElement" /> could not be found in the visual tree.
  40.         /// </exception>
  41.         public static void LoadVisualTree([NotNull] this EditorWindow window)
  42.         {
  43.             if (window == null)
  44.                 throw new ArgumentNullException(nameof(window));
  45.  
  46.             var script = MonoScript.FromScriptableObject(window);
  47.             var path = AssetDatabase.GetAssetPath(script);
  48.  
  49.             var xml = Path.ChangeExtension(path, "uxml");
  50.             var css = Path.ChangeExtension(path, "uss");
  51.  
  52.             var asset = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(xml);
  53.             if (asset == null)
  54.                 throw new FileNotFoundException("Associated UXML file could not be found.");
  55.  
  56.             var sheet = AssetDatabase.LoadAssetAtPath<StyleSheet>(css);
  57.             if (sheet == null)
  58.                 throw new FileNotFoundException("Associated USS file could not be found.");
  59.  
  60.             var tree = asset.CloneTree();
  61.             tree.styleSheets.Add(sheet);
  62.  
  63.             window.rootVisualElement.Clear();
  64.             window.rootVisualElement.Add(tree);
  65.  
  66.             var fields = window
  67.                 .GetType()
  68.                 .GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly)
  69.                 .Where(s => typeof(VisualElement).IsAssignableFrom(s.FieldType))
  70.                 .ToArray();
  71.  
  72.             foreach (var field in fields)
  73.             {
  74.                 var name = field.Name;
  75.                 var type = field.FieldType;
  76.  
  77.                 var element = tree.Q(name);
  78.                 if (element == null)
  79.                     throw new InvalidOperationException($"Element '{name}' of type '{type}' could not be found in visual tree.");
  80.  
  81.                 field.SetValue(window, element);
  82.             }
  83.         }
  84.     }
  85. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top