Remove ClientScene.RegisterPrefab with assetId, Spawn/UnspawnDelegate

This commit is contained in:
vis2k 2020-09-29 09:43:34 +02:00
parent c54be229a8
commit 6b2cd3aefa

View File

@ -288,111 +288,6 @@ public static void RegisterPrefab(GameObject prefab)
RegisterPrefabIdentity(identity);
}
/// <summary>
/// Registers a prefab with the spawning system.
/// <para>When a NetworkIdentity object is spawned on a server with NetworkServer.SpawnObject(), and the prefab that the object was created from was registered with RegisterPrefab(), the client will use that prefab to instantiate a corresponding client object with the same netId.</para>
/// <para>The NetworkManager has a list of spawnable prefabs, it uses this function to register those prefabs with the ClientScene.</para>
/// <para>The set of current spawnable object is available in the ClientScene static member variable ClientScene.prefabs, which is a dictionary of NetworkAssetIds and prefab references.</para>
/// <para>NOTE: newAssetId can not be set on GameObjects that already have an assetId</para>
/// </summary>
/// <param name="prefab">A GameObject that will be spawned.</param>
/// <param name="newAssetId">An assetId to be assigned to this GameObject. This allows a dynamically created game object to be registered for an already known asset Id.</param>
/// <param name="spawnHandler">A method to use as a custom spawnhandler on clients.</param>
/// <param name="unspawnHandler">A method to use as a custom un-spawnhandler on clients.</param>
public static void RegisterPrefab(GameObject prefab, Guid newAssetId, SpawnDelegate spawnHandler, UnSpawnDelegate unspawnHandler)
{
// We need this check here because we don't want a null handler in the lambda expression below
if (spawnHandler == null)
{
Debug.LogError($"Can not Register null SpawnHandler for {newAssetId}");
return;
}
RegisterPrefab(prefab, newAssetId, msg => spawnHandler(msg.position, msg.assetId), unspawnHandler);
}
/// <summary>
/// Registers a prefab with the spawning system.
/// <para>When a NetworkIdentity object is spawned on a server with NetworkServer.SpawnObject(), and the prefab that the object was created from was registered with RegisterPrefab(), the client will use that prefab to instantiate a corresponding client object with the same netId.</para>
/// <para>The NetworkManager has a list of spawnable prefabs, it uses this function to register those prefabs with the ClientScene.</para>
/// <para>The set of current spawnable object is available in the ClientScene static member variable ClientScene.prefabs, which is a dictionary of NetworkAssetIds and prefab references.</para>
/// <para>NOTE: newAssetId can not be set on GameObjects that already have an assetId</para>
/// </summary>
/// <param name="prefab">A GameObject that will be spawned.</param>
/// <param name="newAssetId">An assetId to be assigned to this GameObject. This allows a dynamically created game object to be registered for an already known asset Id.</param>
/// <param name="spawnHandler">A method to use as a custom spawnhandler on clients.</param>
/// <param name="unspawnHandler">A method to use as a custom un-spawnhandler on clients.</param>
public static void RegisterPrefab(GameObject prefab, Guid newAssetId, SpawnHandlerDelegate spawnHandler, UnSpawnDelegate unspawnHandler)
{
if (newAssetId == Guid.Empty)
{
Debug.LogError($"Could not register handler for '{prefab.name}' with new assetId because the new assetId was empty");
return;
}
if (prefab == null)
{
Debug.LogError("Could not register handler for prefab because the prefab was null");
return;
}
NetworkIdentity identity = prefab.GetComponent<NetworkIdentity>();
if (identity == null)
{
Debug.LogError("Could not register handler for '" + prefab.name + "' since it contains no NetworkIdentity component");
return;
}
if (identity.assetId != Guid.Empty && identity.assetId != newAssetId)
{
Debug.LogError($"Could not register Handler for '{prefab.name}' to {newAssetId} because it already had an AssetId, Existing assetId {identity.assetId}");
return;
}
if (identity.sceneId != 0)
{
Debug.LogError($"Can not Register '{prefab.name}' because it has a sceneId, make sure you are passing in the original prefab and not an instance in the scene.");
return;
}
identity.assetId = newAssetId;
Guid assetId = identity.assetId;
if (spawnHandler == null)
{
Debug.LogError($"Can not Register null SpawnHandler for {assetId}");
return;
}
if (unspawnHandler == null)
{
Debug.LogError($"Can not Register null UnSpawnHandler for {assetId}");
return;
}
if (spawnHandlers.ContainsKey(assetId) || unspawnHandlers.ContainsKey(assetId))
{
Debug.LogWarning($"Replacing existing spawnHandlers for prefab '{prefab.name}' with assetId '{assetId}'");
}
if (prefabs.ContainsKey(assetId))
{
// this is error because SpawnPrefab checks prefabs before handler
Debug.LogError($"assetId '{assetId}' is already used by prefab '{prefabs[assetId].name}', unregister the prefab first before trying to add handler");
}
NetworkIdentity[] identities = prefab.GetComponentsInChildren<NetworkIdentity>();
if (identities.Length > 1)
{
Debug.LogWarning($"Prefab '{prefab.name}' has multiple NetworkIdentity components. There should only be one NetworkIdentity on a prefab, and it must be on the root object.");
}
// Debug.Log("Registering custom prefab '" + prefab.name + "' as asset:" + assetId + " " + spawnHandler.GetMethodName() + "/" + unspawnHandler.GetMethodName());
spawnHandlers[assetId] = spawnHandler;
unspawnHandlers[assetId] = unspawnHandler;
}
/// <summary>
/// Removes a registered spawn prefab that was setup with ClientScene.RegisterPrefab.
/// </summary>