123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- #if UNITY_2021_2_OR_NEWER
- using System;
- using System.Collections.Generic;
- using UnityEditor.Overlays;
- using UnityEngine.UIElements;
- using UnityEditor;
- using UnityEngine;
- using UnityEditor.Toolbars;
- namespace SingularityGroup.HotReload.Editor {
- [Overlay(typeof(SceneView), "Hot Reload", true)]
- [Icon("Assets/HotReload/Editor/Resources/Icon_DarkMode.png")]
- internal class HotReloadOverlay : ToolbarOverlay {
- HotReloadOverlay() : base(HotReloadToolbarIndicationButton.id, HotReloadToolbarEventsButton.id, HotReloadToolbarRecompileButton.id) {
- EditorApplication.update += Update;
- }
-
- EditorIndicationState.IndicationStatus lastIndicationStatus;
-
- [EditorToolbarElement(id, typeof(SceneView))]
- class HotReloadToolbarIndicationButton : EditorToolbarButton, IAccessContainerWindow {
- internal const string id = "HotReloadOverlay/LogoButton";
- public EditorWindow containerWindow { get; set; }
- EditorIndicationState.IndicationStatus lastIndicationStatus;
-
- internal HotReloadToolbarIndicationButton() {
- icon = GetIndicationIcon();
- tooltip = EditorIndicationState.IndicationStatusText;
- clicked += OnClick;
- EditorApplication.update += Update;
- }
- void OnClick() {
- EditorWindow.GetWindow<HotReloadWindow>().Show();
- EditorWindow.GetWindow<HotReloadWindow>().SelectTab(typeof(HotReloadRunTab));
- }
-
- void Update() {
- if (lastIndicationStatus != EditorIndicationState.CurrentIndicationStatus) {
- icon = GetIndicationIcon();
- tooltip = EditorIndicationState.IndicationStatusText;
- lastIndicationStatus = EditorIndicationState.CurrentIndicationStatus;
- }
- }
- ~HotReloadToolbarIndicationButton() {
- clicked -= OnClick;
- EditorApplication.update -= Update;
- }
- }
-
- [EditorToolbarElement(id, typeof(SceneView))]
- class HotReloadToolbarEventsButton : EditorToolbarButton, IAccessContainerWindow {
- internal const string id = "HotReloadOverlay/EventsButton";
- public EditorWindow containerWindow { get; set; }
-
- bool lastShowingRedDot;
-
- internal HotReloadToolbarEventsButton() {
- icon = HotReloadState.ShowingRedDot ? GUIHelper.GetInvertibleIcon(InvertibleIcon.EventsNew) : GUIHelper.GetInvertibleIcon(InvertibleIcon.Events);
- tooltip = "Events";
- clicked += OnClick;
- EditorApplication.update += Update;
- }
- void OnClick() {
- HotReloadEventPopup.Open(PopupSource.Overlay, Event.current.mousePosition);
- }
-
- void Update() {
- if (lastShowingRedDot != HotReloadState.ShowingRedDot) {
- icon = HotReloadState.ShowingRedDot ? GUIHelper.GetInvertibleIcon(InvertibleIcon.EventsNew) : GUIHelper.GetInvertibleIcon(InvertibleIcon.Events);
- lastShowingRedDot = HotReloadState.ShowingRedDot;
- }
- }
- ~HotReloadToolbarEventsButton() {
- clicked -= OnClick;
- EditorApplication.update -= Update;
- }
- }
-
-
- [EditorToolbarElement(id, typeof(SceneView))]
- class HotReloadToolbarRecompileButton : EditorToolbarButton, IAccessContainerWindow {
- internal const string id = "HotReloadOverlay/RecompileButton";
-
- public EditorWindow containerWindow { get; set; }
-
- private Texture2D refreshIcon => GUIHelper.GetInvertibleIcon(InvertibleIcon.Recompile);
- internal HotReloadToolbarRecompileButton() {
- icon = refreshIcon;
- tooltip = "Recompile";
- clicked += HotReloadRunTab.RecompileWithChecks;
- }
- }
- private static Texture2D latestIcon;
- private static Dictionary<string, Texture2D> iconTextures = new Dictionary<string, Texture2D>();
- private static Spinner spinner = new Spinner(100);
- private static Texture2D GetIndicationIcon() {
- if (EditorIndicationState.IndicationIconPath == null || EditorIndicationState.SpinnerActive) {
- latestIcon = spinner.GetIcon();
- } else {
- latestIcon = GUIHelper.GetLocalIcon(EditorIndicationState.IndicationIconPath);
- }
- return latestIcon;
- }
- private static Image indicationIcon;
- private static Label indicationText;
- bool initialized;
- /// <summary>
- /// Create Hot Reload overlay panel.
- /// </summary>
- public override VisualElement CreatePanelContent() {
- var root = new VisualElement() { name = "Hot Reload Indication" };
- root.style.flexDirection = FlexDirection.Row;
-
- indicationIcon = new Image() { image = GUIHelper.GetLocalIcon(EditorIndicationState.greyIconPath) };
- indicationIcon.style.height = 30;
- indicationIcon.style.width = 30;
- indicationIcon.style.marginLeft = 2;
- indicationIcon.style.marginTop = 1;
- indicationIcon.style.marginRight = 5;
-
- indicationText = new Label(){text = EditorIndicationState.IndicationStatusText};
- indicationText.style.paddingTop = 9;
- indicationText.style.marginLeft = new StyleLength(StyleKeyword.Auto);
- indicationText.style.marginRight = new StyleLength(StyleKeyword.Auto);
-
- root.Add(indicationIcon);
- root.Add(indicationText);
- root.style.width = 190;
- root.style.height = 32;
- initialized = true;
- return root;
- }
- static bool _repaint;
- static bool _instantRepaint;
- static DateTime _lastRepaint;
- private void Update() {
- if (!initialized) {
- return;
- }
- if (lastIndicationStatus != EditorIndicationState.CurrentIndicationStatus) {
- indicationIcon.image = GetIndicationIcon();
- indicationText.text = EditorIndicationState.IndicationStatusText;
- lastIndicationStatus = EditorIndicationState.CurrentIndicationStatus;
- }
- try {
- if (HotReloadEventPopup.I.open
- && EditorWindow.mouseOverWindow
- && EditorWindow.mouseOverWindow?.GetType() == typeof(UnityEditor.PopupWindow)
- ) {
- _repaint = true;
- }
- } catch (NullReferenceException) {
- // Unity randomly throws nullrefs when EditorWindow.mouseOverWindow gets accessed
- }
- if (_repaint && DateTime.UtcNow - _lastRepaint > TimeSpan.FromMilliseconds(33)) {
- _repaint = false;
- _instantRepaint = true;
- }
- if (_instantRepaint) {
- HotReloadEventPopup.I.Repaint();
- }
- }
- ~HotReloadOverlay() {
- EditorApplication.update -= Update;
- }
- }
- }
- #endif
|