Skip to content

Instantly share code, notes, and snippets.

@James-Frowen
Last active April 12, 2022 15:14
Show Gist options
  • Save James-Frowen/22a56deb3e70f54d31952ae9a3d18a0e to your computer and use it in GitHub Desktop.
Save James-Frowen/22a56deb3e70f54d31952ae9a3d18a0e to your computer and use it in GitHub Desktop.
Helper to create network objects at runtime
/*
MIT License
Copyright (c) 2021 James Frowen
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
using Mirage;
using System;
using UnityEngine;
namespace JamesFrowen.Networking
{
/// <summary>
/// Creates a new network object using its type has prefabHash
/// </summary>
/// <typeparam name="TComp"></typeparam>
public static class NetworkObjectCreater<TComp> where TComp : NetworkBehaviour
{
internal static readonly int PrefabHash = typeof(TComp).FullName.GetStableHashCode();
internal static readonly string Name = typeof(TComp).Name;
public static TComp ServerCreate(ServerObjectManager objectManager, INetworkPlayer owner, Action<TComp> preSpawn = null) => ServerCreate(objectManager, owner, objectManager.transform, preSpawn);
public static TComp ServerCreate(ServerObjectManager objectManager, INetworkPlayer owner, Transform parent, Action<TComp> preSpawn = null)
{
var go = new GameObject(Name, typeof(NetworkIdentity), typeof(TComp));
go.transform.parent = parent;
var identity = go.GetComponent<NetworkIdentity>();
identity.SpawnSettings = new NetworkIdentity.TransformSpawnSettings(false, false, false);
var comp = go.GetComponent<TComp>();
preSpawn?.Invoke(comp);
objectManager.Spawn(go, PrefabHash, owner);
return comp;
}
/// <summary>
///
/// </summary>
/// <param name="objectManager"></param>
/// <param name="onStartClient">Callback to add to OnStartClient event (called after object is spawned)</param>
public static void RegisterSpawnHandler(ClientObjectManager objectManager, Action<TComp> onStartClient = null)
{
objectManager.RegisterSpawnHandler(PrefabHash, ClientSpawn(objectManager.transform, onStartClient), null);
}
private static SpawnHandlerDelegate ClientSpawn(Transform parent, Action<TComp> onStartClient)
{
return (SpawnMessage msg) =>
{
var go = new GameObject(Name, typeof(NetworkIdentity), typeof(TComp));
go.transform.parent = parent;
var identity = go.GetComponent<NetworkIdentity>();
var comp = go.GetComponent<TComp>();
if (onStartClient != null)
identity.OnStartClient.AddListener(() => onStartClient.Invoke(comp));
return identity;
};
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment