Removed Server Only scene object from MultipleAdditiveScenes example

This commit is contained in:
MrGadget1024 2020-11-14 02:26:19 -05:00
parent 7ea55ce935
commit d5e162c0d0
6 changed files with 33 additions and 103 deletions

View File

@ -122,86 +122,6 @@ NavMeshSettings:
debug: debug:
m_Flags: 0 m_Flags: 0
m_NavMeshData: {fileID: 0} m_NavMeshData: {fileID: 0}
--- !u!1 &29930032
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 29930035}
- component: {fileID: 29930034}
- component: {fileID: 29930033}
- component: {fileID: 29930036}
m_Layer: 0
m_Name: Spawner
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &29930033
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 29930032}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 9b91ecbcc199f4492b9a91e820070131, type: 3}
m_Name:
m_EditorClassIdentifier:
sceneId: 6047645
serverOnly: 1
m_AssetId:
hasSpawned: 0
--- !u!114 &29930034
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 29930032}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: f522bf510b49da44caa9f3ca0ac17f3b, type: 3}
m_Name:
m_EditorClassIdentifier:
syncMode: 0
syncInterval: 0.1
prizePrefab: {fileID: 114251241889735402, guid: 8cec47ed46e0eff45966a5173d3aa0d3,
type: 3}
--- !u!4 &29930035
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 29930032}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &29930036
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 29930032}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: b7fdb599e1359924bad6255660370252, type: 3}
m_Name:
m_EditorClassIdentifier:
syncMode: 0
syncInterval: 0.1
forceHidden: 0
--- !u!1 &473309615 --- !u!1 &473309615
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -234,7 +154,7 @@ Transform:
- {fileID: 2061474489} - {fileID: 2061474489}
- {fileID: 1072549450} - {fileID: 1072549450}
m_Father: {fileID: 0} m_Father: {fileID: 0}
m_RootOrder: 3 m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1001 &535961555 --- !u!1001 &535961555
PrefabInstance: PrefabInstance:
@ -667,7 +587,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1} m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: [] m_Children: []
m_Father: {fileID: 0} m_Father: {fileID: 0}
m_RootOrder: 2 m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1001 &2061474488 --- !u!1001 &2061474488
PrefabInstance: PrefabInstance:

View File

@ -312,6 +312,8 @@ MonoBehaviour:
playerSpawnMethod: 1 playerSpawnMethod: 1
spawnPrefabs: spawnPrefabs:
- {fileID: 1139254171913846, guid: 8cec47ed46e0eff45966a5173d3aa0d3, type: 3} - {fileID: 1139254171913846, guid: 8cec47ed46e0eff45966a5173d3aa0d3, type: 3}
rewardPrefab: {fileID: 1139254171913846, guid: 8cec47ed46e0eff45966a5173d3aa0d3,
type: 3}
instances: 2 instances: 2
gameScene: Assets/Mirror/Examples/MultipleAdditiveScenes/Scenes/Game.unity gameScene: Assets/Mirror/Examples/MultipleAdditiveScenes/Scenes/Game.unity
--- !u!4 &69965670 --- !u!4 &69965670

View File

@ -8,6 +8,10 @@ namespace Mirror.Examples.MultipleAdditiveScenes
[AddComponentMenu("")] [AddComponentMenu("")]
public class MultiSceneNetManager : NetworkManager public class MultiSceneNetManager : NetworkManager
{ {
[Header("Spawner Setup")]
[Tooltip("Reward Prefab for the Spawner")]
public GameObject rewardPrefab;
[Header("MultiScene Setup")] [Header("MultiScene Setup")]
public int instances = 3; public int instances = 3;
@ -66,6 +70,7 @@ IEnumerator OnServerAddPlayerDelayed(NetworkConnection conn)
/// </summary> /// </summary>
public override void OnStartServer() public override void OnStartServer()
{ {
Spawner.rewardPrefab = rewardPrefab;
StartCoroutine(ServerLoadSubScenes()); StartCoroutine(ServerLoadSubScenes());
} }
@ -77,7 +82,10 @@ IEnumerator ServerLoadSubScenes()
for (int index = 1; index <= instances; index++) for (int index = 1; index <= instances; index++)
{ {
yield return SceneManager.LoadSceneAsync(gameScene, new LoadSceneParameters { loadSceneMode = LoadSceneMode.Additive, localPhysicsMode = LocalPhysicsMode.Physics3D }); yield return SceneManager.LoadSceneAsync(gameScene, new LoadSceneParameters { loadSceneMode = LoadSceneMode.Additive, localPhysicsMode = LocalPhysicsMode.Physics3D });
subScenes.Add(SceneManager.GetSceneAt(index));
Scene newScene = SceneManager.GetSceneAt(index);
subScenes.Add(newScene);
Spawner.InitialSpawn(newScene);
} }
subscenesLoaded = true; subscenesLoaded = true;

View File

@ -8,7 +8,6 @@ public class Reward : NetworkBehaviour
static readonly ILogger logger = LogFactory.GetLogger(typeof(Reward)); static readonly ILogger logger = LogFactory.GetLogger(typeof(Reward));
public bool available = true; public bool available = true;
public Spawner spawner;
public RandomColor randomColor; public RandomColor randomColor;
void OnValidate() void OnValidate()
@ -34,18 +33,18 @@ public void ClaimPrize(GameObject player)
// First hit turns it off, pending the object being destroyed a few frames later. // First hit turns it off, pending the object being destroyed a few frames later.
available = false; available = false;
Color prizeColor = randomColor.color; Color32 color = randomColor.color;
// calculate the points from the color ... lighter scores higher as the average approaches 255 // calculate the points from the color ... lighter scores higher as the average approaches 255
// UnityEngine.Color RGB values are float fractions of 255 // UnityEngine.Color RGB values are float fractions of 255
uint points = (uint)(((prizeColor.r * 255) + (prizeColor.g * 255) + (prizeColor.b * 255)) / 3); uint points = (uint)(((color.r) + (color.g) + (color.b)) / 3);
if (logger.LogEnabled()) logger.LogFormat(LogType.Log, "Scored {0} points R:{1} G:{2} B:{3}", points, prizeColor.r, prizeColor.g, prizeColor.b); if (logger.LogEnabled()) logger.LogFormat(LogType.Log, "Scored {0} points R:{1} G:{2} B:{3}", points, color.r, color.g, color.b);
// award the points via SyncVar on the PlayerController // award the points via SyncVar on the PlayerController
player.GetComponent<PlayerScore>().score += points; player.GetComponent<PlayerScore>().score += points;
// spawn a replacement // spawn a replacement
spawner.SpawnPrize(); Spawner.SpawnReward(gameObject.scene);
// destroy this one // destroy this one
NetworkServer.Destroy(gameObject); NetworkServer.Destroy(gameObject);

View File

@ -1,27 +1,30 @@
using UnityEngine; using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.Serialization;
namespace Mirror.Examples.MultipleAdditiveScenes namespace Mirror.Examples.MultipleAdditiveScenes
{ {
public class Spawner : NetworkBehaviour internal class Spawner : MonoBehaviour
{ {
public NetworkIdentity prizePrefab; [FormerlySerializedAs("prizePrefab")]
internal static GameObject rewardPrefab;
public override void OnStartServer() internal static void InitialSpawn(Scene scene)
{ {
if (!NetworkServer.active) return;
for (int i = 0; i < 10; i++) for (int i = 0; i < 10; i++)
SpawnPrize(); SpawnReward(scene);
} }
public void SpawnPrize() internal static void SpawnReward(Scene scene)
{ {
if (!NetworkServer.active) return;
Vector3 spawnPosition = new Vector3(Random.Range(-19, 20), 1, Random.Range(-19, 20)); Vector3 spawnPosition = new Vector3(Random.Range(-19, 20), 1, Random.Range(-19, 20));
GameObject reward = Instantiate(rewardPrefab, spawnPosition, Quaternion.identity);
// spawn as child of the spawner that's already in the additive scene at 0,0,0 so we don't have to move it SceneManager.MoveGameObjectToScene(reward, scene);
GameObject newPrize = Instantiate(prizePrefab.gameObject, spawnPosition, Quaternion.identity, transform); NetworkServer.Spawn(reward);
Reward reward = newPrize.gameObject.GetComponent<Reward>();
reward.spawner = this;
NetworkServer.Spawn(newPrize);
} }
} }
} }

View File

@ -8,8 +8,6 @@ public class Reward : NetworkBehaviour
static readonly ILogger logger = LogFactory.GetLogger(typeof(Reward)); static readonly ILogger logger = LogFactory.GetLogger(typeof(Reward));
public bool available = true; public bool available = true;
uint points;
public RandomColor randomColor; public RandomColor randomColor;
void OnValidate() void OnValidate()
@ -41,7 +39,7 @@ public void ClaimPrize(GameObject player)
// calculate the points from the color ... lighter scores higher as the average approaches 255 // calculate the points from the color ... lighter scores higher as the average approaches 255
// UnityEngine.Color RGB values are float fractions of 255 // UnityEngine.Color RGB values are float fractions of 255
points = (uint)(((color.r) + (color.g) + (color.b)) / 3); uint points = (uint)(((color.r) + (color.g) + (color.b)) / 3);
if (logger.LogEnabled()) logger.LogFormat(LogType.Log, "Scored {0} points R:{1} G:{2} B:{3}", points, color.r, color.g, color.b); if (logger.LogEnabled()) logger.LogFormat(LogType.Log, "Scored {0} points R:{1} G:{2} B:{3}", points, color.r, color.g, color.b);
// award the points via SyncVar on the PlayerController // award the points via SyncVar on the PlayerController