Search Feedback

10
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

(9242)

2D

(233)

Ads

(42)

AI & Navigation

(70)

Analytics

(103)

Animation

(318)

Asset Store

(234)

Assets

(493)

Audio

(158)

Cloud Build

(98)

Collaborate

(33)

Docs & Tutorials

(202)

Editor

(2137)

Everyplay

(13)

Game Performance Reporting

(15)

General

(825)

Graphics

(783)

GUI

(371)

Input

(147)

Licensing

(76)

Networking

(163)

Physics

(346)

Platforms

(407)

Profiling & Optimization

(71)

Runtime

(164)

Scripting

(1030)

Terrain

(151)

WebGL

(132)