Skip to content

Instantly share code, notes, and snippets.

View James-Frowen's full-sized avatar

James Frowen James-Frowen

View GitHub Profile
@James-Frowen
James-Frowen / EchoSocket.cs
Created October 17, 2021 16:08
Behaviour that makes Sockets echo back message. For Mirage networking
using System.Text;
using UnityEngine;
namespace Mirage.SocketLayer
{
/// <summary>
/// Monobehaviour to help debug a cloud server, can help check if ports are open and a socket is sending message correctly
/// <para>Server will echo back the message that was sent to it</para>
/// <para>Client will send 4 byte message every second, incrementing by 1 each time</para>
/// </summary>
using System.Collections.Generic;
using UnityEngine;
namespace Mirror
{
/// <summary>
/// Component that controls visibility of networked objects between scenes.
/// <para>Any object with this component on it will only be visible to other objects in the same scene</para>
/// <para>This would be used when the server has multiple additive subscenes loaded to isolate players to their respective subscenes</para>
/// <para>Does not actively check if scene has changed</para>
@James-Frowen
James-Frowen / NetworkManagerGUI.cs
Created March 16, 2021 21:01
OnGui for NetworkManager to make iit quick to set up and start a server
using Cysharp.Threading.Tasks;
using UnityEngine;
namespace Mirage
{
/// <summary>
/// An extension for the NetworkManager that displays a default HUD for controlling the network state of the game.
/// <para>This component also shows useful internal state for the networking system in the inspector window of the editor. It allows users to view connections, networked objects, message handlers, and packet statistics. This information can be helpful when debugging networked games.</para>
/// </summary>
[DisallowMultipleComponent]
@James-Frowen
James-Frowen / ExitPlayModeOnCompile.cs
Created February 15, 2021 21:21
Exits play more when scripts are recompiled
using UnityEditor;
using UnityEditor.Compilation;
using UnityEngine;
namespace JamesFrowen.EditorScripts.Compilation
{
/// <summary>
/// Stop the game the scripts are recomplied
/// </summary>
[InitializeOnLoad]
using System;
#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;
#endif
namespace JamesFrowen.PositionSync
{
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
public class BackingFieldNameAttribute : PropertyAttribute
@James-Frowen
James-Frowen / Example.cs
Created December 12, 2020 22:01
Override type read/write functions per field/parameter
public class PackedIntSerializer : IMirrorSerializer<int>
{
public int Read(NetworkReader reader)
{
// unpack
}
public void Write(NetworkWriter writer, int value)
{
// pack
@James-Frowen
James-Frowen / PositionCompression.cs
Last active October 1, 2022 16:30
Write/Read Compressed Vector3 for mirror
public class PositionCompression
{
public struct CompressFloat
{
public readonly float minFloat;
public readonly float maxFloat;
public readonly uint minUint;
public readonly uint maxUint;
@James-Frowen
James-Frowen / PlayerReference.cs
Last active October 29, 2020 03:23
Reference to NetworkBehaviour that is not currently spawned on client (useful for synclists)
// player script (in its own file)
public class Player : NetworkBehaviour { }
// reference to Player that can be sent over network
public struct PlayerReference
{
public uint netid;
private Player _player;
public PlayerReference(Player value) : this()

Use scriptable object to refernce object. This doesn't require the object to have spawned, you can just listen for the OnValueChange (and check if value is already set)

Reference

[CreateAssetMenu(fileName = "NetworkIdentityReference", menuName = "Component Reference/NetworkIdentity")]
public class NetworkIdentityReference : ScriptableObject
{
    [SerializeField] NetworkIdentity _value;

    public event Action<NetworkIdentity> OnValueChange;
@James-Frowen
James-Frowen / Example.md
Last active October 10, 2020 22:04
Used to sync a referemce to a NetworkIdentity without requiring the object to be spawned when the reference was sent

NetworkIdentityReference can be used for synclists

public class MyBehaviour : NetworkBehaviour
{
    public SyncList<NetworkIdentityReference> others = new SyncList<NetworkIdentityReference>();

    public override void OnStartClient()
    {
 foreach (NetworkIdentity other in others)