Sample code
Here we provide you with a sample application to experience ZEGOCLOUD's In-app Chat, and also describe how to run the sample code.
Prerequisites
Go to ZEGOCLOUD Admin Console to create a new project, and get the AppID and AppSign of your project.
The SDK now also supports using Token for authentication, if you want to change your authentication mode, refer to the Guide for upgrading the authentication mode from using the AppSign to Token.
Environment-specific requirements:
-
Install Unity 2021.3.18f1c1 or later version. If not installed, you can download Unity Hub from Unity official website, and then install the Unity version you need (if you are not sure, it is recommended to install the latest LTS version). When downloading, it is recommended to select the corresponding Platforms module according to your needs, for example, installing on Windows:
-
Select the corresponding development environment or device according to the platform that the sample code needs to run on:
- To run the Android sample source code: requires devices or emulators that support Android 4.1 or later versions and JDK 1.6 or later versions (it is recommended to use a real device). If it is a real device, please enable the "allow debugging" option.
- To run the iOS sample source code: requires Xcode 13.0 or later versions and devices or emulators that support iOS 9.0 or later versions (it is recommended to use a real device).
- To run the macOS sample source code: requires macOS 10.11 or later versions.
- To run the Windows sample source code: requires Windows 7 or later versions and Visual Studio 2015 or later versions installed.
- Ensure that the device running the program is connected to the network properly.
- Ensure that Unity has installed the Platforms module corresponding to the platform that needs to be run. If Unity has been installed but the corresponding Platforms module has not been installed, you need to download the corresponding module. For example, on Windows: Open Unity Hub, click the "Install" option on the left, select the Unity version to be installed, click the icon in the upper right corner, select the "Add modules" option, and select the corresponding module in the pop-up window for download.
Sample code structure
The following structure shows the subdirectory structure of the In-app Chat sample code:
Unity3dDemo
└─Assets
├─Scenes // Scenes
│ Conv.unity // Conversation scene
│ Conv.unity.meta
│ Login.unity // Login scene
│ Login.unity.meta
│
└─Scripts // Demo scripts
CallOptionList.cs // Call invitation options
CallOptionList.cs.meta
ConvCreateOptionList.cs // Create conversation
ConvCreateOptionList.cs.meta
ConvPage.cs // Conversation page
ConvPage.cs.meta
ConvPageOptionList.cs // Switch conversation, room, and group lists
ConvPageOptionList.cs.meta
GroupPage.cs // Group-related logic
GroupPage.cs.meta
KeyCenter.cs // Fill in AppID and AppSign
KeyCenter.cs.meta
LoginClicked.cs // Login
LoginClicked.cs.meta
MemberPage.cs // Group and room member list-related logic
MemberPage.cs.meta
MessagePage.cs // Message-related logic
MessagePage.cs.meta
RoomPage.cs // Room-related logic
RoomPage.cs.meta
│
└─ZIM // ZIM Unity SDK package
├─Plugins // Import ZIM plugins for various platforms
│ ├─Android
│ ├─iOS
│ ├─macOS
│ └─windows
└─Scripts // C# encapsulation of ZIM plugin interface
Run the sample code
-
Refer to the "Integrating SDK" section in Getting started to complete the SDK integration.
-
Fill in the "appID" and "appSign" mentioned in the previous "Prerequisites" section in the
Assets/Scripts/KeyCenter.cs
file. -
Click the "Projects" tab in Unity Hub, click "Open" in the upper right corner, and open the downloaded sample source code.
-
After opening the sample source code, an
Editor version not installed
prompt may pop up, indicating that the Unity Editor version used by the sample source code project cannot be found. At this time, select another Unity Editor version and specify the Unity version you are using. -
After opening the project, select "Login" and "Conv" under the "Project > Assets > Scenes" directory in the lower left corner and drag them to the "Hierarchy" window in the upper left corner.
-
In the top menu bar, select "File > Build Settings."
-
Make sure that "Scenes/Login" is 0 and "Scenes/Conv" is 1 in "Scenes in Build," select the platform in "Platform," and switch to the platform you want to run.
-
Start running.
Run on Android device
- Refer to step 7, switch the "Platform" to the "Android" platform, click "Player Settings" to set package name and other configurations.
- After completing the settings, connect the Android device and click "Build And Run".
Run on iOS device
- Refer to step 7, switch the "Platform" to the "iOS" platform, click "Build" to save the iOS project locally, and open the built project through Xcode.
- In the opened Xcode project, open the "Targets - Unity-iPhone - Signing & Capabilities - Signing" tab, first make sure to check "Automatically manage signing", then switch "Team" to your own team, and modify "Bundle Identifier" to your own Bundle ID.
- Open "Targets - Unity-iPhone - Build Phases - Embeded Frameworks", click the plus sign "+", and select the path of the integrated ZIM SDK: "ZIM/Plugins/IOS/ZIM.xcframework/ios-arm64_armv7/ZIM.framework".
- Open "Targets - Unity-iPhone - Build Settings", search for bitcode in the search box, and make sure that Enable Bitcode is set to "No".
- Click the Run button to run the sample demo.
Run on Mac device
- Refer to step 7, switch the "Platform" to the "macOS" platform, select the architecture to be built, such as Intel + Apple Silicon, and then click "Build" to generate the App locally.
- After the application is generated, double-click to run it.
Run on Windows device
- Refer to step 7, switch the "Platform" to the "Windows" platform, select the architecture to be built, note that the architecture selected here must be consistent with the SDK architecture retained during integration, and then click "Build" to generate the App locally.
- Double-click the generated .exe file to run it.