public static T CreateDelegate <T>(string fullTypeName, string methodName) where T : Delegate
        {
            var type = ReflectionExtensions.GetTypeByName(fullTypeName);

            if (type == null)
            {
                Debug.LogError($"type == null (fullTypeName: {fullTypeName})");
                return(null);
            }
            var parameterTypes = (from param in typeof(T).GetMethod("Invoke").GetParameters() select param.ParameterType).ToArray();
            var methodInfo     = type.GetStaticMethod(methodName, parameterTypes);

            if (methodInfo == null)
            {
                Debug.LogError($"methodInfo == null (methodName: {methodName})");
                return(null);
            }
            try
            {
                return((T)Delegate.CreateDelegate(typeof(T), null, methodInfo, true));
            }
            catch (Exception ex)
            {
                Debug.LogError($"{methodName}'s signature might've been modified between Unity versions");
                throw ex;
            }
        }
        public static ReflectedField <T> GetStaticField <T>(string fullTypeName, string fieldName)
        {
            var type = ReflectionExtensions.GetTypeByName(fullTypeName);

            if (type == null)
            {
                Debug.LogError("type == null");
                return(null);
            }
            var field = type.GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Public);

            if (field == null)
            {
                return(null);
            }
            return(new ReflectedField <T>(null, field));
        }
        public static ReflectedInstanceProperty <T> GetProperty <T>(string fullTypeName, string propertyName)
        {
            var type = ReflectionExtensions.GetTypeByName(fullTypeName);

            if (type == null)
            {
                Debug.LogError("type == null");
                return(null);
            }
            var property = type.GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.Public | System.Reflection.BindingFlags.Instance);

            if (property == null)
            {
                Debug.LogError($"property == null (propertyName: {propertyName})");
            }
            return(new ReflectedInstanceProperty <T>(property));
        }
예제 #4
0
        public static T CreateDelegate <T>(string fullTypeName, string methodName) where T : Delegate
        {
            var type = ReflectionExtensions.GetTypeByName(fullTypeName);

            if (type == null)
            {
                Debug.LogError($"type == null (fullTypeName: {fullTypeName})");
                return(null);
            }
            var parameterTypes = (from param in typeof(T).GetMethod("Invoke").GetParameters() select param.ParameterType).ToArray();
            var methodInfo     = type.GetStaticMethod(methodName, parameterTypes);

            if (methodInfo == null)
            {
                Debug.LogError($"methodInfo == null (methodName: {methodName})");
                return(null);
            }
            return((T)Delegate.CreateDelegate(typeof(T), null, methodInfo, true));
        }
예제 #5
0
        void Initialize(int primaryOrder)
        {
            if (windowMethod == null)
            {
#if UNITY_2019_3
                s_OverlayWindowType = ReflectionExtensions.GetTypeByName("UnityEditor.SceneViewOverlay+OverlayWindow");
#elif UNITY_2020_1_OR_NEWER
                s_OverlayWindowType = ReflectionExtensions.GetTypeByName("UnityEditor.OverlayWindow");
#endif
                s_WindowFunctionType   = ReflectionExtensions.GetTypeByName("UnityEditor.SceneViewOverlay+WindowFunction");
                s_SceneViewOverlayType = ReflectionExtensions.GetTypeByName("UnityEditor.SceneViewOverlay");

                var windowDisplayOptionType = ReflectionExtensions.GetTypeByName("UnityEditor.SceneViewOverlay+WindowDisplayOption");
                s_WindowMethod_parameter_overlay = Enum.Parse(windowDisplayOptionType, "OneWindowPerTarget");

#if UNITY_2019_3
                windowMethod = s_SceneViewOverlayType.GetMethods(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public).FirstOrDefault(t => t.Name == "Window" && t.GetParameters().Length == 4);
#elif UNITY_2020_1_OR_NEWER
                //public static void ShowWindow(OverlayWindow window)
                windowMethod = s_SceneViewOverlayType.GetMethod("ShowWindow", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
#endif
            }

            InternalWindowFunction outerWindowFunc = OuterWindowFunc;
            var sceneViewFuncDelegate = Delegate.CreateDelegate(s_WindowFunctionType, this, outerWindowFunc.Method);

#if UNITY_2019_3
            windowMethod_parameters = new object[]
            {
                title, sceneViewFuncDelegate, primaryOrder, s_WindowMethod_parameter_overlay
            };
#elif UNITY_2020_1_OR_NEWER
            //public OverlayWindow(GUIContent title, SceneViewOverlay.WindowFunction guiFunction, int primaryOrder, Object target, SceneViewOverlay.WindowDisplayOption option)
            var overlayWindow = Activator.CreateInstance(s_OverlayWindowType, title, sceneViewFuncDelegate, primaryOrder, null, s_WindowMethod_parameter_overlay);
            windowMethod_parameters = new object[] { overlayWindow };
#endif
        }
예제 #6
0
        static ChiselRectSelection()
        {
            reflectionSucceeded = false;

            unityRectSelectionType = ReflectionExtensions.GetTypeByName("UnityEditor.RectSelection");
            if (unityRectSelectionType == null)
            {
                return;
            }

            unityEnumSelectionType = ReflectionExtensions.GetTypeByName("UnityEditor.RectSelection+SelectionType");
            if (unityEnumSelectionType == null)
            {
                return;
            }

            rectSelectionField = typeof(SceneView).GetField("m_RectSelection", BindingFlags.NonPublic | BindingFlags.Instance);
            if (rectSelectionField == null)
            {
                return;
            }

            rectSelectionIDField = unityRectSelectionType.GetField("s_RectSelectionID", BindingFlags.NonPublic | BindingFlags.Static);
            if (rectSelectionIDField == null)
            {
                return;
            }

            RectSelectionID       = (int)rectSelectionIDField.GetValue(null);
            rectSelectingField    = unityRectSelectionType.GetField("m_RectSelecting", BindingFlags.NonPublic | BindingFlags.Instance);
            selectStartPointField = unityRectSelectionType.GetField("m_SelectStartPoint", BindingFlags.NonPublic | BindingFlags.Instance);
            selectionStartField   = unityRectSelectionType.GetField("m_SelectionStart", BindingFlags.NonPublic | BindingFlags.Instance);
            lastSelectionField    = unityRectSelectionType.GetField("m_LastSelection", BindingFlags.NonPublic | BindingFlags.Instance);
            currentSelectionField = unityRectSelectionType.GetField("m_CurrentSelection", BindingFlags.NonPublic | BindingFlags.Instance);
            selectMousePointField = unityRectSelectionType.GetField("m_SelectMousePoint", BindingFlags.NonPublic | BindingFlags.Instance);

            updateSelectionMethod = unityRectSelectionType.GetMethod("UpdateSelection", BindingFlags.NonPublic | BindingFlags.Static,
                                                                     null,
                                                                     new Type[] {
                typeof(UnityEngine.Object[]),
                typeof(UnityEngine.Object[]),
                unityEnumSelectionType,
                typeof(bool)
            },
                                                                     null);
            selectionTypeAdditive    = Enum.Parse(unityEnumSelectionType, "Additive");
            selectionTypeSubtractive = Enum.Parse(unityEnumSelectionType, "Subtractive");
            selectionTypeNormal      = Enum.Parse(unityEnumSelectionType, "Normal");

            reflectionSucceeded = rectSelectingField != null &&
                                  selectStartPointField != null &&
                                  selectionStartField != null &&
                                  lastSelectionField != null &&
                                  currentSelectionField != null &&
                                  selectMousePointField != null &&
                                  updateSelectionMethod != null &&

                                  selectionTypeAdditive != null &&
                                  selectionTypeSubtractive != null &&
                                  selectionTypeNormal != null;
        }