Search Feedback

11
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

(10086)

2D

(266)

Ads

(66)

AI & Navigation

(81)

Analytics

(120)

Animation

(370)

Asset Store

(280)

Assets

(513)

Audio

(172)

Cloud Build

(126)

Collaborate

(46)

Docs & Tutorials

(219)

Editor

(2320)

Everyplay

(13)

Game Performance Reporting

(16)

General

(944)

Graphics

(830)

GUI

(401)

Input

(157)

Licensing

(85)

Networking

(179)

Physics

(369)

Platforms

(430)

Profiling & Optimization

(77)

Runtime

(172)

Scripting

(1104)

Terrain

(164)

WebGL

(139)