Search Feedback

14
votes

NetworkWriter for Custom Spawn Handler

Networking

-

-

Currently, there's no way to pass initialization data to a custom spawn handler.

I suggest this:

// overload that accepts a NetworkWriter
NetworkServer.Spawn(GameObject go, NetworkHash128 assetId, NetworkWriter writer)

// new spawn delegate that takes NetworkReader
GameObject SpawnWithReaderDelegate(Vector3 position, NetworkHash128 assetId, NetworkReader reader)

// overload that accepts NetworkReader
void ClientScene.RegisterSpawnHandler(NetworkHash128 assetId, SpawnWithReaderDelegate spawnWithReaderHandler, UnSpawnDelegate unspawnHandler)

// This would then be possible:
ClientScene.RegisterSpawnHandler(assetId, SpawnObjectWithReader, UnspawnObject);

public GameObject SpawnObjectWithReader(Vector3 position, NetworkHash128 assetId, NetworkReader reader)
{
var go = Instantiate(prefab) as GameObject;
int sentValue = reader. ReadInt32();
int someLocalData = SomeLocalData.someVar;
go.GetComponent<MyScript>().Initialize(sentValue, someLocalData);
return go;
}

Comments (2)

  1. 7f6f87b6282154620d963ceb83491e5c?d=mm

    angusmf

    Jul 11, 2017 18:58

    IMO, the workaround for this is something you have to do so often in UNET code (wait for NetworkServer.Active == true) that there's no point in adding something like this to the API for such a narrow use.

  2. Bb62dbba3bb3255091a0c26936236c94?d=mm

    Wobes

    Jul 09, 2017 00:28

    bump

Your opinion counts

Help us make things better. Share your great idea for improving Unity or vote for other people’s.

Log in to post a new idea

Categories

All

(9039)

2D

(221)

Ads

(40)

AI & Navigation

(67)

Analytics

(92)

Animation

(304)

Asset Store

(223)

Assets

(489)

Audio

(157)

Cloud Build

(94)

Collaborate

(31)

Docs & Tutorials

(197)

Editor

(2084)

Everyplay

(13)

Game Performance Reporting

(13)

General

(812)

Graphics

(762)

GUI

(363)

Input

(145)

Licensing

(73)

Networking

(158)

Physics

(342)

Platforms

(403)

Profiling & Optimization

(69)

Runtime

(161)

Scripting

(1015)

Terrain

(152)

WebGL

(131)