Search Unity

Asset Bundle's lose Network Identity Asset ID required for uNet spawning, Nested Prefabs

Discussion in 'Asset Bundles' started by Phedg1, Jan 17, 2019.

  1. Phedg1

    Phedg1

    Joined:
    Mar 3, 2015
    Posts:
    113
    The game I'm working on has the expectation that players will be able to download the source code, open it in Unity and create their own levels using premade prefabs. Those levels will then be saved as nested prefabs in asset bundles. The hope is that users will then be able to share those asset bundles with each other in order to play networked games together in the levels they've made and shared.

    However, when a level prefab is built into an asset bundle it seems the NetworkIdentity.assetId NetworkHash128 property isn't serialized and is lost, causing errors the error "Failed to spawn server object, did you forget to add it to the NetworkManager?" when the client joins the server. Saving the assetId as a string on the prefab and registering the prefab with ClientScene.RegisterPrefab(levelPrefab, NetworkHash128.Parse(assetIdString)) will get around this, allowing the levels to be loaded on the client. However, every networked prefab that's part of the level nested prefab seems to have no observers. This doesn't cause any errors because the level prefab was spawned successfully.

    The game is almost finished and is built around the uNet framework. I know it's been depreciated but Unity hasn't released an alternative which is just plain frustrating. Can anyone help? Thanks.
     
    Last edited: Jan 17, 2019
    Futurristic and Sam-DreamsMaker like this.
  2. Sam-DreamsMaker

    Sam-DreamsMaker

    Joined:
    May 2, 2015
    Posts:
    13
    Same for me
     
  3. Ryanc_unity

    Ryanc_unity

    Unity Technologies

    Joined:
    Jul 22, 2015
    Posts:
    332
    "download the source code, open it in Unity and create their own levels using premade prefabs"

    Where do the premade prefabs come from? Are you downloading a *.unitypackage with the source premade prefabs, or are you trying to use data loaded from an asset bundle?
     
  4. Futurristic

    Futurristic

    Joined:
    Jun 21, 2016
    Posts:
    47
    Hello Ryanc, I am facing similar problem when trying to download assets bundle file, assign it assetId at runtime and register it on Client side by ClientScene.RegisterSpawnHandler(assetId, SpawnObject, UnSpawnObject), script.
    I fallow the custom spawn handler given in link https://docs.unity3d.com/Manual/UNetCustomSpawning.html
    However, It spawn successfully on host but on client give me an error like
    OnObjectSpawn netId: 3 has invalid asset id in unity unet....
    OnObjectSpawn netId: 4 has invalid asset id in unity unet....so on...
    I am not able to file solution around it, when trying to Instantiate content by assets bundle....Help me please......

    Thanks....
     
  5. skaughtx0r

    skaughtx0r

    Joined:
    Mar 9, 2014
    Posts:
    74
    I found a solution to this, but it requires modifying the MultiplayerHLAPI package.

    The problem is that the OnValidate() method is running in play mode in the editor, which calls SetupIDs. In SetupIDs, it's unable to find the prefab path (since the asset was loaded from a bundle) so the assetID gets set to all 0's and made invalid.

    I ended up just wrapping the code in SetupIDs() in an
    if (!Application.isPlaying) { ... }
    block.

    NetworkIdentity.cs
    Code (CSharp):
    1. void SetupIDs()
    2. {
    3.     if (!Application.isPlaying)
    4.     {
    5.         GameObject prefab;
    6.         if (ThisIsAPrefab())
    7.         {
    8.             ForceSceneId(0);
    9.             AssignAssetID(gameObject);
    10.         }
    11.         else if (ThisIsASceneObjectWithThatReferencesPrefabAsset(out prefab))
    12.         {
    13.             AssignAssetID(prefab);
    14.         }
    15.         else
    16.         {
    17.             m_AssetId.Reset();
    18.         }
    19.     }
    20. }
    For reference I made this change in my MultiplayerHLAPI fork: https://github.com/skaughtx0r/MultiplayerHLAPI/blob/master/Runtime/NetworkIdentity.cs#L397