Fish-Net: Networking Evolved
  • Overview
    • What is FishNet?
      • Features
        • Unity Compatibility
        • Performance
          • Benchmark Setup
          • Fish-Networking Vs Mirror
      • Pro, Projects, and Support
      • Business Support
      • Development
        • Changelog
        • Roadmap
      • Branding
      • Legal Restrictions
    • Showcase
      • Upcoming Releases
    • Asset Integrations
      • Fish-Network-Discovery
    • Community Resources
  • Guides
    • Getting Started
      • Installing Fish-Networking
      • Getting Connected
      • Preparing Your Player
      • Moving Your Player Around
      • Spawning and Despawning Items
      • Using SyncVars to Sync Colors
      • Connecting to Remote Devices
      • Beyond the Basics
    • High-Level Overview
      • Fundamentals
      • Networking Models
      • Terminology
        • Server, Client, Host
        • Communicating
        • Miscellaneous
      • Transports
    • Features
      • Server and Client Identification
        • Executing on Server or Client
        • NetworkConnections
      • Networked GameObjects and Scripts
        • NetworkObjects
        • NetworkBehaviour
        • Spawning and Despawning
          • Predicted Spawning
          • Nested NetworkObjects
          • Object Pooling
      • Network State Events
      • Network Communication
        • Remote Procedure Calls
        • SyncTypes
          • Customizing Behavior
          • SyncVar
          • SyncList
          • SyncHashSet
          • SyncDictionary
          • SyncTimer
          • SyncStopwatch
          • Custom SyncType
        • Broadcasts
      • Data Serialization
        • Custom Serializers
          • Interface Serializers
          • Inheritance Serializers
      • Ownership
        • Using Ownership To Read Values
      • Area of Interest (Observer System)
        • Modifying Conditions
        • Custom Conditions
      • Scene Management
        • Scene Events
        • Scene Data
          • SceneLookupData
          • SceneLoadData
          • SceneUnloadData
        • Loading Scenes
          • Automatic Online and Offline Scenes
        • Unloading Scenes
        • Scene Stacking
        • Scene Caching
        • Scene Visibility
        • Persisting NetworkObjects
        • Custom Scene Processors
          • Addressables
      • InstanceFinder
      • Addressables
      • Transports
        • Multipass
      • Prediction
        • What Is Client-Side Prediction
        • Configuring PredictionManager
        • Configuring TimeManager
        • Configuring NetworkObject
        • Offline Rigidbodies
        • Interpolations
        • Creating Code
          • Controlling An Object
          • Non-Controlled Object
          • Understanding ReplicateState
            • Using States In Code
            • Predicting States In Code
          • Advanced Controls
        • Custom Comparers
        • PredictionRigidbody
        • Using NetworkColliders
      • Lag Compensation
        • States
        • Raycast
        • Projectiles
    • Upgrading API
    • Server Hosting
      • Edgegap - Official Partner
        • Getting Started with Edgegap
      • Hathora
        • Getting Started with Hathora
      • Amazon Web Services (AWS)
        • Getting Started with AWS
    • Upgrading To Fish-Networking
    • Troubleshooting
      • Technical Limitations
      • Creating Bug Reports
        • Report Example
      • FAQ
  • FishNet Building Blocks
    • Components
      • Managers
        • NetworkManager
        • TimeManager
        • PredictionManager
        • ServerManager
        • ClientManager
        • SceneManager
        • TransportManager
          • IntermediateLayer
        • StatisticsManager
        • ObserverManager
          • HashGrid
        • RollbackManager (Pro Feature)
      • Prediction
        • Network Collider
          • NetworkCollision
          • NetworkCollision2D
          • NetworkTrigger
          • NetworkTrigger2D
        • OfflineRigidbody
        • PredictedOwner
        • PredictedSpawn
      • Utilities
        • PingDisplay
        • BandwidthDisplay
        • Tick Smoothers
          • NetworkTickSmoother
          • OfflineTickSmoother
          • MonoTickSmoother [Obsolete]
          • DetachableNetworkTickSmoother [Obsolete]
      • PlayerSpawner
      • DefaultScene
      • ServerSpawner
      • Authenticator
      • ColliderRollback
      • NetworkAnimator
      • NetworkBehaviour
      • NetworkTransform
      • NetworkObject
      • NetworkObserver
    • Prefabs
      • NetworkManager
      • NetworkHudCanvas
    • ScriptableObjects
      • ObserverConditions
        • DistanceCondition
        • GridCondition
        • HostOnlyCondition
        • MatchCondition
        • OwnerOnlyCondition
        • SceneCondition
      • SpawnablePrefabs
        • DefaultPrefabObjects
        • SinglePrefabObjects
        • DualPrefabObjects
      • LevelLoggingConfiguration
    • Transports
      • Tugboat
      • Multipass
      • Yak (Pro Feature)
      • Bayou
      • FishyWebRTC
      • FishyUnityTransport
      • FishySteamworks (Steam)
      • FishyEOS (Epic Online Services)
      • FishyFacepunch (Steam)
      • FishyRealtime (Photon)
  • API Documentation
    • API Reference
Powered by GitBook
On this page
  • Scene Addressables
  • Prefab Addressables
  1. Guides
  2. Features

Addressables

PreviousInstanceFinderNextTransports

Last updated 1 year ago

Both addressable scenes and prefabs work over the network.

Scene Addressables

Scene addressables utilize Fish-Networking's . With a few overrides you can implement addressable scenes using .

Prefab Addressables

To work reliably each addressables package must have a unique ushort Id, between 1 and 65535. Never use 0 as the Id, as the preconfigured SpawnablePrefabs use this Id. You may assign your addressable Ids however you like, for instance using a dictionary that tracks your addressable names with Ids.

Registering addressable prefabs with Fish-Networking is easy once each package has been given an Id.

The code below shows one way of loading and unloading addressable prefabs for the network.

/// <summary>
/// Reference to your NetworkManager.
/// </summary>
private NetworkManager _networkManager => InstanceFinder.NetworkManager;
/// <summary>
/// Used to load and unload addressables in async.
/// </summary>
private AsyncOperationHandle<IList<GameObject>> _asyncHandle;

/// <summary>
/// Loads an addressables package by string.
/// You can load whichever way you prefer, this is merely an example.
/// </summary>
public IEnumerator LoadAddressables(string addressablesPackage)
{
    /* FishNet uses an Id to identify addressable packages
     * over the network. You can set the Id to whatever, however
     * you like. A very simple way however is to use our GetStableHash
     * helper methods to return a unique key for the package name.
     * This does require the package names to be unique. */
    ushort id = addressablesPackage.GetStableHash16();

    /* GetPrefabObjects will return the prefab
     * collection to use for Id. Passing in true
     * will create the collection if needed. */
    SinglePrefabObjects spawnablePrefabs = (SinglePrefabObjects)_networkManager.GetPrefabObjects<SinglePrefabObjects>(id, true);

    /* Get a cache to store networkObject references in from our helper object pool.
     * This is not required, you can make a new list if you like. But if you
     * prefer to prevent allocations FishNet has the really helpful CollectionCaches
     * and ObjectCaches, as well Resettable versions of each. */
    List<NetworkObject> cache = CollectionCaches<NetworkObject>.RetrieveList();

    /* Load addressables normally. If the object is a NetworkObject prefab
     * then add it to our cache! */
    _asyncHandle = Addressables.LoadAssetsAsync<GameObject>(addressablesPackage, addressable =>
    {
        NetworkObject nob = addressable.GetComponent<NetworkObject>();
        if (nob != null)
            cache.Add(nob);
    });
    yield return _asyncHandle;
    
    /* Add the cached references to spawnablePrefabs. You could skip
     * caching entirely and just add them as they are read in our LoadAssetsAsync loop
     * but this saves more performance by adding them all at once. */
    spawnablePrefabs.AddObjects(cache);

    //Optionally(obviously, do it) store the collection cache for use later. We really don't like garbage!
    CollectionCaches<NetworkObject>.Store(cache);
}

/// <summary>
/// Loads an addressables package by string.
/// </summary>
public void UnoadAddressables(string addressablesPackage)
{
    //Get the Id the same was as we did for loading.
    ushort id = addressablesPackage.GetStableHash16();

    /* Once again get the prefab collection for the Id and
     * clear it so that there are no references of the objects
     * in memory. */
    SinglePrefabObjects spawnablePrefabs = (SinglePrefabObjects)_networkManager.GetPrefabObjects<SinglePrefabObjects>(id, true);
    spawnablePrefabs.Clear();
    //You may now release your addressables!
    Addressables.Release(_asyncHandle);
}

When adding addressables on your client be sure to do so before the server will send spawn messages for them.

Custom Scene Processors
this guide