Skip to content

Commit 279694f

Browse files
removed editor code + unnecessary netcodehooks
1 parent 891a319 commit 279694f

File tree

2 files changed

+3
-101
lines changed

2 files changed

+3
-101
lines changed

Assets/Prefabs/Game/NetworkObjectSpawner.prefab

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ GameObject:
1010
m_Component:
1111
- component: {fileID: 5747206859721612921}
1212
- component: {fileID: -5484724568297991178}
13-
- component: {fileID: 2681238421323495432}
1413
- component: {fileID: -6110993549598497440}
1514
m_Layer: 0
1615
m_Name: NetworkObjectSpawner
@@ -50,18 +49,6 @@ MonoBehaviour:
5049
AlwaysReplicateAsRoot: 0
5150
DontDestroyWithOwner: 0
5251
AutoObjectParentSync: 1
53-
--- !u!114 &2681238421323495432
54-
MonoBehaviour:
55-
m_ObjectHideFlags: 0
56-
m_CorrespondingSourceObject: {fileID: 0}
57-
m_PrefabInstance: {fileID: 0}
58-
m_PrefabAsset: {fileID: 0}
59-
m_GameObject: {fileID: 721719496969292127}
60-
m_Enabled: 1
61-
m_EditorHideFlags: 0
62-
m_Script: {fileID: 11500000, guid: 6aedfcf74b3f4f248897af16490caa9d, type: 3}
63-
m_Name:
64-
m_EditorClassIdentifier:
6552
--- !u!114 &-6110993549598497440
6653
MonoBehaviour:
6754
m_ObjectHideFlags: 0
@@ -74,6 +61,4 @@ MonoBehaviour:
7461
m_Script: {fileID: 11500000, guid: 40102bedb931e0b4ab8087df94ede8b5, type: 3}
7562
m_Name:
7663
m_EditorClassIdentifier:
77-
m_NetcodeHooks: {fileID: 2681238421323495432}
78-
m_SpawnObjectDataPrefab: {fileID: -8472638279017472308, guid: 95f831895b23a9e429f2b9fe75f9747d, type: 3}
7964
m_SpawnObjectData: []
Lines changed: 3 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4-
using System.Linq;
54
using Unity.Netcode;
6-
using UnityEditor;
7-
using UnityEditor.SceneManagement;
85
using UnityEngine;
96

107
namespace Unity.Multiplayer.Samples.Utilities
@@ -14,36 +11,14 @@ namespace Unity.Multiplayer.Samples.Utilities
1411
/// marked by a special tag, collects their Transform data, destroys their prefab instance, and performs the dynamic
1512
/// spawning of said objects during Netcode for GameObject's (Netcode) OnNetworkSpawn() callback.
1613
/// </summary>
17-
[RequireComponent(typeof(NetcodeHooks))]
18-
public class NetworkObjectSpawner : MonoBehaviour
14+
public class NetworkObjectSpawner : NetworkBehaviour
1915
{
20-
[SerializeField]
21-
NetcodeHooks m_NetcodeHooks;
22-
23-
[SerializeField]
24-
SpawnObjectData m_SpawnObjectDataPrefab;
25-
2616
[SerializeField]
2717
List<SpawnObjectData> m_SpawnObjectData;
2818

29-
const string k_NetworkObjectSpawnerCollectableTag = "NetworkObjectSpawnerCollectable";
30-
31-
void Awake()
32-
{
33-
m_NetcodeHooks.OnNetworkSpawnHook += OnNetworkSpawn;
34-
}
35-
36-
void OnDestroy()
37-
{
38-
if (m_NetcodeHooks)
39-
{
40-
m_NetcodeHooks.OnNetworkSpawnHook -= OnNetworkSpawn;
41-
}
42-
}
43-
44-
void OnNetworkSpawn()
19+
public override void OnNetworkSpawn()
4520
{
46-
if (!NetworkManager.Singleton.IsServer)
21+
if (!IsServer)
4722
{
4823
enabled = false;
4924
return;
@@ -75,63 +50,5 @@ void SpawnNetworkObjects()
7550
Destroy(m_SpawnObjectData[i].gameObject);
7651
}
7752
}
78-
79-
#if UNITY_EDITOR
80-
public void CollectTaggedPrefabInstances()
81-
{
82-
var prefabStage = PrefabStageUtility.GetPrefabStage(gameObject);
83-
84-
var root = prefabStage.prefabContentsRoot;
85-
86-
var networkObjects = root.GetComponentsInChildren<NetworkObject>();
87-
var taggedNetworkObjects = networkObjects.Where(obj => obj.CompareTag(k_NetworkObjectSpawnerCollectableTag));
88-
89-
foreach (var editorOnlyObject in taggedNetworkObjects)
90-
{
91-
var pathToPrefab = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(editorOnlyObject);
92-
var original =
93-
PrefabUtility.GetCorrespondingObjectFromSourceAtPath(editorOnlyObject, pathToPrefab);
94-
95-
var instantiated = PrefabUtility.InstantiatePrefab(m_SpawnObjectDataPrefab.gameObject);
96-
var instantiatedPrefab = instantiated as GameObject;
97-
98-
if (instantiatedPrefab)
99-
{
100-
instantiatedPrefab.transform.SetPositionAndRotation(editorOnlyObject.transform.position,
101-
editorOnlyObject.transform.rotation);
102-
103-
instantiatedPrefab.transform.localScale = editorOnlyObject.transform.lossyScale;
104-
instantiatedPrefab.transform.SetParent(root.gameObject.transform);
105-
106-
var spawnedObjectData = instantiatedPrefab.GetComponent<SpawnObjectData>();
107-
spawnedObjectData.prefabReference = original.gameObject;
108-
instantiatedPrefab.name += $"({original.name})";
109-
110-
m_SpawnObjectData.Add(spawnedObjectData);
111-
112-
// destroy scene prefab instance
113-
DestroyImmediate(editorOnlyObject.gameObject, true);
114-
115-
PrefabUtility.SaveAsPrefabAsset(root, prefabStage.assetPath, out var success);
116-
}
117-
}
118-
}
119-
}
120-
121-
[CustomEditor(typeof(NetworkObjectSpawner))]
122-
public class NetworkObjectSpawnerEditor : Editor
123-
{
124-
public override void OnInspectorGUI()
125-
{
126-
DrawDefaultInspector();
127-
128-
var networkObjectSpawner = (NetworkObjectSpawner)target;
129-
if (PrefabStageUtility.GetCurrentPrefabStage() &&
130-
GUILayout.Button("Collect tagged prefab instances"))
131-
{
132-
networkObjectSpawner.CollectTaggedPrefabInstances();
133-
}
134-
}
13553
}
136-
#endif
13754
}

0 commit comments

Comments
 (0)