Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEditor;
- using UnityEditor.Callbacks;
- using UnityEditor.Compilation;
- using UnityEngine;
- public static class FlyoutPopup
- {
- private const float _sizeRatio = 0.4f;
- private const double _animDuration = 0.25f;
- private static WindowData data = new WindowData();
- private static Dictionary<SceneView, WindowData> _windows = new Dictionary<SceneView, WindowData>();
- private static Type _projectBrowserType = System.Reflection.Assembly.GetAssembly(typeof(EditorWindow)).GetTypes().First(t => t.Name == "ProjectBrowser");
- private static Func<Event, bool> _triggerEvent = e => {
- #if UNITY_EDITOR_WIN
- return e.alt && e.keyCode == KeyCode.I;
- #else
- return e.control && e.shift && e.keyCode == KeyCode.P;
- #endif
- };
- [InitializeOnLoadMethod]
- private static void Init() {
- SceneView.duringSceneGui += OnSceneGUI;
- SceneView.duringSceneGui += OnButtonSceneGUI;
- EditorApplication.update += Update;
- CompilationPipeline.assemblyCompilationStarted += CloseAllWindows;
- }
- private class WindowData {
- public EditorWindow Browser;
- public bool Showing;
- public bool EventActive;
- public double AnimStartTime;
- public Rect Target;
- public Rect StartSize;
- }
- private static SceneView currentSceneView;
- private static Event e;
- private static Rect buttonRect;
- private static float lerpValue = 0;
- private static float time = 0;
- [DidReloadScripts]
- private static void ScriptsCompiling() {
- CloseAllWindows(null);
- }
- private static void CloseAllWindows(string _) {
- foreach (var window in _windows)
- if (window.Value.Browser != null)
- window.Value.Browser.Close();
- _windows.Clear();
- }
- private static void Update() {
- if (!UnityEditorInternal.InternalEditorUtility.isApplicationActive) return;
- if (currentSceneView == null) return;
- if (e == null) return;
- if (_triggerEvent(e)) {
- if (data?.EventActive ?? false) return;
- GetProjectBrowser(currentSceneView);
- switch(e.type) {
- case EventType.KeyUp: {
- ShowHidePanel();
- break;
- }
- }
- }
- if (data.Browser == null) {
- GetProjectBrowser(currentSceneView);
- return;
- }
- var padding = 10;
- var targetY = 242;
- // FixSize();
- if (time <= 1) {
- time = (float)(EditorApplication.timeSinceStartup - data.AnimStartTime);
- lerpValue = OutBackEase(time, (float)_animDuration);
- if (data.Showing) {
- data.Browser.position = new Rect(data.Target.x + padding, Mathf.Lerp(data.Target.y, data.Target.y - targetY, lerpValue), data.Target.width - padding*2, Mathf.Lerp(20, 250, lerpValue));
- }
- else {
- data.Browser.minSize = Vector2.zero;
- data.Browser.position = new Rect(data.Target.x + 0, Mathf.Lerp(data.Target.y - targetY, data.Target.y, lerpValue), data.Target.width - 0*2, Mathf.Lerp(250, 20, lerpValue));
- }
- Debug.Log("lerp");
- }
- else {
- if (data.Showing) {
- data.Browser.position = new Rect(data.Target.x + padding, data.Target.y - targetY, data.Target.width - padding*2, 250);
- }
- else {
- data.Browser.minSize = Vector2.zero;
- data.Browser.position = new Rect(data.Target.x + 0, data.Target.y, data.Target.width - 0*2, 20);
- }
- //Debug.Log("no lerp : x: " + data.Target.x + " - width: " + data.Target.width + " - y: " + data.Target.y + " - height: " + data.Target.height);
- }
- data.Browser.Repaint();
- }
- private static float OutBackEase(float time, float duration, float overshoot = 0.25f) {
- return ((time = time / duration - 1) * time * ((overshoot + 1) * time + overshoot) + 1);
- }
- private static void ShowHidePanel() {
- e.Use();
- lerpValue = 0;
- time = 0;
- data.AnimStartTime = EditorApplication.timeSinceStartup;
- data.Showing = !data.Showing;
- }
- private static void FixSize() {
- //TODO: get rid of the flicker when changing layout size
- var scenePosition = SceneView.lastActiveSceneView.position;
- scenePosition.y += scenePosition.height;
- // Debug.Log("scene pos y: " + scenePosition.y);
- // Debug.Log("scene pos: " + scenePosition);
- scenePosition.height = 0;
- data.Target = scenePosition;
- }
- private static void OnSceneGUI(SceneView sceneView) {
- if (!UnityEditorInternal.InternalEditorUtility.isApplicationActive) return;
- currentSceneView = sceneView;
- e = Event.current;
- if (e == null) return;
- if (data.Browser == null) return;
- FixSize();
- }
- private static void OnButtonSceneGUI(SceneView sceneView) {
- Handles.BeginGUI();
- var scenePosition = sceneView.position;
- var padding = 10;
- var up = 290;
- var down = 45;
- var size = 30;
- var heightfix = sceneView.camera.pixelRect.max.y / 3;
- var ypos = sceneView.camera.pixelRect.max.y - heightfix;
- if (time <= 1) {
- if (data.Showing) {
- buttonRect = new Rect(padding, Mathf.Lerp(ypos - down, ypos - up, lerpValue), size, size);
- }
- else {
- buttonRect = new Rect(0, Mathf.Lerp(ypos - up, ypos - down, lerpValue), size, size);
- }
- }
- else {
- if (data.Showing) {
- buttonRect = new Rect(padding, ypos - up, size, size);
- }
- else {
- // buttonRect = new Rect(0, down, size, size);
- // Rect rect = new Rect(Screen.width - width, Screen.height - height, width, height);
- buttonRect = new Rect(0, ypos - down, size, size);
- }
- }
- GUIStyle style = new GUIStyle(GUI.skin.button);
- style.alignment = TextAnchor.MiddleCenter;
- GUI.skin.button.normal.background = EditorGUIUtility.FindTexture("d_winbtn_mac_close");
- if (data.Showing) {
- GUI.Box(buttonRect, EditorGUIUtility.IconContent("d_scrolldown_uielements@2x"), style);
- }
- else {
- GUI.Box(buttonRect, EditorGUIUtility.IconContent("d_scrollup_uielements@2x"), style);
- }
- if (e.type == EventType.MouseDown && buttonRect.Contains(e.mousePosition)) {
- ShowHidePanel();
- Debug.Log("work whore!!");
- }
- Handles.EndGUI();
- }
- private static WindowData GetProjectBrowser(SceneView sceneView) {
- try {
- if (_windows.Count == 0) {
- if (data.Browser == null) {
- data.Browser = ScriptableObject.CreateInstance(_projectBrowserType) as EditorWindow;
- var scenePosition = SceneView.lastActiveSceneView.position;
- scenePosition.y += scenePosition.height;
- scenePosition.height = 0;
- data.Target = scenePosition;
- _windows.Add(sceneView, data);
- data.Browser.ShowPopup();
- data.Browser.Repaint();
- data.Showing = false;
- Action<SceneView> validate = null;
- validate = _ => {
- if (!ValidateBrowser(sceneView, data.Browser))
- SceneView.beforeSceneGui -= validate;
- };
- SceneView.beforeSceneGui += validate;
- EditorApplication.CallbackFunction validateEditor = null;
- validateEditor = () => {
- if (!ValidateBrowser(sceneView, data.Browser))
- EditorApplication.update -= validateEditor;
- };
- EditorApplication.update += validateEditor;
- }
- return data;
- }
- }
- catch (Exception ex) {
- Debug.LogException(ex);
- }
- return null;
- }
- private static bool ValidateBrowser(SceneView sceneView, EditorWindow browser) {
- if (sceneView == null) {
- _windows.Remove(sceneView);
- if (browser != null) {
- browser.Close();
- }
- return false;
- }
- if (!_windows.ContainsKey(sceneView) && browser != null) {
- browser.Close();
- return false;
- }
- if (browser == null)
- return false;
- return true;
- }
- }
Add Comment
Please, Sign In to add comment