Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Reflection;
- using UnityEditor;
- namespace Beast.Claw.Editor
- {
- public class GameViewReflection
- {
- private const BindingFlags INSTANCE_PRIVATE = BindingFlags.Instance | BindingFlags.NonPublic;
- private Type _type_PlayModeWindow;
- private Type _type_ZoomableArea;
- private EditorWindow _window_PlayModeWindow;
- private FieldInfo _field_PlayModeWindow_zoomArea;
- private FieldInfo _field_ZoomableArea_Scale;
- public FieldInfo GetField_ZoomableArea_Scale()
- {
- _cashedType<EditorApplication>("UnityEditor.ZoomableArea", ref _type_ZoomableArea);
- _cashedField(_type_ZoomableArea, "m_Scale", INSTANCE_PRIVATE, ref _field_ZoomableArea_Scale);
- return _field_ZoomableArea_Scale;
- }
- public FieldInfo GetField_PlayModeWindow_ZoomArea()
- {
- _cashedType<EditorApplication>("UnityEditor.GameView", ref _type_PlayModeWindow);
- _cashedField(_type_PlayModeWindow, "m_ZoomArea", INSTANCE_PRIVATE, ref _field_PlayModeWindow_zoomArea);
- return _field_PlayModeWindow_zoomArea;
- }
- public EditorWindow Get_PlayModeWindow()
- {
- _cashedType<EditorApplication>("UnityEditor.GameView", ref _type_PlayModeWindow);
- _cashedWindow(_type_PlayModeWindow, ref _window_PlayModeWindow);
- return _window_PlayModeWindow;
- }
- private void _cashedField(Type ownerType, string fieldName, BindingFlags flags, ref FieldInfo cash)
- {
- if (cash != null)
- {
- return;
- }
- cash = ownerType.GetField(fieldName, flags);
- if (cash == null)
- {
- throw new Exception($"Failed to get field {ownerType} ({fieldName})");
- }
- }
- private void _cashedWindow(Type windowType, ref EditorWindow cash)
- {
- if (cash != null)
- {
- return;
- }
- cash = EditorWindow.GetWindow(windowType);
- if (cash == null)
- {
- throw new Exception($"Failed: EditorWindow.GetWindow({windowType.FullName})");
- }
- }
- private void _cashedType<TAssembly>(string name, ref Type cash)
- {
- if (cash != null)
- {
- return;
- }
- cash = typeof(TAssembly).Assembly.GetType(name, true);
- }
- }
- }
- #if UNITY_2022_3_45
- using System;
- using System.Reflection;
- using Beast.Claw.Attributes;
- using Beast.Claw.Debug;
- using UnityEditor;
- using UnityEngine;
- namespace Beast.Claw.Editor
- {
- /// <summary>
- /// Unity 2022.3.45
- /// Fixes Game View Editor Window scale reset after domain reloaded.
- /// </summary>
- [DomainInitialize]
- public static class GameViewScaleSelfResetFix
- {
- private const string PREFS_KEY_X = nameof(GameViewScaleSelfResetFix) + "_AAUO4ggU0JiK5i";
- private const string PREFS_KEY_Y = nameof(GameViewScaleSelfResetFix) + "_9iGSn8r4uhU0Am";
- private static readonly GameViewReflection CASH;
- static GameViewScaleSelfResetFix()
- {
- try
- {
- CASH = new GameViewReflection();
- AssemblyReloadEvents.afterAssemblyReload += _afterAssemblyReload;
- AssemblyReloadEvents.beforeAssemblyReload += _beforeAssemblyReload;
- }
- catch (Exception e)
- {
- BDebug.StaticLogException(e);
- }
- }
- private static void _afterAssemblyReload()
- {
- try
- {
- if (!PlayerPrefs.HasKey(PREFS_KEY_X) ||
- !PlayerPrefs.HasKey(PREFS_KEY_Y))
- {
- return;
- }
- Vector2 scale;
- scale.x = PlayerPrefs.GetFloat(PREFS_KEY_X);
- scale.y = PlayerPrefs.GetFloat(PREFS_KEY_Y);
- PlayerPrefs.DeleteKey(PREFS_KEY_X);
- PlayerPrefs.DeleteKey(PREFS_KEY_Y);
- EditorWindow window = CASH.Get_PlayModeWindow();
- FieldInfo zoomAreaField = CASH.GetField_PlayModeWindow_ZoomArea();
- object zoomArea = zoomAreaField.GetValue(window);
- FieldInfo scaleField = CASH.GetField_ZoomableArea_Scale();
- scaleField.SetValue(zoomArea, scale);
- BDebug.StaticLogInfo("Applied scale: " + scale);
- }
- catch (Exception e)
- {
- BDebug.StaticLogException(e);
- }
- }
- private static void _beforeAssemblyReload()
- {
- try
- {
- EditorWindow window = CASH.Get_PlayModeWindow();
- FieldInfo zoomAreaField = CASH.GetField_PlayModeWindow_ZoomArea();
- object zoomArea = zoomAreaField.GetValue(window);
- FieldInfo scaleField = CASH.GetField_ZoomableArea_Scale();
- Vector2 scale = (Vector2)scaleField.GetValue(zoomArea);
- PlayerPrefs.SetFloat(PREFS_KEY_X, scale.x);
- PlayerPrefs.SetFloat(PREFS_KEY_Y, scale.y);
- BDebug.StaticLogInfo("Saved current scale: " + scale);
- }
- catch (Exception e)
- {
- BDebug.StaticLogException(e);
- }
- }
- }
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement