Search Unity

Suggestion to UNET devs - Custom spawn handling

Discussion in 'UNet' started by VinQbator, Mar 3, 2016.

  1. VinQbator

    VinQbator

    Joined:
    Aug 9, 2014
    Posts:
    12
    I'm trying to use a custom spawn handler to spawn a child object for player prefab. SpawnDelegate() has 2 arguments: position and assetId. Im not using the position, since the object will be spawned attached to the player and since network delay is inevitable, it makes sense to calculate/set the position locally by client.

    But actually it doesnt even matter if I use the position or not, after the OnObjectSpawn(), ApplySpawnPayload() will be called anyways in the ClientScene.cs and the position will be overwritten with the position received from server. That just does not make sense. Whats the purpose of custom spawn handling at all, if I cant set the most important element of the spawning, position, from there?

    In addition to that, when using ClientScene.RegisterPrefab(), the custom spawning won't work at all and i had to look through unet code to figure that out. This should be mentioned in the custom spawning section of manual I guess.

    For now, I have to use OnStartClient() of the just spawned object to spawn it...
     
  2. gsus725

    gsus725

    Joined:
    Aug 23, 2010
    Posts:
    250
    I just ran into this bug too in 2017.1. ClientScene.RegisterPrefab(GameObject, SpawnDelegate, UnSpawnDelegate) DOES NOT WORK. The prefab you are trying to register will not even appear in ClientScene.prefabs. But if you change it to simply ClientScene.RegisterPrefab(GameObject) suddenly it will work and will appear in ClientScene.prefabs.

    Here is an example
    Code (JavaScript):
    1. var objects_to_register : GameObject[]; //drag from editor
    2.  
    3. function Awake(){
    4.     RegisterPrefabs();
    5. }
    6.  
    7. function RegisterPrefabs(){
    8.     for(var g : GameObject in objects_to_register){
    9.         //have just 1 of these two below lines enabled at a time to see the difference
    10.         ClientScene.RegisterPrefab(g); //this will work
    11.         ClientScene.RegisterPrefab(g, SpawnTest, UnSpawnTest); //this will NOT work
    12.     }
    13.     //if you tried to have custom handlers then this will show you that none of the prefabs were actually registered at all like they should be
    14.     for(var v in ClientScene.prefabs) print(v);
    15. }
    16.  
    17. //if you used RegisterPrefab with handlers this function will have an error about assetId not found in ClientScene.prefabs
    18. function SpawnTest(pos : Vector3, assetId : NetworkHash128){
    19.     print("SpawnTest");
    20.     var prefab : GameObject = ClientScene.prefabs[assetId];
    21.     var go : GameObject = Instantiate(prefab, pos, Quaternion.identity);
    22.     return go;
    23. }
    24.  
    25. function UnSpawnTest(go : GameObject){
    26.     print("UnSpawnTest");
    27. }
    Someone who can explain better than me please submit a bug report
     
  3. gsus725

    gsus725

    Joined:
    Aug 23, 2010
    Posts:
    250
    TLDR: RegisterPrefab(GameObject, SpawnDelegate, UnSpawnDelegate) does not work at all in fact it doesn't even register the object to ClientScene.prefabs