This guide will help you migrate your code if upgrading the Unity Chat SDK to the 5.x version.

The primary change was to replace enums with custom structs. This change makes the SDK much more resilient to API changes, but it may require you to refactor parts of your code that rely on the enums' compilation constant property.

Affected Types

Copied!

Here’s a full list of enum types that got replaced with a struct:

Namespace -> Type

Copied!
  • StreamChat.Core.Models- > StreamMessageType

  • StreamChat.Core.Responses -> StreamImageCropType

  • StreamChat.Core.Responses -> StreamImageResizeType

  • StreamChat.Core.InternalDTO.Models-> AutomodBehaviourType

  • StreamChat.Core.InternalDTO.Models-> AutomodType

  • StreamChat.Core.InternalDTO.Models-> BlockListOptionsBehavior

  • StreamChat.Core.InternalDTO.Models-> ImageCropType

  • StreamChat.Core.InternalDTO.Models-> ImageResizeType

  • StreamChat.Core.InternalDTO.Models-> MessageType

  • StreamChat.Core.InternalDTO.Models-> PushProviderType

  • StreamChat.Core.InternalDTO.Requests-> CreateCallRequestType

  • StreamChat.Core.InternalDTO.Requests-> CreatePollRequestVotingVisibility

  • StreamChat.Core.InternalDTO.Requests-> MessageRequestType

  • StreamChat.Core.InternalDTO.Requests-> TranslateMessageRequestLanguage

  • StreamChat.Core.InternalDTO.Requests-> UpdatePollRequestVotingVisibility

  • StreamChat.Core.InternalDTO.Responses-> ChannelMemberResponseRole

Non-breaking Changes

Copied!

In many cases, the migration from enums to structs will not require any code changes at all, as the syntax remains the same.

Variable Declaration and Assignment

Copied!

Method Parameters and Return Types

Copied!

Basic Equality Comparisons

Copied!

Breaking Changes and How to Fix Them

Copied!

While many use cases will work the same, there are some scenarios where the change from enums to structs will require code modifications.

Switch Statements

Copied!

The most common scenario requiring rewriting the code is when an enum is used in a switch statement. Such cases must be converted to a chain of if/else statements or use the latest pattern-matching syntax in C# 7.0+.

Initialization and Default Values

Copied!

Enums have an implicit default value of 0. Structs do not, and you may need to explicitly initialize the struct where an enum default was previously used.

Old code (with enum):

Copied!

New code (with struct):

Copied!

Const assignment

Copied!

Enums were allowed to be used in compile-time constants.

Old code (with enum):

Copied!

New code (with struct):

Copied!

Structs cannot be const because they are reference types, so you cannot assign them as compile-time constants. Instead, the closest alternative is to use static readonly. This introduces a slight difference: while const values are replaced at compile-time, static readonly values are evaluated at runtime. This means that code relying on const behavior (e.g., embedded in assemblies or libraries) might require adjustment to work with the new static readonly fields.

Summary

Copied!

This concludes the migration guidance on upgrading your project to Stream Chat SDK for Unity 5.x. If you've encountered any issues not mentioned in this guide or have any questions about the migration process, please don't hesitate to contact us. We're here to assist you and ensure a smooth transition to your projects’ latest version of the SDK.