MirageNet / Mirage

Easy to use high performance Network library for Unity 3d
https://miragenet.github.io/Mirage/
MIT License
507 stars 63 forks source link

fix(ClientObjectManager): add prefab to SpawnHandler when registering #1171

Closed GeekBrony closed 7 months ago

GeekBrony commented 7 months ago

When registering a prefab with the below function, the resulting SpawnHandler has an empty Prefab.

clientObjectManager.RegisterSpawnHandler(prefab, OnClientSpawnCharacter, OnClientUnSpawnCharacter);

With a custom spawn handler, the below function fails even with a registered prefab.

private NetworkIdentity OnClientSpawnCharacter(SpawnMessage msg)
{
    if (!msg.PrefabHash.HasValue)
        return null;

    int prefabHash = msg.PrefabHash.Value;
    SpawnHandler handler = clientObjectManager.GetSpawnHandler(prefabHash);

    NetworkIdentity prefab = handler.Prefab;
    var pos = msg.SpawnValues.Position ?? prefab.transform.position;
    var rot = msg.SpawnValues.Rotation ?? prefab.transform.rotation;

    return Instantiate(prefab, pos, rot);
}

This pull request adds the below constructors for SpawnHandler, which add the respective NetworkIdentity into the SpawnHandler so that the prefab can be found during spawn.

public SpawnHandler(NetworkIdentity prefab, SpawnHandlerDelegate spawnHandler, UnSpawnDelegate unspawnHandler);
public SpawnHandler(NetworkIdentity prefab, SpawnHandlerAsyncDelegate spawnHandlerAsync, UnSpawnDelegate unspawnHandler);
James-Frowen commented 7 months ago

Looks like a good change

github-actions[bot] commented 7 months ago

:tada: This PR is included in version 147.0.3 :tada:

The release is available on GitHub release

Your semantic-release bot :package::rocket: