# EditorConnection

> Handles the connection from the Editor to the Player.

## Definition

* **Type:** Class
* **Namespace:** [UnityEditor.Networking.PlayerConnection](/engine/6000.5/script-reference/unityeditor/networking/playerconnection.md)
* **Assembly:** UnityEditor.CoreModule
* **Inherits from:** [ScriptableSingleton\<EditorConnection>](/engine/6000.5/script-reference/unityeditor/scriptablesingleton1.md)

```csharp
public class EditorConnection : ScriptableSingleton<EditorConnection>
```

## Remarks

Sets up events for connecting to and sending data to the Player.

This is a singleton class and can be accessed using instance.

This can only be used in a class that inherits from MonoBehaviour, Object or ScriptableObject.

Set the "Autoconnect Profiler" in the Build Settings or build the Player through code with the BuildPipeline using build options: [BuildOptions.ConnectToHost](/engine/6000.5/script-reference/unityeditor/buildoptions/connecttohost.md) and [BuildOptions.Development](/engine/6000.5/script-reference/unityeditor/buildoptions/development.md) to initialize the connection.

The Player ID is used to tell multiple connected Players apart. By default, data is sent to all Players. A connected Player's ID is not guaranteed to be the same the next time it connects.

## Examples

```csharp
using System;
using UnityEngine;
using UnityEditor;
using System.Text;
using UnityEditor.Networking.PlayerConnection;
using UnityEngine.Networking.PlayerConnection;

public class EditorConnectionExample : EditorWindow
{
    public static readonly Guid kMsgSendEditorToPlayer = new Guid("EXAMPLEGUID");
    public static readonly Guid kMsgSendPlayerToEditor = new Guid("EXAMPLEGUID");

    [MenuItem("Test/EditorConnectionExample")]
    static void Init()
    {
        EditorConnectionExample window = (EditorConnectionExample)EditorWindow.GetWindow(typeof(EditorConnectionExample));
        window.Show();
        window.titleContent = new GUIContent("EditorConnectionExample");
    }

    void OnEnable()
    {
        EditorConnection.instance.Initialize();
        EditorConnection.instance.Register(kMsgSendPlayerToEditor, OnMessageEvent);
    }

    void OnDisable()
    {
        EditorConnection.instance.Unregister(kMsgSendPlayerToEditor, OnMessageEvent);
        EditorConnection.instance.DisconnectAll();
    }

    private void OnMessageEvent(MessageEventArgs args)
    {
        var text = Encoding.ASCII.GetString(args.data);
        Debug.Log("Message from player: " + text);
    }

    void OnGUI()
    {
        var playerCount = EditorConnection.instance.ConnectedPlayers.Count;
        StringBuilder builder = new StringBuilder();
        builder.AppendLine(string.Format("{0} players connected.", playerCount));
        int i = 0;
        foreach (var p in EditorConnection.instance.ConnectedPlayers)
        {
            builder.AppendLine(string.Format("[{0}] - {1} {2}", i++, p.name, p.playerId));
        }
        EditorGUILayout.HelpBox(builder.ToString(), MessageType.Info);

        if (GUILayout.Button("Send message to player"))
        {
            EditorConnection.instance.Send(kMsgSendEditorToPlayer, Encoding.ASCII.GetBytes("Hello from Editor"));
        }
    }
}
```

## Properties

| Property                                                                                                                         | Description                      |
| -------------------------------------------------------------------------------------------------------------------------------- | -------------------------------- |
| [ConnectedPlayers](/engine/6000.5/script-reference/unityeditor/networking/playerconnection/editorconnection/connectedplayers.md) | A list of the connected Players. |

## Methods

| Method                                                                                                                                         | Description                                                                   |
| ---------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------- |
| [DisconnectAll](/engine/6000.5/script-reference/unityeditor/networking/playerconnection/editorconnection/disconnectall.md)                     | Disconnects all of the active connections between the Editor and the Players. |
| [Initialize](/engine/6000.5/script-reference/unityeditor/networking/playerconnection/editorconnection/initialize.md)                           | Initializes the EditorConnection.                                             |
| [Register](/engine/6000.5/script-reference/unityeditor/networking/playerconnection/editorconnection/register.md)                               | Registers a callback on a certain message ID.                                 |
| [RegisterConnection](/engine/6000.5/script-reference/unityeditor/networking/playerconnection/editorconnection/registerconnection.md)           | Registers a callback, executed when a new Player connects to the Editor.      |
| [RegisterDisconnection](/engine/6000.5/script-reference/unityeditor/networking/playerconnection/editorconnection/registerdisconnection.md)     | Registers a callback on a Player when that Player disconnects.                |
| [Send](/engine/6000.5/script-reference/unityeditor/networking/playerconnection/editorconnection/send.md)                                       | Sends data to the connected Players.                                          |
| [TrySend](/engine/6000.5/script-reference/unityeditor/networking/playerconnection/editorconnection/trysend.md)                                 | Attempts to send data from the Editor to the connected Players.               |
| [Unregister](/engine/6000.5/script-reference/unityeditor/networking/playerconnection/editorconnection/unregister.md)                           | Deregisters a registered callback.                                            |
| [UnregisterConnection](/engine/6000.5/script-reference/unityeditor/networking/playerconnection/editorconnection/unregisterconnection.md)       | Unregisters the connection callback.                                          |
| [UnregisterDisconnection](/engine/6000.5/script-reference/unityeditor/networking/playerconnection/editorconnection/unregisterdisconnection.md) | Unregisters the disconnection callback.                                       |

## Inheritance

**Inherited Members:**

* [ScriptableSingleton\<EditorConnection>.Save(bool)](/engine/6000.5/script-reference/unityeditor/scriptablesingleton1/save.md)
* [ScriptableSingleton\<EditorConnection>.GetFilePath()](/engine/6000.5/script-reference/unityeditor/scriptablesingleton1/getfilepath.md)
* [ScriptableSingleton\<EditorConnection>.instance](/engine/6000.5/script-reference/unityeditor/scriptablesingleton1/instance.md)
* [ScriptableObject.CreateInstance(string)](/engine/6000.5/script-reference/unityengine/scriptableobject/createinstance.md#createinstance\(string\))
* [ScriptableObject.CreateInstance(Type)](/engine/6000.5/script-reference/unityengine/scriptableobject/createinstance.md#createinstance\(type\))
* [ScriptableObject.CreateInstance\<T>()](/engine/6000.5/script-reference/unityengine/scriptableobject/createinstance.md)
* [Object.GetEntityId()](/engine/6000.5/script-reference/unityengine/object/getentityid.md)
* [Object.GetHashCode()](/engine/6000.5/script-reference/unityengine/object/gethashcode.md)
* [Object.InstantiateAsync\<T>(T)](/engine/6000.5/script-reference/unityengine/object/instantiateasync.md)
* [Object.InstantiateAsync\<T>(T, Transform)](/engine/6000.5/script-reference/unityengine/object/instantiateasync.md)
* [Object.InstantiateAsync\<T>(T, Vector3, Quaternion)](/engine/6000.5/script-reference/unityengine/object/instantiateasync.md)
* [Object.InstantiateAsync\<T>(T, Transform, Vector3, Quaternion)](/engine/6000.5/script-reference/unityengine/object/instantiateasync.md)
* [Object.InstantiateAsync\<T>(T, int)](/engine/6000.5/script-reference/unityengine/object/instantiateasync.md)
* [Object.InstantiateAsync\<T>(T, int, Transform)](/engine/6000.5/script-reference/unityengine/object/instantiateasync.md)
* [Object.InstantiateAsync\<T>(T, int, Vector3, Quaternion)](/engine/6000.5/script-reference/unityengine/object/instantiateasync.md)
* [Object.InstantiateAsync\<T>(T, int, ReadOnlySpan\<Vector3>, ReadOnlySpan\<Quaternion>)](/engine/6000.5/script-reference/unityengine/object/instantiateasync.md)
* [Object.InstantiateAsync\<T>(T, int, Transform, Vector3, Quaternion)](/engine/6000.5/script-reference/unityengine/object/instantiateasync.md)
* [Object.InstantiateAsync\<T>(T, int, Transform, Vector3, Quaternion, CancellationToken)](/engine/6000.5/script-reference/unityengine/object/instantiateasync.md)
* [Object.InstantiateAsync\<T>(T, int, Transform, ReadOnlySpan\<Vector3>, ReadOnlySpan\<Quaternion>)](/engine/6000.5/script-reference/unityengine/object/instantiateasync.md)
* [Object.InstantiateAsync\<T>(T, int, Transform, ReadOnlySpan\<Vector3>, ReadOnlySpan\<Quaternion>, CancellationToken)](/engine/6000.5/script-reference/unityengine/object/instantiateasync.md)
* [Object.InstantiateAsync\<T>(T, InstantiateParameters, CancellationToken)](/engine/6000.5/script-reference/unityengine/object/instantiateasync.md)
* [Object.InstantiateAsync\<T>(T, int, InstantiateParameters, CancellationToken)](/engine/6000.5/script-reference/unityengine/object/instantiateasync.md)
* [Object.InstantiateAsync\<T>(T, Vector3, Quaternion, InstantiateParameters, CancellationToken)](/engine/6000.5/script-reference/unityengine/object/instantiateasync.md)
* [Object.InstantiateAsync\<T>(T, int, Vector3, Quaternion, InstantiateParameters, CancellationToken)](/engine/6000.5/script-reference/unityengine/object/instantiateasync.md)
* [Object.InstantiateAsync\<T>(T, int, ReadOnlySpan\<Vector3>, ReadOnlySpan\<Quaternion>, InstantiateParameters, CancellationToken)](/engine/6000.5/script-reference/unityengine/object/instantiateasync.md)
* [Object.Instantiate(Object, Vector3, Quaternion)](/engine/6000.5/script-reference/unityengine/object/instantiate.md#instantiate\(object-vector3-quaternion\))
* [Object.Instantiate(Object, Vector3, Quaternion, Transform)](/engine/6000.5/script-reference/unityengine/object/instantiate.md#instantiate\(object-vector3-quaternion-transform\))
* [Object.Instantiate(Object)](/engine/6000.5/script-reference/unityengine/object/instantiate.md#instantiate\(object\))
* [Object.Instantiate(Object, Scene)](/engine/6000.5/script-reference/unityengine/object/instantiate.md#instantiate\(object-scene\))
* [Object.Instantiate\<T>(T, InstantiateParameters)](/engine/6000.5/script-reference/unityengine/object/instantiate.md)
* [Object.Instantiate\<T>(T, Vector3, Quaternion, InstantiateParameters)](/engine/6000.5/script-reference/unityengine/object/instantiate.md)
* [Object.Instantiate(Object, Transform)](/engine/6000.5/script-reference/unityengine/object/instantiate.md#instantiate\(object-transform\))
* [Object.Instantiate(Object, Transform, bool)](/engine/6000.5/script-reference/unityengine/object/instantiate.md#instantiate\(object-transform-bool\))
* [Object.Instantiate\<T>(T)](/engine/6000.5/script-reference/unityengine/object/instantiate.md)
* [Object.Instantiate\<T>(T, Vector3, Quaternion)](/engine/6000.5/script-reference/unityengine/object/instantiate.md)
* [Object.Instantiate\<T>(T, Vector3, Quaternion, Transform)](/engine/6000.5/script-reference/unityengine/object/instantiate.md)
* [Object.Instantiate\<T>(T, Transform)](/engine/6000.5/script-reference/unityengine/object/instantiate.md)
* [Object.Instantiate\<T>(T, Transform, bool)](/engine/6000.5/script-reference/unityengine/object/instantiate.md)
* [Object.Destroy(Object, float)](/engine/6000.5/script-reference/unityengine/object/destroy.md)
* [Object.DestroyImmediate(Object, bool)](/engine/6000.5/script-reference/unityengine/object/destroyimmediate.md)
* [Object.FindObjectsByType(Type)](/engine/6000.5/script-reference/unityengine/object/findobjectsbytype.md#findobjectsbytype\(type\))
* [Object.FindObjectsByType(Type, FindObjectsInactive)](/engine/6000.5/script-reference/unityengine/object/findobjectsbytype.md#findobjectsbytype\(type-findobjectsinactive\))
* [Object.DontDestroyOnLoad(Object)](/engine/6000.5/script-reference/unityengine/object/dontdestroyonload.md)
* [Object.FindAnyObjectByType\<T>()](/engine/6000.5/script-reference/unityengine/object/findanyobjectbytype.md#findanyobjectbytypet\(\))
* [Object.FindAnyObjectByType\<T>(FindObjectsInactive)](/engine/6000.5/script-reference/unityengine/object/findanyobjectbytype.md#findanyobjectbytypet\(findobjectsinactive\))
* [Object.FindObjectsByType\<T>()](/engine/6000.5/script-reference/unityengine/object/findobjectsbytype.md#findobjectsbytypet\(\))
* [Object.FindObjectsByType\<T>(FindObjectsInactive)](/engine/6000.5/script-reference/unityengine/object/findobjectsbytype.md#findobjectsbytypet\(findobjectsinactive\))
* [Object.FindAnyObjectByType(Type)](/engine/6000.5/script-reference/unityengine/object/findanyobjectbytype.md#findanyobjectbytype\(type\))
* [Object.FindAnyObjectByType(Type, FindObjectsInactive)](/engine/6000.5/script-reference/unityengine/object/findanyobjectbytype.md#findanyobjectbytype\(type-findobjectsinactive\))
* [Object.ToString()](/engine/6000.5/script-reference/unityengine/object/tostring.md)
* [Object.name](/engine/6000.5/script-reference/unityengine/object/name.md)
* [Object.hideFlags](/engine/6000.5/script-reference/unityengine/object/hideflags.md)

### Operators

| Operator                                                                           | Description                                                             |
| ---------------------------------------------------------------------------------- | ----------------------------------------------------------------------- |
| [operator ==](/engine/6000.5/script-reference/unityengine/object/op-equality.md)   | Compares two object references to see if they refer to the same object. |
| [bool](/engine/6000.5/script-reference/unityengine/object/op-implicit.md)          | Determines whether the object exists.                                   |
| [operator !=](/engine/6000.5/script-reference/unityengine/object/op-inequality.md) | Compares if two objects refer to a different object.                    |
