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

(8816)

2D

(213)

Ads

(40)

AI & Navigation

(59)

Analytics

(88)

Animation

(292)

Asset Store

(212)

Assets

(485)

Audio

(152)

Cloud Build

(93)

Collaborate

(28)

Docs & Tutorials

(190)

Editor

(2034)

Everyplay

(13)

Game Performance Reporting

(11)

General

(796)

Graphics

(743)

GUI

(353)

Input

(143)

Licensing

(73)

Networking

(153)

Physics

(330)

Platforms

(399)

Profiling & Optimization

(66)

Runtime

(153)

Scripting

(990)

Terrain

(149)

WebGL

(130)