HellFireKoder

SimplePlanes Mod UI InputBlocker.cs

Apr 2nd, 2021 (edited)
1,539
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.34 KB | None | 0 0
  1. using System;
  2. using System.Reflection;
  3. using UnityEngine;
  4.  
  5. public class InputBlocker : MonoBehaviour
  6. {
  7.     private RectTransform rect;
  8.  
  9.     private BoxCollider blockCollider;
  10.  
  11.     private static GameObject devConsoleBlockerResource = null;
  12.  
  13.     private int framesEnabled = 0;
  14.  
  15.     // Start is called before the first frame update
  16.     void Awake()
  17.     {
  18.         this.rect = this.GetComponent<RectTransform>();
  19.         if (devConsoleBlockerResource == null)
  20.         {
  21.             devConsoleBlockerResource = Resources.Load<GameObject>("Prefabs/Gui/DevConsoleInputBlocker");
  22.         }
  23.     }
  24.  
  25.     private void OnEnable()
  26.     {
  27.         framesEnabled = 0;
  28.  
  29.         if (this.blockCollider == null)
  30.         {
  31.             GameObject blockObject = GameObject.Instantiate<GameObject>(devConsoleBlockerResource);
  32.             blockObject.name = "ModInputBlocker";
  33.             blockObject.layer = 9;
  34.             try
  35.             {
  36.                 blockObject.transform.SetParent(ServiceProvider.Instance.UserInterface.UIRootObject.transform);
  37.             }
  38.             catch
  39.             {
  40.                 this.enabled = false;
  41.                 Debug.LogWarning("Mod could not parent input blocker. Clicks may go through the Mod UI.");
  42.             }
  43.             blockObject.transform.localPosition = Vector3.zero;
  44.             blockObject.transform.localScale = Vector3.one;
  45.             blockObject.transform.SetAsFirstSibling();
  46.             blockObject.SetActive(true);
  47.             this.blockCollider = blockObject.GetComponent<BoxCollider>();
  48.         }
  49.  
  50.         this.blockCollider.gameObject.SetActive(true);
  51.     }
  52.  
  53.     private void OnDisable()
  54.     {
  55.         if (this.blockCollider != null)
  56.         {
  57.             this.blockCollider.gameObject.SetActive(false);
  58.         }
  59.     }
  60.  
  61.     private void OnDestroy()
  62.     {
  63.         if (this.blockCollider != null)
  64.         {
  65.             GameObject.Destroy(this.blockCollider.gameObject);
  66.         }
  67.     }
  68.  
  69.     private static float? GameScale = null;
  70.  
  71.     private static PropertyInfo UiScale;
  72.     private static object GameSettings;
  73.  
  74.     // Update is called once per frame
  75.     void Update()
  76.     {
  77.         if (framesEnabled == 2)
  78.         {
  79.             Vector3[] array = new Vector3[4];
  80.             this.rect.GetWorldCorners(array);
  81.             Vector3 size = array[2] - array[0];
  82.             Vector3 center = (array[0] + array[2]) / 2f;
  83.             center -= new Vector3((float)Screen.width, (float)Screen.height) / 2f;
  84.             size.z = 0f;
  85.  
  86.             if (GameSettings == null)
  87.             {
  88.                 Type gameType = Type.GetType("Assets.Game.Game, Assembly-CSharp");
  89.                 object gameInstance = gameType.GetProperty("Instance").GetValue(null, null);
  90.  
  91.                 GameSettings = gameType.GetProperty("Settings").GetValue(gameInstance);
  92.                 UiScale = GameSettings.GetType().GetProperty("UiScale");
  93.             }
  94.  
  95.             GameScale = (float)UiScale.GetValue(GameSettings);
  96.             if (GameScale.Value < 1)
  97.             {
  98.                 size /= GameScale.Value;
  99.                 center /= GameScale.Value;
  100.             }
  101.  
  102.             this.blockCollider.size = size;
  103.             this.blockCollider.center = center;
  104.  
  105.             framesEnabled++;
  106.         }
  107.         else if (framesEnabled < 2)
  108.         {
  109.             framesEnabled++;
  110.         }
  111.     }
  112. }
  113.  
Advertisement
Add Comment
Please, Sign In to add comment