Skip to content

Instantly share code, notes, and snippets.

@chadobado
Last active August 5, 2021 22:44
Show Gist options
  • Save chadobado/1fbc40984aceb0df42b126ca021cb711 to your computer and use it in GitHub Desktop.
Save chadobado/1fbc40984aceb0df42b126ca021cb711 to your computer and use it in GitHub Desktop.
Fusion input / networked property testing
//using System;
using System.Collections;
using System.Collections.Generic;
using Fusion;
using Fusion.Sockets;
using UnityEngine;
public struct TestPoseInput : INetworkInput
{
public Vector3 position;
public Quaternion rotation;
public float blendshapeA;
public float blendshapeB;
public float blendshapeC;
public static TestPoseInput GetMockPose()
{
return new TestPoseInput()
{
position = new Vector3(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f)),
rotation = new Quaternion(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f)),
blendshapeA = Random.Range(0f, 1f),
blendshapeB = Random.Range(0f, 1f),
blendshapeC = Random.Range(0f, 1f)
};
}
}
public class TestPlayer : NetworkBehaviour, INetworkRunnerCallbacks
{
#region Local Properties
public Vector3 position;
public Quaternion rotation;
public float blendshapeA;
public float blendshapeB;
public float blendshapeC;
#endregion
#region Networked Properties
[Networked(OnChanged = nameof(OnPoseChanged))] public TestPoseInput m_Pose { get; set; }
#endregion
//0) Player is spawned
public override void Spawned()
{
if(Object.InputAuthority)
Runner.AddCallbacks(this);
}
//1) Fusion requests input from client
public void OnInput(NetworkRunner runner, NetworkInput input)
{
TestPoseInput packet = new TestPoseInput();
packet = TestPoseInput.GetMockPose();
input.Set(packet);
}
//2) Client performs local simulation (just setting m_Pose)
//3) Host receives input, and performs local simulation (just setting m_Pose)
public override void FixedUpdateNetwork()
{
if (GetInput(out TestPoseInput input))
{
m_Pose = input;
}
}
//4) Client receives OnChange, and overwrites with host's state
public static void OnPoseChanged(Changed<TestPlayer> changed)
{
changed.Behaviour.OnPoseChanged();
}
public void OnPoseChanged()
{
position = m_Pose.position;
rotation = m_Pose.rotation;
blendshapeA = m_Pose.blendshapeA;
blendshapeB = m_Pose.blendshapeB;
blendshapeC = m_Pose.blendshapeC;
}
#region Unused fusion callbacks
public void OnConnectedToServer(NetworkRunner runner)
{
}
public void OnConnectFailed(NetworkRunner runner, NetAddress remoteAddress, NetConnectFailedReason reason)
{
}
public void OnConnectRequest(NetworkRunner runner, NetworkRunnerCallbackArgs.ConnectRequest request)
{
}
public void OnDisconnectedFromServer(NetworkRunner runner)
{
}
public void OnInputMissing(NetworkRunner runner, PlayerRef player, NetworkInput input)
{
}
public void OnObjectWordsChanged(NetworkRunner runner, NetworkObject networkedObject, HashSet<int> changedWords, NetworkObjectMemoryPtr oldMemory)
{
}
public void OnPlayerJoined(NetworkRunner runner, PlayerRef player)
{
}
public void OnPlayerLeft(NetworkRunner runner, PlayerRef player)
{
}
public void OnShutdown(NetworkRunner runner, ShutdownReason shutdownReason)
{
}
public void OnUserSimulationMessage(NetworkRunner runner, SimulationMessagePtr message)
{
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment