Skip to content

Repository files navigation

csharp-sdk

buildversion

LeanCloud C# SDK,基于 .Net Standard 2.0 标准开发,包括服务如下:

  • 存储
  • 排行榜
  • 即时通讯
  • Live Query
  • 云引擎
  • 多人对战

参考:Unity Demo

安装

直接导入

Release 下载指定版本 SDK,暂不支持 Nuget 方式。

UPM

在 Unity 项目的 Packages/manifest.json 中添加依赖项

"dependencies": {
"com.leancloud.storage": "https://github.com/leancloud/csharp-sdk-upm.git#storage-2.4.1",
"com.leancloud.realtime": "https://github.com/leancloud/csharp-sdk-upm.git#realtime-2.4.1",
"com.leancloud.play": "https://github.com/leancloud/csharp-sdk-upm.git#play-2.4.1"
}

编译

Repo clone 仓库,使用 Visual Studio 打开 csharp-sdk.sln 编译。 Unity 用户在编译完成后,请将 XX-Unity 工程中 Debug/Release 的 dlls 拷贝至 Unity 工程下的 Plugins 目录中即可使用。 其他 .Net 平台用户使用 XX 工程即可。 (XX 指 Storage,Realtime,LiveQuery 等)

测试

  • Install .NET SDK 3.1 and 5.0 (use dotnet --list-sdks to list all installed versions).
  • Run dotnet test in a .sln/.csproj dir.

ARM arch Mac users can install x64 arch .NET SDK via https://github.com/isen-ng/homebrew-dotnet-sdk-versions and use `dotnetx64`` instead.

项目结构

由于 Unity 平台并不是标准的 .Net Standard 2.0,所以在每个服务下单独拆分出了 XX-Unity 工程,源码和主工程是一致的,只是在依赖库方面有些区别。后面也可能针对 Unity 平台做些相关支持。

├── csharp-sdk.sln // 项目配置
├── Common // 公共库,包含基础功能
├── Storage // 存储服务
│ ├── Storage // .Net Standard 2.0 工程
│ ├── Storage-Unity // Unity 工程
│ └── Storage.Test // 单元测试
├── Realtime // 即时通讯服务
│ ├── Realtime // .Net Standard 2.0 工程
│ ├── Realtime-Unity // Unity 工程
│ └── Realtime.Test // 单元测试
├── LiveQuery // LiveQuery 服务
│ ├── LiveQuery // .Net Standard 2.0 工程
│ ├── LiveQuery-Unity // Unity 工程
│ └── LiveQuery.Test // 单元测试
├── Sample // 示例
│ ├── RealtimeApp // 即时通讯应用,主要测试断线重连
│ └── LiveQueryApp // LiveQuery 应用,主要测试断线重连
└── UnityLibs // Unity 依赖
└── Newtonsoft.Json.dll // Json 库,由于 Unity iOS AOT 的原因,不能使用 .Net Standard 2.0 版本

导入

usingLeanCloud;// 数据存储usingLeanCloud.Storage;// 即时通讯usingLeanCloud.Realtime;// 多人对战usingLeancloud.Play;

初始化

LCApplication.Initialize("ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz","NUKmuRbdAhg1vrb2wexYo1jo","https://ikggdre2.lc-cn-n1-shared.com");

调试

开启调试日志

LCLogger.LogDelegate+=(level,info)=>{switch(level){caseLCLogLevel.Debug:WriteLine($"[DEBUG] {DateTime.Now}{info}\n");break;caseLCLogLevel.Warn:WriteLine($"[WARNING] {DateTime.Now}{info}\n");break;caseLCLogLevel.Error:WriteLine($"[ERROR] {DateTime.Now}{info}\n");break;default:WriteLine(info);break;}}

数据存储

对象

LCObjectobj=newLCObject("Hello");obj["intValue"]=123;awaitobj.Save();

更多关于对象用法:参考

查询

LCQuery<LCObject>query=newLCQuery<LCObject>("Hello");query.Limit(2);List<LCObject>list=awaitquery.Find();

更多关于查询用法:参考

文件

LCFilefile=newLCFile("avatar",AvatarFilePath);awaitfile.Save((count,total)=>{TestContext.WriteLine($"progress: {count}/{total}");});

更多关于文件用法:参考

用户

awaitLCUser.Login("hello","world");

更多关于用户用法:参考

GeoPoint

LCGeoPointp1=newLCGeoPoint(20.0059,110.3665);

更多关于 GeoPoint 用法:参考

即时通讯

用户

LCIMClientclient=newLCIMClient("c1");// 登录awaitclient.Open();// 注销awaitclient.Close();

更多关于用户用法:参考

对话

// 创建普通对话LCIMConversationconversation=awaitclient.CreateConversation(newstring[]{"world"},name:name,unique:false);// 创建聊天室LCIMConversationchatroom=awaitclient.CreateChatRoom(name);// 创建临时对话LCIMConversationtempConversation=awaitclient.CreateTemporaryConversation(newstring[]{"world"});

更多关于对话用法:参考

消息

// 发送消息LCIMTextMessagetextMessage=newLCIMTextMessage("hello, world");awaitconversation.Send(textMessage);// 接收消息m2.OnMessage=(conv,msg)=>{if(msgisLCIMTextMessagetextMsg){WriteLine($"text: {textMsg.Text}");}};

更多关于对话用法:参考

排行榜

创建排行榜

LCLeaderboardleaderboard=awaitLCLeaderboard.CreateLeaderboard(leaderboardName);

更新成绩

awaitLCLeaderboard.UpdateStatistics(user,newDictionary<string,double>{{leaderboardName,100}});

获取成绩

LCUseruser=awaitLCUser.Login(username,password);ReadOnlyCollection<LCStatistic>statistics=awaitLCLeaderboard.GetStatistics(user);foreach(LCStatisticstatisticinstatistics){WriteLine($"{statistic.Name} : {statistic.Value}");}

获取我附近的成绩

awaitLCUser.Login(username,password);LCLeaderboardleaderboard=LCLeaderboard.CreateWithoutData(leaderboardName);ReadOnlyCollection<LCRanking>rankings=awaitleaderboard.GetResultsAroundUser(limit:5);foreach(LCRankingrankinginrankings){WriteLine($"{ranking.Rank} : {ranking.User.ObjectId}, {ranking.Value}");}

获取榜单

LCLeaderboardleaderboard=LCLeaderboard.CreateWithoutData(leaderboardName);ReadOnlyCollection<LCRanking>rankings=awaitleaderboard.GetResults();foreach(LCRankingrankinginrankings){WriteLine($"{ranking.Rank} : {ranking.User.ObjectId}, {ranking.Value}");}

LiveQuery

订阅

LCQuery<LCObject>query=newLCQuery<LCObject>("Account");query.WhereGreaterThan("balance",100);liveQuery=awaitquery.Subscribe();

事件

// 新建符合条件对象的事件liveQuery.OnCreate=(obj)=>{WriteLine($"create: {obj}");};// 符合条件对象更新事件liveQuery.OnUpdate=(obj,updatedKeys)=>{WriteLine($"update: {obj}");};// 符合条件对象被删除事件liveQuery.OnDelete=(objId)=>{WriteLine($"delete: {objId}");};// 有新的符合条件的对象事件liveQuery.OnEnter=(obj,updatedKeys)=>{WriteLine($"enter: {obj}");};// 有符合条件的对象不再满足条件事件liveQuery.OnLeave=(obj,updatedKeys)=>{WriteLine($"level: {obj}");};

特殊事件

当一个用户成功登录应用,OnLogin 事件会被触发。下面的 user 就是登录的 LCUser:

awaitLCUser.Login("hello","world");LCQuery<LCUser>userQuery=LCUser.GetQuery();userQuery.WhereEqualTo("username","hello");LCLiveQueryuserLiveQuery=awaituserQuery.Subscribe();userLiveQuery.OnLogin=(user)=>{WriteLine($"login: {user}");};

云引擎

脚手架工程

文档

API 文档

About

LeanCloud C# SDK

Resources

Stars

26 stars

Watchers

11 watching

Forks

Releases

Packages

Used by

Contributors

Languages