/// <summary> /// Adds the board script and sets its values /// </summary> /// <param name="node"></param> private static void TraverseBoard(GameObject node) { var count = GameObjectUtility.RemoveMonoBehavioursWithMissingScript(node); if (count == 0) { DestroyImmediate(node.GetComponent <Board>()); } var board = node.AddComponent <Board>(); // references board.GameManager = gameManager; board.flipDistance = 1; board.flipDuration = 1; // events board.TouchOrb = new UnityEvent(); LinkEvents += () => { var action = Delegate.CreateDelegate( typeof(UnityAction), root.transform.Find("Board").GetComponent <Board>(), "DisplayOrbMessage" ) as UnityAction; UnityEventTools.AddVoidPersistentListener(board.TouchOrb, action); }; }
void TryRegisterVoid(UnityEventBase poller, UnityAction f) { var count = poller.GetPersistentEventCount(); if (this._clean_empty_no_target_events && count > 0) { //poller.PollEvent.RemoveAllListeners(); // Only non-persistant listeners. for (var i = 0; i < count; i++) { if (poller.GetPersistentTarget(i) == null || poller.GetPersistentMethodName(i) == null) { UnityEventTools.RemovePersistentListener(poller, i); } } } count = poller.GetPersistentEventCount(); if (count == 0) { UnityEventTools.AddVoidPersistentListener(poller, f); poller.SetPersistentListenerState(0, this._unity_event_call_state); } else if (count > 0) { #if NEODROID_DEBUG if (this.Debugging) { Debug.Log($"PollEvent on {poller} already has a listeners"); } #endif } }
/// <summary> /// Creates a default Close button. /// </summary> public static ButtonData CreateCloseButton(TutorialWelcomePage page) { var data = new ButtonData { Text = "Close", OnClick = new UnityEvent() }; UnityEventTools.AddVoidPersistentListener(data.OnClick, page.CloseCurrentModalDialog); data.OnClick.SetPersistentListenerState(0, UnityEventCallState.EditorAndRuntime); return(data); }
public override void OnInspectorGUI() { InputReceiverDelegate ird = (InputReceiverDelegate)target; int sum = ird.menuAcceptEvent.GetPersistentEventCount() * ird.menuBackEvent.GetPersistentEventCount() * ird.menuMoveEvent.GetPersistentEventCount() * ird.menuFailEvent.GetPersistentEventCount(); if (sum == 0) { if (GUILayout.Button("Assign Menu Events")) { if (ird.menuAcceptEvent.GetPersistentEventCount() == 0) { Undo.RecordObject(target, "Added listener"); string[] guids = AssetDatabase.FindAssets("MenuAcceptEvent"); string result = AssetDatabase.GUIDToAssetPath(guids[0]); Debug.Log(result); GameEvent ge = (GameEvent)AssetDatabase.LoadAssetAtPath(result, typeof(GameEvent)); UnityEventTools.AddVoidPersistentListener(ird.menuAcceptEvent, ge.Raise); } if (ird.menuBackEvent.GetPersistentEventCount() == 0) { Undo.RecordObject(target, "Added listener"); string[] guids = AssetDatabase.FindAssets("MenuBackEvent"); string result = AssetDatabase.GUIDToAssetPath(guids[0]); Debug.Log(result); GameEvent ge = (GameEvent)AssetDatabase.LoadAssetAtPath(result, typeof(GameEvent)); UnityEventTools.AddVoidPersistentListener(ird.menuBackEvent, ge.Raise); } if (ird.menuMoveEvent.GetPersistentEventCount() == 0) { Undo.RecordObject(target, "Added listener"); string[] guids = AssetDatabase.FindAssets("MenuMoveEvent"); string result = AssetDatabase.GUIDToAssetPath(guids[0]); Debug.Log(result); GameEvent ge = (GameEvent)AssetDatabase.LoadAssetAtPath(result, typeof(GameEvent)); UnityEventTools.AddVoidPersistentListener(ird.menuMoveEvent, ge.Raise); } if (ird.menuFailEvent.GetPersistentEventCount() == 0) { Undo.RecordObject(target, "Added listener"); string[] guids = AssetDatabase.FindAssets("MenuFailEvent"); string result = AssetDatabase.GUIDToAssetPath(guids[0]); Debug.Log(result); GameEvent ge = (GameEvent)AssetDatabase.LoadAssetAtPath(result, typeof(GameEvent)); UnityEventTools.AddVoidPersistentListener(ird.menuFailEvent, ge.Raise); } } } DrawDefaultInspector(); }
protected virtual void ConvertButtonToMMFPlayerDemo() { #if UNITY_EDITOR if (TargetMMF_Player != null) { TargetButton.onClick = new Button.ButtonClickedEvent(); UnityAction action = new UnityAction(TargetMMF_Player.PlayFeedbacks); UnityEventTools.AddVoidPersistentListener(TargetButton.onClick, action); EditorUtility.SetDirty(TargetButton); PrefabUtility.RecordPrefabInstancePropertyModifications(gameObject.transform); } #endif }
bool AddVoidPersistentListenerOnce(EventTrigger evTrigger, UnityAction call, EventTriggerType eventId) { if (!ContainsTriggerCallback(evTrigger, call, eventId)) { EventTrigger.Entry entry = new EventTrigger.Entry() { eventID = eventId }; UnityEventTools.AddVoidPersistentListener(entry.callback, call); evTrigger.triggers.Add(entry); Debug.Log("Added UnityEvent to " + evTrigger.name + " calling " + call.Method.Name + " when " + eventId + " is triggered!", evTrigger); return(true); } return(false); }
public void Bind(UnityEvent @event) { #if UNITY_EDITOR if (value == null) { System.Reflection.MethodInfo targetinfo = UnityEvent.GetValidMethodInfo(target, setMethodName, new Type[0]); if (targetinfo == null) { Debug.LogError("no method " + setMethodName + "() in " + target.ToString()); } UnityAction action = Delegate.CreateDelegate(typeof(UnityAction), target, targetinfo, false) as UnityAction; UnityEventTools.AddVoidPersistentListener(@event, action); } else if (value is int) { UnityEventTools.AddIntPersistentListener(@event, GetAction <int>(target, setMethodName), (int)value); } else if (value is float) { UnityEventTools.AddFloatPersistentListener(@event, GetAction <float>(target, setMethodName), (float)value); } else if (value is string) { UnityEventTools.AddStringPersistentListener(@event, GetAction <string>(target, setMethodName), (string)value); } else if (value is bool) { UnityEventTools.AddBoolPersistentListener(@event, GetAction <bool>(target, setMethodName), (bool)value); } else if (value is GameObject) { Bind <GameObject>(@event); } else if (value is Transform) { Bind <Transform>(@event); } else { Debug.LogError("unable to assign " + value.GetType()); } #else System.Reflection.MethodInfo targetinfo = UnityEvent.GetValidMethodInfo(target, setMethodName, new Type[0]); @event.AddListener(() => targetinfo.Invoke(target, new object[] { value })); #endif }
// Try to create a link between two nodes. // Returns true if the link is established successfully. public static bool TryLinkNodes( NodeBase nodeFrom, Outlet outlet, NodeBase nodeTo, MethodInfo targetMethod ) { var triggerEvent = outlet.boundEvent; // Determine the type of the target action. var actionType = GetUnityActionToInvokeMethod(targetMethod); if (actionType == null) { return(false); // invalid target method type } // Create an action that is bound to the target method. var targetAction = Delegate.CreateDelegate( actionType, nodeTo, targetMethod ); if (triggerEvent is UnityEvent) { // The trigger event has no parameter. // Add the action to the event with a default parameter. if (actionType == typeof(UnityAction)) { UnityEventTools.AddVoidPersistentListener( triggerEvent, (UnityAction)targetAction ); return(true); } if (actionType == typeof(UnityAction <float>)) { UnityEventTools.AddFloatPersistentListener( triggerEvent, (UnityAction <float>)targetAction, 1.0f ); return(true); } //if (actionType == typeof(UnityAction<object>)) //{ // UnityEventTools.AddPersistentListener( // triggerEvent, (UnityAction<object>)targetAction, // ); // return true; //} } else if (triggerEvent is UnityEvent <float> ) { // The trigger event has a float parameter. // Then the target method should have a float parameter too. if (actionType == typeof(UnityAction <float>)) { // Add the action to the event. UnityEventTools.AddPersistentListener( (UnityEvent <float>)triggerEvent, (UnityAction <float>)targetAction ); return(true); } } else if (triggerEvent is UnityEvent <int> ) { // The trigger event has a float parameter. // Then the target method should have a float parameter too. if (actionType == typeof(UnityAction <int>)) { // Add the action to the event. UnityEventTools.AddPersistentListener( (UnityEvent <int>)triggerEvent, (UnityAction <int>)targetAction ); return(true); } } else if (triggerEvent is UnityEvent <string> ) { // The trigger event has a float parameter. // Then the target method should have a float parameter too. if (actionType == typeof(UnityAction <string>)) { // Add the action to the event. UnityEventTools.AddPersistentListener( (UnityEvent <string>)triggerEvent, (UnityAction <string>)targetAction ); return(true); } } else if (triggerEvent is UnityEvent <Vector3> ) { // The trigger event has a Vector3 parameter. // Then the target method should have a Vector3 parameter too. if (actionType == typeof(UnityAction <Vector3>)) { // Add the action to the event. UnityEventTools.AddPersistentListener( (UnityEvent <Vector3>)triggerEvent, (UnityAction <Vector3>)targetAction ); return(true); } } else if (triggerEvent is UnityEvent <Quaternion> ) { // The trigger event has a Quaternion parameter. // Then the target method should have a Quaternion parameter too. if (actionType == typeof(UnityAction <Quaternion>)) { // Add the action to the event. UnityEventTools.AddPersistentListener( (UnityEvent <Quaternion>)triggerEvent, (UnityAction <Quaternion>)targetAction ); return(true); } } else if (triggerEvent is UnityEvent <Color> ) { // The trigger event has a color parameter. // Then the target method should have a color parameter too. if (actionType == typeof(UnityAction <Color>)) { // Add the action to the event. UnityEventTools.AddPersistentListener( (UnityEvent <Color>)triggerEvent, (UnityAction <Color>)targetAction ); return(true); } } else if (triggerEvent is UnityEvent <GameObject> ) { // The trigger event has a color parameter. // Then the target method should have a color parameter too. if (actionType == typeof(UnityAction <GameObject>)) { // Add the action to the event. UnityEventTools.AddPersistentListener( (UnityEvent <GameObject>)triggerEvent, (UnityAction <GameObject>)targetAction ); return(true); } } else if (triggerEvent is UnityEvent <Transform> ) { // The trigger event has a color parameter. // Then the target method should have a color parameter too. if (actionType == typeof(UnityAction <Transform>)) { // Add the action to the event. UnityEventTools.AddPersistentListener( (UnityEvent <Transform>)triggerEvent, (UnityAction <Transform>)targetAction ); return(true); } } else if (triggerEvent is UnityEvent <Collision> ) { // The trigger event has a color parameter. // Then the target method should have a color parameter too. if (actionType == typeof(UnityAction <Collision>)) { // Add the action to the event. UnityEventTools.AddPersistentListener( (UnityEvent <Collision>)triggerEvent, (UnityAction <Collision>)targetAction ); return(true); } } else if (triggerEvent is UnityEvent <Collider> ) { // The trigger event has a color parameter. // Then the target method should have a color parameter too. if (actionType == typeof(UnityAction <Collider>)) { // Add the action to the event. UnityEventTools.AddPersistentListener( (UnityEvent <Collider>)triggerEvent, (UnityAction <Collider>)targetAction ); return(true); } } else if (triggerEvent is UnityEvent <Rigidbody> ) { // The trigger event has a color parameter. // Then the target method should have a color parameter too. if (actionType == typeof(UnityAction <Rigidbody>)) { // Add the action to the event. UnityEventTools.AddPersistentListener( (UnityEvent <Rigidbody>)triggerEvent, (UnityAction <Rigidbody>)targetAction ); return(true); } } else if (triggerEvent is UnityEvent <Collision2D> ) { // The trigger event has a color parameter. // Then the target method should have a color parameter too. if (actionType == typeof(UnityAction <Collision2D>)) { // Add the action to the event. UnityEventTools.AddPersistentListener( (UnityEvent <Collision2D>)triggerEvent, (UnityAction <Collision2D>)targetAction ); return(true); } } else if (triggerEvent is UnityEvent <Collider2D> ) { // The trigger event has a color parameter. // Then the target method should have a color parameter too. if (actionType == typeof(UnityAction <Collider2D>)) { // Add the action to the event. UnityEventTools.AddPersistentListener( (UnityEvent <Collider2D>)triggerEvent, (UnityAction <Collider2D>)targetAction ); return(true); } } else if (triggerEvent is UnityEvent <Rigidbody2D> ) { // The trigger event has a color parameter. // Then the target method should have a color parameter too. if (actionType == typeof(UnityAction <Rigidbody2D>)) { // Add the action to the event. UnityEventTools.AddPersistentListener( (UnityEvent <Rigidbody2D>)triggerEvent, (UnityAction <Rigidbody2D>)targetAction ); return(true); } } else if (triggerEvent is UnityEvent <ContactPoint> ) { // The trigger event has a color parameter. // Then the target method should have a color parameter too. if (actionType == typeof(UnityAction <ContactPoint>)) { // Add the action to the event. UnityEventTools.AddPersistentListener( (UnityEvent <ContactPoint>)triggerEvent, (UnityAction <ContactPoint>)targetAction ); return(true); } } else if (triggerEvent is UnityEvent <ContactPoint2D> ) { // The trigger event has a color parameter. // Then the target method should have a color parameter too. if (actionType == typeof(UnityAction <ContactPoint2D>)) { // Add the action to the event. UnityEventTools.AddPersistentListener( (UnityEvent <ContactPoint2D>)triggerEvent, (UnityAction <ContactPoint2D>)targetAction ); return(true); } } else if (triggerEvent is UnityEvent <Bounds> ) { // The trigger event has a color parameter. // Then the target method should have a color parameter too. if (actionType == typeof(UnityAction <Bounds>)) { // Add the action to the event. UnityEventTools.AddPersistentListener( (UnityEvent <Bounds>)triggerEvent, (UnityAction <Bounds>)targetAction ); return(true); } } if (actionType == typeof(UnityAction)) { UnityEventTools.AddVoidPersistentListener(triggerEvent, (UnityAction)targetAction); return(true); } return(false); // trigger-target mismatch }
/// <summary> /// Adds the game manager script and sets its values, also sets the /// player input component /// </summary> /// <param name="node">Game manager game object</param> private static void TraverseGameManager(GameObject node) { var count = GameObjectUtility.RemoveMonoBehavioursWithMissingScript(node); if (count == 0) { DestroyImmediate(node.GetComponent <GameManager>()); } // script var gameManager = node.AddComponent <GameManager>(); // references gameManager.LevelScene = "Level"; gameManager.MenuScene = "Main Menu"; gameManager.Board = root.transform.Find("Board").gameObject; gameManager.Cam = root.transform.Find("Main Camera").gameObject; gameManager.Test = true; gameManager.TestLevel = "Campaign/Tutorial"; gameManager.BearPrefab = AssetDatabase.LoadAssetAtPath <GameObject>("Assets/Prefabs/Bear.prefab"); gameManager.OrbPrefab = AssetDatabase.LoadAssetAtPath <GameObject>("Assets/Prefabs/Orb.prefab"); gameManager.TilePrefab = AssetDatabase.LoadAssetAtPath <GameObject>("Assets/Prefabs/Tile.prefab"); gameManager.OrbMessageUIAnimator = root.transform.Find("Game UI").Find("Orb Message").GetComponent <Animator>(); gameManager.OrbCountUI = root.transform.Find("Game UI").Find("Orb Count Text").gameObject; gameManager.OrbMessageUI = root.transform.Find("Game UI").Find("Orb Message").gameObject; gameManager.PauseMenuUI = root.transform.Find("Pause Menu").Find("Pause Menu UI").gameObject; gameManager.WinMenuUI = root.transform.Find("Win Menu").Find("Win Menu UI").gameObject; gameManager.NextLevelButton = root.transform.Find("Win Menu").Find("Win Menu UI").Find("Next Button").gameObject; // events gameManager.WinLevel = new UnityEvent(); LinkEvents += () => { var action = Delegate.CreateDelegate( typeof(UnityAction), root.transform.Find("Win Menu").GetComponent <WinMenuUI>(), "Show" ) as UnityAction; UnityEventTools.AddVoidPersistentListener(gameManager.WinLevel, action); }; // player input (Input system 1.0.0) var playerInput = gameManager.GetComponent <PlayerInput>(); // settings playerInput.actions = AssetDatabase.LoadAssetAtPath <InputActionAsset>("Assets/Scripts/Level/controls.inputactions"); playerInput.defaultControlScheme = "Keyboard&Mouse"; playerInput.defaultActionMap = "Player"; playerInput.notificationBehavior = PlayerNotifications.InvokeUnityEvents; // events foreach (var item in playerInput.actionEvents) { // name of events are like this: Player/Center[/Keyboard/c] // so only take the part before [] var eventName = item.actionName.Split('[')[0]; // remove all of its listeners try { for (int i = 0; ; i++) { UnityEventTools.RemovePersistentListener(item, i); } } catch (ArgumentOutOfRangeException) { } switch (eventName) { case "Player/Move": LinkEvents += () => { var action = Delegate.CreateDelegate( typeof(UnityAction <InputAction.CallbackContext>), root.transform.Find("Board").GetComponent <Board>(), "MoveAllPlayers" ) as UnityAction <InputAction.CallbackContext>; UnityEventTools.AddPersistentListener(item, action); }; break; case "Player/Look": LinkEvents += () => { var action = Delegate.CreateDelegate( typeof(UnityAction <InputAction.CallbackContext>), root.transform.Find("Main Camera").GetComponent <CameraController>(), "Move" ) as UnityAction <InputAction.CallbackContext>; UnityEventTools.AddPersistentListener(item, action); }; break; case "Player/Center": LinkEvents += () => { var action = Delegate.CreateDelegate( typeof(UnityAction <InputAction.CallbackContext>), root.transform.Find("Main Camera").GetComponent <CameraController>(), "Center" ) as UnityAction <InputAction.CallbackContext>; UnityEventTools.AddPersistentListener(item, action); }; break; case "Player/Flip": LinkEvents += () => { var action = Delegate.CreateDelegate( typeof(UnityAction <InputAction.CallbackContext>), root.transform.Find("Board").GetComponent <Board>(), "Flip" ) as UnityAction <InputAction.CallbackContext>; UnityEventTools.AddPersistentListener(item, action); }; break; case "Player/Pause": LinkEvents += () => { var action = Delegate.CreateDelegate( typeof(UnityAction <InputAction.CallbackContext>), root.transform.Find("Pause Menu").GetComponent <PauseMenu>(), "Pause" ) as UnityAction <InputAction.CallbackContext>; UnityEventTools.AddPersistentListener(item, action); }; break; case "Paused/Resume": LinkEvents += () => { var action = Delegate.CreateDelegate( typeof(UnityAction <InputAction.CallbackContext>), root.transform.Find("Pause Menu").GetComponent <PauseMenu>(), "Resume" ) as UnityAction <InputAction.CallbackContext>; UnityEventTools.AddPersistentListener(item, action); }; break; // ignore other events default: break; } } }
/// <summary> /// Attaches the correct onClick listener to the given button /// </summary> /// <param name="node"></param> private static void TraverseButton(GameObject node) { var button = node.GetComponent <Button>(); button.onClick = new Button.ButtonClickedEvent(); switch (node.transform.parent.name) { case "Game UI": LinkEvents += () => { var action = Delegate.CreateDelegate( typeof(UnityAction), root.transform.Find("Pause Menu").GetComponent <PauseMenu>(), "Pause" ) as UnityAction; UnityEventTools.AddVoidPersistentListener(button.onClick, action); }; break; case "Pause Menu UI": switch (node.name) { case "Resume Button": LinkEvents += () => { var action = Delegate.CreateDelegate( typeof(UnityAction), root.transform.Find("Pause Menu").GetComponent <PauseMenu>(), "Resume" ) as UnityAction; UnityEventTools.AddVoidPersistentListener(button.onClick, action); }; break; case "Restart Button": LinkEvents += () => { var action = Delegate.CreateDelegate( typeof(UnityAction), root.transform.Find("Pause Menu").GetComponent <PauseMenu>(), "Restart" ) as UnityAction; UnityEventTools.AddVoidPersistentListener(button.onClick, action); }; break; case "Quit Button": LinkEvents += () => { var action = Delegate.CreateDelegate( typeof(UnityAction), root.transform.Find("Pause Menu").GetComponent <PauseMenu>(), "Menu" ) as UnityAction; UnityEventTools.AddVoidPersistentListener(button.onClick, action); }; break; default: break; } break; case "Win Menu UI": switch (node.name) { case "Next Button": LinkEvents += () => { var action = Delegate.CreateDelegate( typeof(UnityAction), root.transform.Find("Win Menu").GetComponent <WinMenuUI>(), "NextLevel" ) as UnityAction; UnityEventTools.AddVoidPersistentListener(button.onClick, action); }; break; case "Quit Button": LinkEvents += () => { var action = Delegate.CreateDelegate( typeof(UnityAction), root.transform.Find("Pause Menu").GetComponent <PauseMenu>(), "Menu" ) as UnityAction; UnityEventTools.AddVoidPersistentListener(button.onClick, action); }; break; default: break; } break; default: break; } }
private int nowStringNumber; //現在の番号 //GUIウィンドウ用関数 void DrawNodeWindow(int id) { //自身のウィンドウをアクティブにする if (Event.current.button == 0 && Event.current.type == EventType.MouseDown) { nowActivId = id; } //入力モード選択 mode = (Mode)EditorGUILayout.EnumPopup(mode); //ドラッグ可能 GUI.DragWindow(new Rect(0, 0, 200, 20)); //入力 if (nowActivId == id) { switch (mode) { case Mode.FUNC: bool compornentsFlag = false; window.height = 115; //windowの高さ変更 //ラベル表示 EditorGUILayout.LabelField("State移行時に呼び出すもの"); EditorGUILayout.LabelField("アタッチされているスクリプトから"); EditorGUILayout.LabelField("引数のないpublicメソッドのみ"); //アタッチされているすべてのコンポーネントを取得(コンポーネントが違えば取得) if (compornents != gameObject.GetComponents <Component>()) { compornents = gameObject.GetComponents <Component>(); compornentsFlag = true; } //コンポーネントが変更されたときの処理 if (compornentsFlag) { //取得するメソッドの条件 var atr = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.DeclaredOnly; List <MethodInfo> methods = new List <MethodInfo>(); //取得するメソッドリスト //コンポーネントごとに回して格納(transform,Animatorは含まない) for (int i = 1; i < compornents.Length; i++) { if (compornents[i].GetType() == typeof(Animator)) { continue; } Type type = compornents[i].GetType(); //コンポーネントのタイプの取得 methods.AddRange(type.GetMethods(atr)); //メソッドに追加 } //引数のあるメソッドを削除 for (int j = 0; j < methods.Count; j++) { if (methods[j].GetParameters().Length > 0) { methods.Remove(methods[j]); } } //使えるメソッドがあれば名前取得 string[] options = { }; int count = 0; if (methods.Count > 0) { foreach (MethodInfo method in methods) { Array.Resize(ref options, options.Length + 1); options[count] = method.Name; count++; } } Array.Resize(ref options, options.Length + 1); //最後にNoneを追加 options[count] = "None"; nowMethods = methods; nowOptions = options; compornentsFlag = false; //一応 } //メソッド選択 state.stringNumber = EditorGUILayout.Popup(state.stringNumber, nowOptions); //メソッドがあれば if (nowStringNumber != state.stringNumber) { nowStringNumber = state.stringNumber; ////タイプからインスタンスを作成する方法 //var args = new object[] { }; //var bar = Activator.CreateInstance(typeClone, args); //CreateDelegateは第二引数にインスタンス //デリゲートを作成し、変数に入れる if (state.stringNumber >= nowMethods.Count) { for (int i = 0; i < state.playDelegate.GetPersistentEventCount() + 1; i++) { UnityEventTools.RemovePersistentListener(state.playDelegate, i); } break; } Type t = nowMethods[state.stringNumber].DeclaringType; if (state.playDelegate.GetPersistentEventCount() != 0) { for (int i = 0; i < state.playDelegate.GetPersistentEventCount() + 1; i++) { UnityEventTools.RemovePersistentListener(state.playDelegate, i); } } UnityEventTools.AddVoidPersistentListener(state.playDelegate, (UnityAction)nowMethods[state.stringNumber].CreateDelegate(typeof(UnityAction), gameObject.transform.GetComponent(t))); } break; } } else { window.height = 50; } }
// Try to create a link between two nodes. // Returns true if the link is established successfully. public static bool TryLinkNodes( Wiring.NodeBase nodeFrom, UnityEventBase triggerEvent, Wiring.NodeBase nodeTo, MethodInfo targetMethod ) { // Determine the type of the target action. var actionType = GetUnityActionToInvokeMethod(targetMethod); if (actionType == null) { return(false); // invalid target method type } // Create an action that is bound to the target method. var targetAction = Delegate.CreateDelegate( actionType, nodeTo, targetMethod ); if (triggerEvent is UnityEvent) { // The trigger event has no parameter. // Add the action to the event with a default parameter. if (actionType == typeof(UnityAction)) { UnityEventTools.AddVoidPersistentListener( triggerEvent, (UnityAction)targetAction ); return(true); } if (actionType == typeof(UnityAction <float>)) { UnityEventTools.AddFloatPersistentListener( triggerEvent, (UnityAction <float>)targetAction, 1.0f ); return(true); } } else if (triggerEvent is UnityEvent <float> ) { // The trigger event has a float parameter. // Then the target method should have a float parameter too. if (actionType == typeof(UnityAction <float>)) { // Add the action to the event. UnityEventTools.AddPersistentListener( (UnityEvent <float>)triggerEvent, (UnityAction <float>)targetAction ); return(true); } } else if (triggerEvent is UnityEvent <Vector3> ) { // The trigger event has a Vector3 parameter. // Then the target method should have a Vector3 parameter too. if (actionType == typeof(UnityAction <Vector3>)) { // Add the action to the event. UnityEventTools.AddPersistentListener( (UnityEvent <Vector3>)triggerEvent, (UnityAction <Vector3>)targetAction ); return(true); } } else if (triggerEvent is UnityEvent <Quaternion> ) { // The trigger event has a Quaternion parameter. // Then the target method should have a Quaternion parameter too. if (actionType == typeof(UnityAction <Quaternion>)) { // Add the action to the event. UnityEventTools.AddPersistentListener( (UnityEvent <Quaternion>)triggerEvent, (UnityAction <Quaternion>)targetAction ); return(true); } } else if (triggerEvent is UnityEvent <Color> ) { // The trigger event has a color parameter. // Then the target method should have a color parameter too. if (actionType == typeof(UnityAction <Color>)) { // Add the action to the event. UnityEventTools.AddPersistentListener( (UnityEvent <Color>)triggerEvent, (UnityAction <Color>)targetAction ); return(true); } } return(false); // trigger-target mismatch }
// Create a connection between two slots. public static bool ConnectSlots(Graphs.Slot fromSlot, Graphs.Slot toSlot) { EnumTypes(); var nodeTo = ((Block)toSlot.node).runtimeInstance; var triggerEvent = GetEventOfOutputSlot(fromSlot); var targetMethod = GetMethodOfInputSlot(toSlot); // Determine the type of the target action. var actionType = GetUnityActionToInvokeMethod(targetMethod); if (actionType == null) { return(false); // invalid target method type } // Create an action that is bound to the target method. var targetAction = Delegate.CreateDelegate( actionType, nodeTo, targetMethod ); if (triggerEvent is UnityEvent) { // The trigger event has no parameter. // Add the action to the event with a default parameter. if (actionType == typeof(UnityAction)) { UnityEventTools.AddVoidPersistentListener( triggerEvent, (UnityAction)targetAction ); return(true); } if (actionType == typeof(UnityAction <float>)) { UnityEventTools.AddFloatPersistentListener( triggerEvent, (UnityAction <float>)targetAction, 1.0f ); return(true); } } /*else if (triggerEvent is UnityEvent<float>) { * // The trigger event has a float parameter. * // Then the target method should have a float parameter too. * if (actionType == typeof(UnityAction<float>)) { * // Add the action to the event. * UnityEventTools.AddPersistentListener ( * (UnityEvent<float>)triggerEvent, * (UnityAction<float>)targetAction * ); * return true; * } * }*/ else if (triggerEvent is UnityEvent <int> ) { // The trigger event has a int parameter. // Then the target method should have a int parameter too. if (actionType == typeof(UnityAction <int>)) { // Add the action to the event. UnityEventTools.AddPersistentListener( (UnityEvent <int>)triggerEvent, (UnityAction <int>)targetAction ); return(true); } } else if (triggerEvent is UnityEvent <string> ) { // The trigger event has a string parameter. // Then the target method should have a string parameter too. if (actionType == typeof(UnityAction <string>)) { // Add the action to the event. UnityEventTools.AddPersistentListener( (UnityEvent <string>)triggerEvent, (UnityAction <string>)targetAction ); return(true); } } else if (triggerEvent is UnityEvent <Vector3> ) { // The trigger event has a Vector3 parameter. // Then the target method should have a Vector3 parameter too. if (actionType == typeof(UnityAction <Vector3>)) { // Add the action to the event. UnityEventTools.AddPersistentListener( (UnityEvent <Vector3>)triggerEvent, (UnityAction <Vector3>)targetAction ); return(true); } }/* else if (triggerEvent is UnityEvent<Quaternion>) { * // The trigger event has a Quaternion parameter. * // Then the target method should have a Quaternion parameter too. * if (actionType == typeof(UnityAction<Quaternion>)) { * // Add the action to the event. * UnityEventTools.AddPersistentListener ( * (UnityEvent<Quaternion>)triggerEvent, * (UnityAction<Quaternion>)targetAction * ); * return true; * } * }*/ else if (triggerEvent is UnityEvent <Color> ) { // The trigger event has a color parameter. // Then the target method should have a color parameter too. if (actionType == typeof(UnityAction <Color>)) { // Add the action to the event. UnityEventTools.AddPersistentListener( (UnityEvent <Color>)triggerEvent, (UnityAction <Color>)targetAction ); return(true); } } else if (triggerEvent is UnityEvent <Texture> ) { // The trigger event has a color parameter. // Then the target method should have a color parameter too. if (actionType == typeof(UnityAction <Texture>)) { // Add the action to the event. UnityEventTools.AddPersistentListener( (UnityEvent <Texture>)triggerEvent, (UnityAction <Texture>)targetAction ); return(true); } } else { var act = GetActionDataType(actionType); var evt = GetEventDataType(triggerEvent.GetType()); if (act == evt) //same type // UnityEventTools.AddPersistentListener(triggerEvent as UnityEvent,targetAction as UnityAction); { if (_AddPersistenceListener == null) { var mi = typeof(UnityEventTools) .GetMethods(); foreach (var m in mi) { if (m.Name == "AddPersistentListener" && m.IsGenericMethod && m.GetGenericArguments().Length == 1) { _AddPersistenceListener = m; break; } } } var actType = GetActionBaseType(targetAction.GetType()); var evtType = GetEventBaseType(triggerEvent.GetType()); /* object action,trigger; * * MethodInfo castMethod = typeof(ConnectionTools).GetMethod("Cast",BindingFlags.Static | BindingFlags.Public ); * action=castMethod.MakeGenericMethod(actType).Invoke (null, new object[]{ targetAction }); * * trigger=castMethod.MakeGenericMethod(evtType).Invoke (null, new object[]{ triggerEvent });*/ var mv = _AddPersistenceListener.MakeGenericMethod(new[] { evt }); mv.Invoke(null, new object[] { triggerEvent, targetAction }); return(true); } } return(false); // trigger-target mismatch }
void Reset() { UnityEventTools.AddVoidPersistentListener(GetComponent <EventTrigger>().OnTriggerEnterEvent, PlayAnimation); }
public override void OnInspectorGUI() { base.OnInspectorGUI(); if (AudioCollection.Instance == null) { if (AudioCollection.Instance == null) { EditorGUILayout.HelpBox("AudioSFX require AudioCollection. " + "To create AudioCollection, select Resources folder then from Create Menu " + "select RUtilities/Create Audio Collection", MessageType.Error); } return; } if (mScript.mClips.Length > 0) { EditorHelper.BoxVertical(() => { for (int i = 0; i < mScript.mClips.Length; i++) { EditorHelper.BoxHorizontal(() => { EditorHelper.TextField(mScript.mClips[i], ""); if (EditorHelper.ButtonColor("x", Color.red, 24)) { var list = mScript.mClips.ToList(); list.Remove(mScript.mClips[i]); mScript.mClips = list.ToArray(); } }); } }, Color.yellow, true); } EditorHelper.BoxVertical(() => { mSearch = EditorHelper.TextField(mSearch, "Search"); if (!string.IsNullOrEmpty(mSearch)) { var clips = AudioCollection.Instance.sfxClips; if (clips != null && clips.Length > 0) { for (int i = 0; i < clips.Length; i++) { if (clips[i].name.ToLower().Contains(mSearch.ToLower())) { if (GUILayout.Button(clips[i].name)) { var list = mScript.mClips.ToList(); if (!list.Contains(clips[i].name)) { list.Add(clips[i].name); mScript.mClips = list.ToArray(); mSearch = ""; EditorGUI.FocusTextInControl(null); } } } } } else { EditorGUILayout.HelpBox("No results", MessageType.Warning); } } }, Color.white, true); if (EditorHelper.ButtonColor("Open Sounds Collection")) { Selection.activeObject = AudioCollection.Instance; } if (mButton != null) { if (EditorHelper.ButtonColor("Add to OnClick event")) { UnityAction action = new UnityAction(mScript.PlaySFX); UnityEventTools.AddVoidPersistentListener(mButton.onClick, action); } } if (GUI.changed) { EditorUtility.SetDirty(mScript); } }
private void Create() { // Create root gameobject GameObject gameObject = new GameObject("NewMissile"); Selection.activeGameObject = gameObject; // Create 3D model if (missileModel != null) { GameObject meshObject = Instantiate(missileModel, gameObject.transform); meshObject.name = "Model"; meshObject.transform.localPosition = Vector3.zero; meshObject.transform.localRotation = Quaternion.identity; } // Create the visual effects if (exhaustVisualEffects != null) { GameObject exhaustVisualEffectsObject = Instantiate(exhaustVisualEffects, gameObject.transform); exhaustVisualEffectsObject.name = "ExhaustVisualEffects"; exhaustVisualEffectsObject.transform.localPosition = Vector3.zero; exhaustVisualEffectsObject.transform.localRotation = Quaternion.identity; } // ************************ AUDIO *************************** // Create an object to store the audio under GameObject audioObject = new GameObject("Audio"); audioObject.transform.parent = gameObject.transform; audioObject.transform.localPosition = Vector3.zero; audioObject.transform.localRotation = Quaternion.identity; // Create the launch audio if (launchAudioClip != null) { GameObject launchAudioObject = new GameObject("LaunchAudio"); launchAudioObject.transform.parent = audioObject.transform; launchAudioObject.transform.localPosition = Vector3.zero; launchAudioObject.transform.localRotation = Quaternion.identity; AudioSource launchAudioSource = launchAudioObject.AddComponent <AudioSource>(); launchAudioSource.clip = launchAudioClip; launchAudioSource.playOnAwake = true; launchAudioSource.loop = false; } // Create the exhaust audio if (exhaustAudioClip != null) { GameObject exhaustAudioObject = new GameObject("ExhaustAudio"); exhaustAudioObject.transform.parent = audioObject.transform; exhaustAudioObject.transform.localPosition = Vector3.zero; exhaustAudioObject.transform.localRotation = Quaternion.identity; AudioSource exhaustAudioSource = exhaustAudioObject.AddComponent <AudioSource>(); exhaustAudioSource.clip = exhaustAudioClip; exhaustAudioSource.playOnAwake = true; exhaustAudioSource.loop = true; } // ************************ Main Components *************************** // Add a rigidbody Rigidbody rBody = gameObject.AddComponent <Rigidbody>(); rBody.useGravity = false; // Add the Missile component Missile missile = gameObject.AddComponent <Missile>(); SerializedObject missileSO = new SerializedObject(missile); missileSO.Update(); // Add a Target Locker TargetLocker targetLocker = gameObject.AddComponent <TargetLocker>(); SerializedObject targetLockerSO = new SerializedObject(targetLocker); targetLockerSO.Update(); targetLockerSO.FindProperty("lockingEnabled").boolValue = false; targetLockerSO.ApplyModifiedProperties(); // Add a target leader TargetLeader targetLeader = gameObject.AddComponent <TargetLeader>(); SerializedObject targetLeaderSO = new SerializedObject(targetLeader); targetLeaderSO.Update(); // Add engines VehicleEngines3D engines = gameObject.AddComponent <VehicleEngines3D>(); SerializedObject enginesSO = new SerializedObject(engines); enginesSO.Update(); // Add a guidance system GuidanceController guidanceController = gameObject.AddComponent <GuidanceController>(); SerializedObject guidanceControllerSO = new SerializedObject(guidanceController); guidanceControllerSO.Update(); // Update the guidance system settings guidanceControllerSO.FindProperty("engines").objectReferenceValue = engines; guidanceControllerSO.ApplyModifiedProperties(); // Add a Detonator Detonator detonator = gameObject.AddComponent <Detonator>(); SerializedObject detonatorSO = new SerializedObject(detonator); detonatorSO.Update(); if (explosion != null) { detonatorSO.FindProperty("detonatingDuration").floatValue = 2; detonatorSO.FindProperty("detonatingStateSpawnObjects").arraySize = 1; detonatorSO.FindProperty("detonatingStateSpawnObjects").GetArrayElementAtIndex(0).objectReferenceValue = explosion; detonatorSO.ApplyModifiedProperties(); } UnityEventTools.AddBoolPersistentListener(detonator.onDetonating, engines.SetRigidbodyKinematic, true); UnityEventTools.AddBoolPersistentListener(detonator.onDetonated, gameObject.SetActive, false); UnityEventTools.AddBoolPersistentListener(detonator.onReset, engines.SetRigidbodyKinematic, false); // Add Health Modifier HealthModifier healthModifier = gameObject.AddComponent <HealthModifier>(); SerializedObject healthModifierSO = new SerializedObject(healthModifier); healthModifierSO.Update(); if (areaDamage) { // Add a damage receiver scanner for the area damage GameObject areaDamageScannerObject = new GameObject("AreaDamageScanner"); areaDamageScannerObject.transform.parent = gameObject.transform; areaDamageScannerObject.transform.localPosition = Vector3.zero; areaDamageScannerObject.transform.localRotation = Quaternion.identity; // Add a kinematic rigidbody Rigidbody areaDamageScannerRigidbody = areaDamageScannerObject.AddComponent <Rigidbody>(); areaDamageScannerRigidbody.isKinematic = true; // Add a sphere trigger collider and set the radius SphereCollider areaDamageScannerCollider = areaDamageScannerObject.AddComponent <SphereCollider>(); areaDamageScannerCollider.isTrigger = true; areaDamageScannerCollider.radius = 20; // Add a damage receiver scanner DamageReceiverScanner areaDamageScanner = areaDamageScannerObject.AddComponent <DamageReceiverScanner>(); SerializedObject areaDamageScannerSO = new SerializedObject(areaDamageScanner); areaDamageScannerSO.FindProperty("scannerTriggerCollider").objectReferenceValue = areaDamageScannerCollider; areaDamageScannerSO.ApplyModifiedProperties(); healthModifierSO.FindProperty("areaDamageReceiverScanner").objectReferenceValue = areaDamageScanner; healthModifierSO.ApplyModifiedProperties(); } // Add a collision scanner CollisionScanner collisionScanner = gameObject.AddComponent <CollisionScanner>(); SerializedObject collisionScannerSO = new SerializedObject(collisionScanner); collisionScannerSO.Update(); // Collision scanner settings if (areaDamage) { UnityEventTools.AddPersistentListener(collisionScanner.onHitDetected, healthModifier.RaycastHitAreaDamage); } else { UnityEventTools.AddPersistentListener(collisionScanner.onHitDetected, healthModifier.RaycastHitDamage); } UnityEventTools.AddPersistentListener(collisionScanner.onHitDetected, detonator.Detonate); if (targetProximityDetonation) { // Add a target proximity trigger to the root transform TargetProximityTrigger targetProximityTrigger = gameObject.AddComponent <TargetProximityTrigger>(); SerializedObject targetProximityTriggerSO = new SerializedObject(targetProximityTrigger); targetProximityTriggerSO.Update(); // Create an object for the proximity scanner trigger collider GameObject proximityTriggerColliderObject = new GameObject("TargetProximityScanner"); proximityTriggerColliderObject.transform.parent = gameObject.transform; proximityTriggerColliderObject.transform.localPosition = Vector3.zero; proximityTriggerColliderObject.transform.localRotation = Quaternion.identity; // Add a kinematic rigidbody Rigidbody proximityTriggerColliderRigidbody = proximityTriggerColliderObject.AddComponent <Rigidbody>(); proximityTriggerColliderRigidbody.isKinematic = true; // Add a sphere trigger collider and set the radius SphereCollider sphereCollider = proximityTriggerColliderObject.AddComponent <SphereCollider>(); sphereCollider.isTrigger = true; sphereCollider.radius = 20; // Add a damage receiver scanner DamageReceiverScanner damageReceiverScanner = proximityTriggerColliderObject.AddComponent <DamageReceiverScanner>(); SerializedObject damageReceiverScannerSO = new SerializedObject(damageReceiverScanner); damageReceiverScannerSO.Update(); // Link the collider to the damage receiver scanner damageReceiverScannerSO.FindProperty("scannerTriggerCollider").objectReferenceValue = sphereCollider; damageReceiverScannerSO.ApplyModifiedProperties(); damageReceiverScannerSO.Update(); // Link the scanner to the proximity trigger targetProximityTriggerSO.FindProperty("scanner").objectReferenceValue = damageReceiverScanner; targetProximityTriggerSO.ApplyModifiedProperties(); targetProximityTriggerSO.Update(); UnityEventTools.AddPersistentListener(targetProximityTrigger.onTriggered, healthModifier.EmitDamage); UnityEventTools.AddPersistentListener(targetProximityTrigger.onTriggered, detonator.Detonate); UnityEventTools.AddPersistentListener(targetLocker.onLocked, targetProximityTrigger.SetTarget); } // Update the target locker settings UnityEventTools.AddPersistentListener(targetLocker.onLocked, targetLeader.SetTarget); UnityEventTools.AddVoidPersistentListener(targetLocker.onNoLock, targetLeader.ClearTarget); UnityEventTools.AddFloatPersistentListener(targetLocker.onNoLock, detonator.BeginDelayedDetonation, 4); // Update the target leader settings UnityEventTools.AddPersistentListener(targetLeader.onLeadTargetPositionUpdated, guidanceController.SetTargetPosition); missileSO.FindProperty("targetLocker").objectReferenceValue = targetLocker; missileSO.ApplyModifiedProperties(); }
// Create a connection between two slots. public static bool ConnectSlots(Graphs.Slot fromSlot, Graphs.Slot toSlot) { EnumTypes(); var nodeTo = ((Node)toSlot.node).runtimeInstance; var triggerEvent = GetEventOfOutputSlot(fromSlot); var targetMethod = GetMethodOfInputSlot(toSlot); // Determine the type of the target action. var actionType = GetUnityActionToInvokeMethod(targetMethod); if (actionType == null) { return(false); // invalid target method type } // Create an action that is bound to the target method. var targetAction = Delegate.CreateDelegate( actionType, nodeTo, targetMethod ); if (triggerEvent is UnityEvent) { // The trigger event has no parameter. // Add the action to the event with a default parameter. if (actionType == typeof(UnityAction)) { UnityEventTools.AddVoidPersistentListener( triggerEvent, (UnityAction)targetAction ); return(true); } if (actionType == typeof(UnityAction <float>)) { UnityEventTools.AddFloatPersistentListener( triggerEvent, (UnityAction <float>)targetAction, 1.0f ); return(true); } } else if (triggerEvent is UnityEvent <float> ) { // The trigger event has a float parameter. // Then the target method should have a float parameter too. if (actionType == typeof(UnityAction <float>)) { // Add the action to the event. UnityEventTools.AddPersistentListener( (UnityEvent <float>)triggerEvent, (UnityAction <float>)targetAction ); return(true); } } else if (triggerEvent is UnityEvent <int> ) { // The trigger event has a int parameter. // Then the target method should have a int parameter too. if (actionType == typeof(UnityAction <int>)) { // Add the action to the event. UnityEventTools.AddPersistentListener( (UnityEvent <int>)triggerEvent, (UnityAction <int>)targetAction ); return(true); } } else if (triggerEvent is UnityEvent <string> ) { // The trigger event has a string parameter. // Then the target method should have a string parameter too. if (actionType == typeof(UnityAction <string>)) { // Add the action to the event. UnityEventTools.AddPersistentListener( (UnityEvent <string>)triggerEvent, (UnityAction <string>)targetAction ); return(true); } } else if (triggerEvent is UnityEvent <Vector3> ) { // The trigger event has a Vector3 parameter. // Then the target method should have a Vector3 parameter too. if (actionType == typeof(UnityAction <Vector3>)) { // Add the action to the event. UnityEventTools.AddPersistentListener( (UnityEvent <Vector3>)triggerEvent, (UnityAction <Vector3>)targetAction ); return(true); } } else if (triggerEvent is UnityEvent <Quaternion> ) { // The trigger event has a Quaternion parameter. // Then the target method should have a Quaternion parameter too. if (actionType == typeof(UnityAction <Quaternion>)) { // Add the action to the event. UnityEventTools.AddPersistentListener( (UnityEvent <Quaternion>)triggerEvent, (UnityAction <Quaternion>)targetAction ); return(true); } } else if (triggerEvent is UnityEvent <Color> ) { // The trigger event has a color parameter. // Then the target method should have a color parameter too. if (actionType == typeof(UnityAction <Color>)) { // Add the action to the event. UnityEventTools.AddPersistentListener( (UnityEvent <Color>)triggerEvent, (UnityAction <Color>)targetAction ); return(true); } } else if (triggerEvent is UnityEvent <Texture> ) { // The trigger event has a color parameter. // Then the target method should have a color parameter too. if (actionType == typeof(UnityAction <Texture>)) { // Add the action to the event. UnityEventTools.AddPersistentListener( (UnityEvent <Texture>)triggerEvent, (UnityAction <Texture>)targetAction ); return(true); } } return(false); // trigger-target mismatch }
public static void AddPersistentListener(this UnityEventBase self, UnityAction unityAction) { UnityEventTools.AddVoidPersistentListener(self, unityAction); }