mirror of
https://github.com/Kaveinator/NetickProForUnity.git
synced 2025-10-26 09:49:06 -07:00
Added PlayerSpawner, a helper script for quick prototyping, used to spawn/despawn a player prefab when a player (client or host) has connected/disconnected.
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
37
Scripts/PlayerSpawner.cs
Normal file
37
Scripts/PlayerSpawner.cs
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
using Netick;
|
||||||
|
using Netick.Unity;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// This is a helper script for quick prototyping, used to spawn/despawn a player prefab when a player (client or host) has connected/disconnected.
|
||||||
|
/// </summary>
|
||||||
|
[AddComponentMenu("Netick/Player Spawner")]
|
||||||
|
public class PlayerSpawner : NetworkEventsListener
|
||||||
|
{
|
||||||
|
public GameObject PlayerPrefab;
|
||||||
|
public Transform SpawnPosition;
|
||||||
|
public float HorizontalOffset = 5f;
|
||||||
|
public bool StaggerSpawns = true;
|
||||||
|
public bool DestroyPlayerObjectWhenLeaving = true;
|
||||||
|
|
||||||
|
// This is called on the server when a player has connected.
|
||||||
|
public override void OnPlayerConnected(NetworkSandbox sandbox, Netick.NetworkPlayer client)
|
||||||
|
{
|
||||||
|
var spawnPos = SpawnPosition.position;
|
||||||
|
if (StaggerSpawns)
|
||||||
|
spawnPos += (HorizontalOffset * Vector3.left) * (1 + sandbox.ConnectedPlayers.Count);
|
||||||
|
var player = sandbox.NetworkInstantiate(PlayerPrefab, spawnPos, SpawnPosition.rotation, client);
|
||||||
|
client.PlayerObject = player;
|
||||||
|
}
|
||||||
|
|
||||||
|
// This is called on the server when a player has disconnected.
|
||||||
|
public override void OnPlayerDisconnected(NetworkSandbox sandbox, Netick.NetworkPlayer client, TransportDisconnectReason transportDisconnectReason)
|
||||||
|
{
|
||||||
|
if (!DestroyPlayerObjectWhenLeaving)
|
||||||
|
return;
|
||||||
|
|
||||||
|
var netObj = client.PlayerObject as NetworkObject;
|
||||||
|
if (netObj != null)
|
||||||
|
Sandbox.Destroy(netObj);
|
||||||
|
}
|
||||||
|
}
|
||||||
11
Scripts/PlayerSpawner.cs.meta
Normal file
11
Scripts/PlayerSpawner.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 13d95c28de1bac54f8b26fc8dd960077
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "com.karrar.netick",
|
"name": "com.karrar.netick",
|
||||||
"version": "0.13.15",
|
"version": "0.13.16",
|
||||||
"displayName": "Netick",
|
"displayName": "Netick",
|
||||||
"description": "A networking solution for Unity",
|
"description": "A networking solution for Unity",
|
||||||
"unity": "2021.3",
|
"unity": "2021.3",
|
||||||
|
|||||||
Reference in New Issue
Block a user