Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Unity 2022.2 is now available as the latest Tech release.
    Dismiss Notice
  3. We are making some changes to the DOTS forums.
    Dismiss Notice
  4. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Remote Instantiate

Discussion in 'Multiplayer' started by davej256, Nov 24, 2022.

  1. davej256

    davej256

    Joined:
    Nov 11, 2022
    Posts:
    8
    Hello. I've been brainstorming a few ways to remotely instantiate objects on client machines. Ideally you would want a way to map them to IDs so that you don't have to deal with pesky string allocations and such over a network.

    I've experimented with AssetDatabase, which allows you to return all of the prefabs in a certain folder with a GUID tagged to them along with a way to lookup the path using the GUID. Great! But you can't use it in a build.
    Next I found Resources.LoadAll() which can return all of the prefabs in a specified folder, but doesn't return the path to them or any way to tag them to an ID.

    Are there any clever hacks for this? Or should I just bite the bullet and use strings?
     
  2. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    114
    Hi @davej256 , what Networking Framework are you using? These will usually take care of spawning for you, allowing you to spawn objects with a simple API call. Here's how it is done in Netcode For Gameobjects, for example
     
  3. davej256

    davej256

    Joined:
    Nov 11, 2022
    Posts:
    8
    I am writing a custom framework. Regarding the link you sent, it seems a bit annoying to have to register every prefab that you want to spawn. I guess that's the answer to the problem then. I looked at photon and it seems that they don't do this, and that with their method you just place a prefab in the resources folder and attach their component to it, then instantiate via string. Very interesting.
     
    Last edited: Nov 24, 2022
  4. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    114
    I'd avoid that approach. Using Resources is in general a bad practice performance and memory-wise, and using strings for referencing assets leads to broken code the moment you rename something