I want to run the inside of the lambda expression linked when one method is called (notify another method)

Asked 1 years ago, Updated 1 years ago, 33 views

■Current state
 Using a multi-online system called PhotonCloud,
 I'm trying to make a multi-match.
 
 OnConnectedToPhoton on PhotonEventManager.cs is
 When connected to the PhotonServer, events fly.
 I'd like to create a system in which a certain process is called when an event comes flying.
 I'm worried about not being called.
 We have confirmed that the OnConnectedToPhoton method is called.
 I think I can do basic things like events and delivery, but I haven't done it yet.

■What you want to do (C#3.0)
 I'm not particular about the lambda style of the title.
 It's like notifying another method that one method has been called.  Your intention is to

■ Source and Description
 Below is OnConnectedToPhoton on PhotonEventManager.cs and
 
so that the inside of the lambda formula is called within the SetManager method on ConnectionPhase.cs  I think I'm linking it, but the OnConnectedToPhoton method is called
 The inside of the lambda type is not called.

●PhotonCloud.cs

using UnityEngine;
using System;

public class PhotoEventManager: Photo.MonoBehavior {

    /// <summary>
    /// Called upon successful connection.
    /// </summary>
    public void OnConnectedToPhoton()
    {
        Debug.Log("OnConnectedToPhoton");
    }

    // Call when disconnected
    public void OnDisconnectedFromPhoton()
    {
        Debug.Log("Disconnected from Photon.");
    }
}

●ConnectionPhase.cs

using System;

public class ConnectionPhase
{

    public void SetManager (PhotonEventManager manager)
    {

        manager.OnConnectedPhoton=()=> 
        {
            UnityEngine.Debug.Log("Success");
        };
    }
}

■Environment
Unity 5.5.1 f1
C#3.0
VisualStudio 2015 Community

c# unity3d

2022-09-30 11:55

1 Answers

We interpreted that OnConnectedToPhoton() should be able to register outside of PhotonEventManager.

PhotonEventManagerin the class

public delete void Action(); /////// Add if you get in trouble if the action below is undefined (can be written out of class)

public event Action PhotoConnected; ////// Add

/// <summary>
/// Called upon successful connection.
/// </summary>
public void OnConnectedToPhoton()
{
    Debug.Log("OnConnectedToPhoton");

    //////// Additional hereinafter
    Action = PhotonConnected;
    if(action!=null)
        action();
}

Add the event definition and the code to call its handler as shown in .

Registering side is

manager.PhotonConnected+=()=>
    {
        UnityEngine.Debug.Log("Success");
    };

Register the event handler as shown in .


2022-09-30 11:55

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.