Search Unity

Question Howto add newly created prefabs to a running game ?

Discussion in 'Prefabs' started by inflo007, Nov 6, 2022.

  1. inflo007

    inflo007

    Joined:
    Nov 7, 2021
    Posts:
    8
    hi all, how are you ?

    The thing is -> I want to write/play a game (netcode game if that is interresting for this question) and while i play with my friends, I or a friend has created a new mesh , and we want it to appear in our scene while we are playing ?

    We dont want to stop the game, write new code to add the mesh/gameobject and then start the game again ?

    How is this possible ?

    Thanks for answers :)

    flo
     
  2. inflo007

    inflo007

    Joined:
    Nov 7, 2021
    Posts:
    8
    i mean , spawning a gameobject from harddisk (with path) , while game is running
     
  3. MirceaI

    MirceaI

    Unity Technologies

    Joined:
    Nov 24, 2020
    Posts:
    36
  4. inflo007

    inflo007

    Joined:
    Nov 7, 2021
    Posts:
    8
    hi @Mirceal ,

    thanks for your answer, but the addressable states "Once an asset (e.g. a prefab) is marked "addressable", it generates an address" . That seemed to mean, the mesh/gameobject/prefab is needed while building/running the game.

    I need something like
    GameObject instance = Instantiate(Resources.Load("myMeshGameObject", typeof(GameObject))) as GameObject;

    but here the thing is, the myMeshGameObject needs also be in the Resources folder while building/running.

    I want to add it later, while the game is still running, i want to add new meshgameobjects ?

    Hope i could explain it a little bit better
     
  5. inflo007

    inflo007

    Joined:
    Nov 7, 2021
    Posts:
    8
    found https://docs.unity3d.com/ScriptReference/AssetBundle.html

    var uwr = UnityWebRequestAssetBundle.GetAssetBundle("https://myserver/myBundle.unity3d");
    yield return uwr.SendWebRequest();

    // Get an asset from the bundle and instantiate it.
    AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(uwr);
    var loadAsset = bundle.LoadAssetAsync<GameObject>("Assets/Players/MainPlayer.prefab");
    yield return loadAsset;

    Instantiate(loadAsset.asset);

    But here it says: Note that bundles are not compatible between platforms

    I am coding a networking (with netcode) game, so it should run on windows and android and others too, so hmmm ? Is there something else ? When i can build a asset WITH a game, that is compatible on different platforms, why can i not only build a asset, that is compatible with all platforms ?
     
    Last edited: Nov 10, 2022
  6. MirceaI

    MirceaI

    Unity Technologies

    Joined:
    Nov 24, 2020
    Posts:
    36
    AssetBundles are optimized for runtime and each platform has specific requirements that might not be compatible with other platforms.

    Let's take a PNG texture example. When you build the AssetBundle containing the texture, depending on your rendering API, platform, etc, the texture might be compressed using DXTn algorithm (https://en.wikipedia.org/wiki/S3_Texture_Compression), PVRTC algorithm (https://en.wikipedia.org/wiki/PVRTC), ASTC algorithm (https://en.wikipedia.org/wiki/Adaptive_scalable_texture_compression) or other algorithm. This makes loading and using the texture very efficient, but also ties the texture to the rendering API and platform for which it was built and it might make it invalid for other platforms.

    You can build your AssetBundle for each platform you need using https://docs.unity3d.com/ScriptReference/BuildPipeline.BuildAssetBundles.html and download the right one at runtime.
     
  7. inflo007

    inflo007

    Joined:
    Nov 7, 2021
    Posts:
    8
    assetbundles looks good, also i can set the build target platform (windows, android, etc) https://docs.unity3d.com/ScriptReference/BuildPipeline.BuildAssetBundle.html with targetPlatform. Also i can run these build from script.

    But what i not can see, how do i load on the network-server (netcode in hosting mode) the right assetbundle for the right platform ? When lets say, a windows-machine started in host+client mode, and an android devices connects with client-mode to the game. How to spawn the right assetbundle on the right platform ?
     
  8. MirceaI

    MirceaI

    Unity Technologies

    Joined:
    Nov 24, 2020
    Posts:
    36
  9. inflo007

    inflo007

    Joined:
    Nov 7, 2021
    Posts:
    8
    but how do i get the platforms of the connected clients ? There is NetworkManager.Singleton.ConnectedClients which returns id, but no platform ?

    On the server side i could say something like:

    if(isServer) {
    GET NetworkManager.Singleton.ConnectedClients
    FOREACH( client-ID ) sendClientRPC( GET platform ) {
    ?? what now ?? simply run
    FOREACH( client-platform ) {
    IF (client-platform == ANDROID) {
    var uwr = UnityWebRequestAssetBundle.GetAssetBundle("https://myserver/myBundle.unity3d");
    yield return uwr.SendWebRequest();

    // Get an asset from the bundle and instantiate it.
    AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(uwr);
    var loadAsset = bundle.LoadAssetAsync<GameObject>("Assets/Players/MainPlayer.prefab");
    yield return loadAsset;

    Instantiate(loadAsset.asset);
    }
    IF(client-platform == WINDOWS) {
    LOAD windows-asset-bundle ...
    }
    }

    Would that work ? Or is there a way to request/GET the connected client platforms on the server side to load/instantiate the right-platform-assetbundle ?

    thx for help
     
  10. inflo007

    inflo007

    Joined:
    Nov 7, 2021
    Posts:
    8
    the netcode spawn means that all networkObjects got the same networkObjectId, so that the same gameobject on every client e.g. translates-to-x.

    So every network-gameobject needs to update/set the networkObjectId to the same value on every client ( per RPC ? ) then it could perhaps work ,or?
     
  11. Junami

    Junami

    Joined:
    Oct 27, 2022
    Posts:
    4
    Hey hate to hijack the thread but a similar issue perhaps. I have been searching around trying to figure this out... I basically want to create objects based on a string of characters at runtime and assign a PNG to each object (PNGs dropped into the Assets folder). Make it a Player and move it around. But it sounds like this isn't something I can do easily. Was hoping it would be as simple as "render sprite(body parts) on screen at (X,Y) from Assets/back.png ... legs.png" but... I would still need to go down this AssetBundle route because of the phone vs PC build?
    I have figured out how to do it in the editor mode, but want to build players based on my pre-determined body part string.

    Any help from anyone is appreciated!
     
  12. inflo007

    inflo007

    Joined:
    Nov 7, 2021
    Posts:
    8