Advertisement
Guest User

InternalClassUtilities

a guest
Feb 21st, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.12 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Reflection;
  3. using UnityEditor;
  4.  
  5. namespace Midva
  6. {
  7.     public class InternalClassUtilities
  8.     {
  9.         /// <summary>
  10.         /// Getting from assembly variable and/or method that later can be called
  11.         /// EXAMPLE: Get instance of ScriptableSingleton<GameViewSizes> and delete it so it gets reimported again
  12.         /// </summary>
  13.         private void InternalMethodAndProperty()
  14.         {
  15.             //Get assembly in which class we need is
  16.             Assembly assembly = typeof(UnityEditor.Editor).Assembly;
  17.  
  18.             //Type of class we need
  19.             System.Type gameViewSizesType = assembly.GetType("UnityEditor.GameViewSizes"); //TODO: Change string to class you need
  20.             //In this example GameViewSizes is ScriptableSingleton<>, so we need to cast it to ScriptableSingleton as generic type GameViewSizes
  21.             System.Type scriptableSingletonType = typeof(ScriptableSingleton<>).MakeGenericType(gameViewSizesType);
  22.  
  23.             //In this example we need property "instance" of ScriptableSingleton<>
  24.             //Get all available properties in this class and print them
  25.             PropertyInfo[] properties = scriptableSingletonType.GetProperties();
  26.             Debug.LogError("Properties of " + scriptableSingletonType.Name + ":");
  27.             foreach (PropertyInfo property in properties)
  28.             {
  29.                 Debug.Log(property.Name);
  30.             }
  31.             //We need a instance property in this class
  32.             PropertyInfo instanceProperty = scriptableSingletonType.GetProperty("instance");  //TODO: Change string to property you need
  33.  
  34.             //Get all available methods in this class and print them
  35.             MethodInfo[] methods = gameViewSizesType.GetMethods();
  36.             Debug.LogError("Methods of " + gameViewSizesType.Name + ":");
  37.             foreach (MethodInfo method in methods)
  38.             {
  39.                 Debug.Log(method.Name);
  40.             }
  41.             //Get one of those method
  42.             MethodInfo methodChanged = gameViewSizesType.GetMethod("Changed"); //TODO: Change string to method you need
  43.  
  44.             //Get instance value
  45.             object instanceObject = instanceProperty.GetValue(null, null);
  46.  
  47.             //Invoke method
  48.             methodChanged.Invoke(instanceObject, null);
  49.  
  50.             //Delete so we regenerate ScriptableSingleton<> in this example
  51.             //DestroyImmediate((Object)instanceObject);
  52.         }
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement