Give Page Feedback | API

Chunk Managers - Custom Managers

Custom Chunk Managers (deriving from ChunkManager) can be used to add completely custom pooling logic or Chunk related logic for custom Asset Chunk Types.

In order to create a custom Chunk Manager, create a new MonoBehaviour script that derives from the ChunkManager base abstract class. This abstract class defines several virtual methods which can be overridden in order to implement the logic required by the Streamable Assets Manager to manage Asset Chunks.

You should place your class inside of the DeepSpaceLabs.SAM namespace, which will make the ChunkManager class available and also help avoid naming conflicts with 3rd party assets or your own code.

--Special Note--
An easy way to start with your custom class is to hover over your custom class name and choose the option Show potential fixes -> Implement abstract class. This will provide default overrides for all abstract class members.

In addition, we recommend adding the following attribute above your class, which will ensure the custom chunk manager is shown with other Chunk Managers in menus:

[AddComponentMenu(GlobalValues.COMPONENT_ROOT_PATH + "Chunk Managers/Custom Chunk Manager Name")]

If you are trying to create a custom pooling based Chunk Manager, consider deriving from the PoolingChunkManager class instead, which will provide you with default pooling logic and reduce the amount of work you need to do. If you wish to go this route, take a look at the Custom Pooling Managers Section.

AwakeExtended (Method - Implementation Optional)

The base ChunkManager class makes use of Unity's Awake method, so if you'd like to add Awake related logic to your custom class, you will need to override this method. It is called automatically at the end of the base ChunkManager's Awake method.

OnDestroyExtended (Method - Implementation Optional)

The base ChunkManager class makes use of Unity's OnDestroyExtended method, so if you'd like to add OnDestroyExtended related logic to your custom class, you will need to override this method. It is called automatically at the end of the base ChunkManager's OnDestroyExtended method.

OnUserRegistered (Method - Implementation Optional)

This is called after a new LOD Group user registers with the Chunk Manager via the Register method. It can be used to perform additional setup and/or processing of the user.

OnUserDeRegistering (Method - Implementation Optional)

This is called just before an LOD Group user is de-registered with the Chunk Manager via the DeRegister method. It can be used to perform cleanup logic that needs to run before the user is officially de-registered.

OnUserDeRegistered (Method - Implementation Optional)

This is called just after an LOD Group user de-registers with the Chunk Manager via the DeRegister method. It can be used to perform cleanup logic that needs to run after the user is officially de-registered.

CreateNewUser (Method - Implementation Optional)

A custom user object needs to be created for each LOD Group that will be used with a Chunk Manager, in order to store LOD Group specific data that is used to carry out the attachment/detatchment logic.

This method is called automatically whenever an LOD Group registers with the Chunk Manager. By default, it creates and returns an instance of the default ChunkManagerUser class, which simply stores the Chunk Manager that created it and the ILODGroup object (which has all information about the registered LOD Group).

If you want to store additional information or provide per LOD Group methods/properties, you can create a custom class that derives from ChunkManagerUser, then override the CreateNewUser method to return an instance of this new custom ChunkManagerUser class.

Note, while this ChunkManagerUser class does not have a presence in this editor guide or the online API, short Summary and param code comments have been provided for it, so you can explore the class and its members further using Visual Studio or whatever Script Editor you are using.

AttachChunksAlreadyInSceneToCells (Method - Implementation Optional)

This is the method to override if you have some sort of pool or other means of storing Asset Chunk Sets. It allows you to retrieve Asset Chunk Sets for World Cells from those resources, to avoid having to stream in new Asset Chunks via the Chunk Streamer.

When Asset Chunks can be retrieved for a World Cell, you should attach them to the World Cell using its AttachChunkToCell method.

You also need to remove from the input list of World Cells any World Cell whose Asset Chunks are attached via this method, otherwise it will incorrectly be sent to the Chunk Streamer.

DetachAndProcessChunksToKeepInScene (Method - Implementation Optional)

This is the method to override if you have some sort of pool or other means of storing Asset Chunk Sets. It allows you to store Asset Chunk Sets for World Cells in those resources, which you can then later reuse.

When Asset Chunks can be stored for a World Cell, you should detach them from the World Cell using its DetachChunksFromCell method.

You also need to remove from the input list of World Cells any World Cell whose Asset Chunks are detached and stored via this method, otherwise it will incorrectly be sent to the Chunk Streamer for unloading.

--Special Note--
The return type of AttachChunksAlreadyInSceneToCells and DetachAndProcessChunksToKeepInScene is IEnumerator. This return type is special as it allows the method to be iterated over, possibly over multiple frames. This allows the execution's performance impact to be spread out over multiple frames, which is awesome! There are two strategies for implementing methods with this return type:

1) Simply include yield return statements within the method's body where the returned object is either null or an instance of a YieldInstruction.

The compiler will auto generate a state machine class, which will be used to iterate the method's logic. The drawback of this technique is every time the method is called, a new instance of the auto generated class is created, resulting in garbage generation throughout the lifetime of the game.

2) Implement one of the reusable enumerator classes found within the API.

This strategy is harder to implement as you will need to implement the state machine logic yourself, as well as perform some other code related task necessary to use the reusable enumerators. It is recommended for experienced coders only!

If you elect to go with option 2, you can find detailed information on how to use the Reusable Enumerator classes in the Yield Enumerator Section within the Secondary Non Components Chapter.

Yield Enumerator Classes

GetChunkPosition (Method - Implementation Optional)

By default, this method is implemented in the base Chunk Manager to cast the input chunk to a GameObject in order to get the Transform position of the object.

If not using Game Objects, you should override this method and implement logic that works correctly with your asset chunk type.

SetChunkPosition (Method - Implementation Optional)

By default, this method is implemented in the base Chunk Manager to cast the input chunk to a GameObject in order to set the Transform position of the object.

If not using Game Objects, you should override this method and implement logic that works correctly with your asset chunk type.

SetChunkActiveState (Method - Implementation Optional)

By default, this method is implemented in the base Chunk Manager to cast the input chunk to a GameObject in order to use the GameObject.SetActive method to set its active state.

If not using Game Objects, you should override this method and implement logic that works correctly with your asset chunk type.

Accessing Registered Users/LOD Groups

The registration of new LOD Group users is handled automatically by the base Chunk Manager class. You can access the ChunkManagerUser object of any registered LOD Group via the RegisteredUsers property, using a simple Indexer, where the index is the userID passed into each of the attachment/detatchment methods.

--Code Example--
ChunkManagerUser user = RegisteredUsers[userID];

Do note that when using this Indexer, the returned object is always of type ChunkManagerUser, so if you have created a custom class deriving from ChunkManagerUser and have overridden the CreateNewUser method to return an instance of this custom class, you will need to cast the returned user object to your custom type in order to access any custom data stored on the custom user object.

You can also iterate through all registered LOD Group users in a garbage free way using the RegistrantEntries method, which returns an enumerable struct object (we recommend using a simple foreach statement to perform the iteration).

While these should be the only members of the RegistrationHandler class you need to utilize, you are free to take a look at this class in more detail by viewing the Registration Handler Section within the Secondary Non Components Chapter.

Learn More About The Registration Handler