public void TestActionInvokeSuccess() { var container = ScriptableObject.CreateInstance<MethodContainer>(); var action = new SerializedAction { MethodContainer = container, MethodName = "Action" }; var actionInt = new SerializedAction<int> { MethodContainer = container, MethodName = "ActionInt" }; var actionIntDouble = new SerializedAction<int, double> { MethodContainer = container, MethodName = "ActionIntDouble" }; var actionIntDoubleBool = new SerializedAction<int, double, bool> { MethodContainer = container, MethodName = "ActionIntDoubleBool" }; Assert.IsTrue(action.CanInvoke); Assert.IsTrue(actionInt.CanInvoke); Assert.IsTrue(actionIntDouble.CanInvoke); Assert.IsTrue(actionIntDoubleBool.CanInvoke); action.Invoke(); actionInt.Invoke(1); actionIntDouble.Invoke(1, 1.0); actionIntDoubleBool.Invoke(1, 1.0, true); Assert.AreEqual(4, container.InvokeCount); }
public void TestActionInvokeSuccess() { var container = ScriptableObject.CreateInstance <MethodContainer>(); var action = new SerializedAction { MethodContainer = container, MethodName = "Action" }; var actionInt = new SerializedAction <int> { MethodContainer = container, MethodName = "ActionInt" }; var actionIntDouble = new SerializedAction <int, double> { MethodContainer = container, MethodName = "ActionIntDouble" }; var actionIntDoubleBool = new SerializedAction <int, double, bool> { MethodContainer = container, MethodName = "ActionIntDoubleBool" }; Assert.IsTrue(action.CanInvoke); Assert.IsTrue(actionInt.CanInvoke); Assert.IsTrue(actionIntDouble.CanInvoke); Assert.IsTrue(actionIntDoubleBool.CanInvoke); action.Invoke(); actionInt.Invoke(1); actionIntDouble.Invoke(1, 1.0); actionIntDoubleBool.Invoke(1, 1.0, true); Assert.AreEqual(4, container.InvokeCount); }
public void TestActionInvokeFail() { var container = ScriptableObject.CreateInstance<MethodContainer>(); var action0 = new SerializedAction { MethodContainer = container, MethodName = "Fake" }; var action1 = new SerializedAction<int> { MethodContainer = null, MethodName = null, }; var action2 = new SerializedAction<int> { MethodContainer = null, MethodName = string.Empty, }; var action3 = new SerializedAction<int> { MethodContainer = null, MethodName = "Action", }; var action4 = new SerializedAction<int, double> { MethodContainer = container, MethodName = string.Empty }; var action5 = new SerializedAction<int, double, bool> { MethodContainer = container, MethodName = null }; Assert.False(action0.CanInvoke); Assert.False(action1.CanInvoke); Assert.False(action2.CanInvoke); Assert.False(action3.CanInvoke); Assert.False(action4.CanInvoke); Assert.False(action5.CanInvoke); Assert.Throws<InvalidOperationException>(() => action0.Invoke()); Assert.Throws<InvalidOperationException>(() => action1.Invoke(1)); Assert.Throws<InvalidOperationException>(() => action2.Invoke(1)); Assert.Throws<InvalidOperationException>(() => action3.Invoke(1)); Assert.Throws<InvalidOperationException>(() => action4.Invoke(1, 1.0)); Assert.Throws<InvalidOperationException>(() => action5.Invoke(1, 1.0, true)); }
public void TestActionInvokeFail() { var container = ScriptableObject.CreateInstance <MethodContainer>(); var action0 = new SerializedAction { MethodContainer = container, MethodName = "Fake" }; var action1 = new SerializedAction <int> { MethodContainer = null, MethodName = null, }; var action2 = new SerializedAction <int> { MethodContainer = null, MethodName = string.Empty, }; var action3 = new SerializedAction <int> { MethodContainer = null, MethodName = "Action", }; var action4 = new SerializedAction <int, double> { MethodContainer = container, MethodName = string.Empty }; var action5 = new SerializedAction <int, double, bool> { MethodContainer = container, MethodName = null }; Assert.False(action0.CanInvoke); Assert.False(action1.CanInvoke); Assert.False(action2.CanInvoke); Assert.False(action3.CanInvoke); Assert.False(action4.CanInvoke); Assert.False(action5.CanInvoke); Assert.Throws <InvalidOperationException>(() => action0.Invoke()); Assert.Throws <InvalidOperationException>(() => action1.Invoke(1)); Assert.Throws <InvalidOperationException>(() => action2.Invoke(1)); Assert.Throws <InvalidOperationException>(() => action3.Invoke(1)); Assert.Throws <InvalidOperationException>(() => action4.Invoke(1, 1.0)); Assert.Throws <InvalidOperationException>(() => action5.Invoke(1, 1.0, true)); }
public override void OnInspectorGUI() { base.DrawDefaultInspector(); RaycastOnClick targetInstance = (RaycastOnClick)target; SerializedObject objInstance = new SerializedObject(targetInstance); // always run serialization on script objInstance.Update(); if (GUILayout.Button("Get methods")) { methodNames.Clear(); // Get all methods of the script List <MethodInfo> methodInfo = typeof(RaycastOnClick).GetMethods().ToList(); // Get each name foreach (MethodInfo method in methodInfo) { // if it is not a derived class (?! that is what i noticed) if (method.Equals(method.GetBaseDefinition())) { scriptMethods.Add(method); methodNames.Add(method.Name); } } // if names found, initalize methods pop-up values if (scriptMethods.Count > 0) { selectedMethodNameIndex = 0; selectedMethodName = methodNames[0]; gotMethods = true; } } // Display methods pop-up if (gotMethods) { selectedMethodNameIndex = EditorGUILayout.Popup("Available methods: " + methodNames.Count, selectedMethodNameIndex, methodNames.ToArray()); selectedMethodName = methodNames[selectedMethodNameIndex]; //Debug.Log ("Method index: " + selectedMethodNameIndex + methodNames[selectedMethodNameIndex]); } if (GUILayout.Button("Get parameters for method: " + selectedMethodName)) { parameters.Clear(); MethodInfo methodInfo = scriptMethods[methodNames.IndexOf(selectedMethodName)]; ParameterInfo[] infos = methodInfo.GetParameters(); foreach (ParameterInfo info in infos) { //Debug.Log("Parameter name " + info.Name + ", parameter type: " + info.ParameterType); // if parameter derives from System if (info.ParameterType.IsSubclassOf(typeof(System.Object)) || info.ParameterType.IsSubclassOf(typeof(UnityEngine.Object))) { var param = DrawFieldBaseOnType(info.ParameterType, info.Name); parameters.Add(param); parametersNames.Add(info.Name); } else { Debug.LogError("Cannot assign method! All method's parameters types must derive either from System.Object or UnityEngine.Object. Error occured with parameter " + info.Name + " of type: " + info.ParameterType); EditorGUILayout.HelpBox("Cannot assign method! All method's parameters types must derive either from System.Object or UnityEngine.Object. Error occured with parameter " + info.Name + " of type: " + info.ParameterType, MessageType.Error); } } if (parameters.Count > 0) { gotParameters = true; } } if (gotParameters) { EditorGUILayout.LabelField("Trigger"); triggerGameObject = EditorGUILayout.ObjectField(triggerGameObject, typeof(GameObject), true, GUILayout.MaxWidth(500), GUILayout.MinWidth(170)) as GameObject; for (int i = 0; i < parameters.Count; i++) { //Debug.Log ("Parameter " + i + " is: " + parameters[i]); System.Object param = DrawFieldBaseOnType(parameters[i], parameters[i].GetType(), parametersNames[i]); parameters[i] = param; } } if (GUILayout.Button("Add method for execution " + selectedMethodName)) { if (triggerGameObject != null) { SerializedAction action = new SerializedAction(triggerGameObject, selectedMethodName, parameters.ToArray(), parametersNames); targetInstance.ActionsToPerform.Add(action); Debug.Log("Added action " + action.methodName + " for exuction"); parameters.Clear(); triggerGameObject = null; } else { EditorGUILayout.HelpBox("Trigger gameobject has not been defined", MessageType.Error); Debug.LogError("Trigger gameobject has not been defined"); } } if (GUILayout.Button("Clear delegates ")) { targetInstance.ActionsToPerform.Clear(); targetInstance.ActionsToPerform = new List <SerializedAction>(); Debug.Log("Actions to perform cleared. Number of actions: " + targetInstance.ActionsToPerform.Count); } if (targetInstance.ActionsToPerform.Count > 0) { foreach (SerializedAction action in targetInstance.ActionsToPerform) { EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Action to execute: ", GUILayout.Width(125)); EditorGUILayout.LabelField(action.methodName, EditorStyles.boldLabel); EditorGUILayout.EndHorizontal(); action.triggerObject = EditorGUILayout.ObjectField("Trigger GameObject: ", action.triggerObject, typeof(GameObject), true) as GameObject; if (action.arguments == null) { action.GetAction(targetInstance); } if (action.arguments != null) { for (int i = 0; i < action.arguments.Length; i++) { //Debug.Log ("Argument of action: " + action.methodName + " is " + action.arguments[i]); if (action.arguments[i] != null) { if (action.unityArguments[i] != null) { UnityEngine.Object prevArg = action.unityArguments[i]; action.unityArguments[i] = DrawFieldBaseOnType(action.unityArguments[i], action.unityArguments[i].GetType(), action.argumentNames[i]) as UnityEngine.Object; } else { System.Object prevArg = action.arguments[i]; action.arguments[i] = DrawFieldBaseOnType(action.arguments[i], action.arguments[i].GetType(), action.argumentNames[i]); } } } if (GUILayout.Button("SAVE CHANGES FOR: " + action.methodName)) { objInstance.ApplyModifiedProperties(); EditorUtility.SetDirty(targetInstance); action.SerializeArguments(); AssetDatabase.SaveAssets(); } } else { Debug.Log("Could not load given arguments!"); EditorGUILayout.HelpBox("Could not load given arguments!", MessageType.Error); } } } }