-
Notifications
You must be signed in to change notification settings - Fork 1
/
NetworkObjectSpawner.cs
87 lines (74 loc) · 2.81 KB
/
NetworkObjectSpawner.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using MalbersAnimations;
using MalbersAnimations.Reactions;
using NUnit.Framework;
using System;
using System.Collections;
using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace BearTag.NetCode
{
public class NetworkObjectSpawner : NetworkBehaviour
{
public static NetworkObjectSpawner Instance;
[SerializeField] NetworkObject networkObject;
[SerializeField] Transform spawnPoint;
[SerializeField] NetworkObject bearNetworkObject;
[SerializeField] NetworkObject wolfNetworkObject;
private void Awake()
{
Instance = this;
}
/// <summary>
/// Spawns the Network Object at the Spawn Point described in the Inspector
/// </summary>
public void SpawnNetworkObject()
{
SpawnNetworkObject(networkObject, spawnPoint.position, spawnPoint.lossyScale, spawnPoint.rotation);
}
/// <summary>
/// Spawns the Network Object at the Spawn Point provided
/// </summary>
/// <param name="networkObject">the Network Object to be spawned</param>
/// <param name="spawnPoint">the Spawn Point</param>
public void SpawnNetworkObject(NetworkObject networkObject, Vector3 spawnPoint, Vector3 scale = default, Quaternion rotation = default)
{
if (!IsHost)
{
return;
}
var instantiatedNetworkObject = Instantiate(networkObject, spawnPoint, rotation, null);
SceneManager.MoveGameObjectToScene(instantiatedNetworkObject.gameObject,
SceneManager.GetSceneByName(gameObject.scene.name));
instantiatedNetworkObject.transform.localScale = scale;
instantiatedNetworkObject.Spawn(destroyWithScene: true);
}
public void SpawnBearHere(Vector3 position, Vector3 scale = default, Quaternion rotation = default)
{
if (!IsHost)
{
return;
}
SpawnBearHereRpc(position, scale, rotation);
}
[Rpc(SendTo.Server)]
public void SpawnBearHereRpc(Vector3 position, Vector3 scale = default, Quaternion rotation = default)
{
SpawnNetworkObject(bearNetworkObject, position, scale, rotation);
}
public void SpawnWolfHere(Vector3 position, Vector3 scale = default, Quaternion rotation = default)
{
if (!IsHost)
{
return;
}
SpawnWolfHereRpc(position, scale, rotation);
}
[Rpc(SendTo.Server)]
public void SpawnWolfHereRpc(Vector3 position, Vector3 scale = default, Quaternion rotation = default)
{
SpawnNetworkObject(wolfNetworkObject, position, scale, rotation);
}
}
}