Skip to content

Commit 11b43b9

Browse files
adapt samples for Unity, add PubnubUnityUtils.NewUnityPubnub() utility method
1 parent 9770ef1 commit 11b43b9

File tree

63 files changed

+1280
-594
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+1280
-594
lines changed

PubNubUnity/Assets/PubNub/Runtime/Util/PNManagerBehaviour.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,10 @@ public Pubnub Initialize(string userId) {
3131
return pubnub;
3232
}
3333

34-
pnConfiguration.UserId = userId;
35-
var pnConfig = ((PNConfiguration)pnConfiguration);
36-
pubnub = pnConfiguration.EnableWebGLBuildMode
37-
? new Pubnub(pnConfig, httpTransportService: new UnityWebGLHttpClientService(),
38-
ipnsdkSource: new UnityPNSDKSource())
39-
: new Pubnub(pnConfig, ipnsdkSource: new UnityPNSDKSource());
40-
if (pnConfiguration.LogToUnityConsole) {
41-
pubnub.SetLogger(new UnityPubNubLogger(pubnub.InstanceId));
42-
}
43-
pubnub.SetJsonPluggableLibrary(new NewtonsoftJsonUnity(pnConfig));
34+
pubnub = PubnubUnityUtils.NewUnityPubnub(pnConfiguration, userId);
4435
pubnub.AddListener(listener);
4536
return pubnub;
37+
4638
}
4739

4840
protected virtual void OnDestroy() {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace PubnubApi.Unity {
2+
public static class PubnubUnityUtils {
3+
/// <summary>
4+
/// Creates a new Pubnub instance with Unity specific settings (like WebGL setup, logging, and JSON library)
5+
/// </summary>
6+
/// <param name="configuration">Pubnub configuration object</param>
7+
/// <param name="webGLBuildMode">Flag for enabling WebGL mode - sets httpTransportService to UnityWebGLHttpClientService</param>
8+
/// <param name="unityLogging">Flag to set Unity specific logger (UnityPubNubLogger)</param>
9+
/// <returns></returns>
10+
public static Pubnub NewUnityPubnub(PNConfiguration configuration, bool webGLBuildMode = false, bool unityLogging = false) {
11+
var pubnub = webGLBuildMode
12+
? new Pubnub(configuration, httpTransportService: new UnityWebGLHttpClientService(),
13+
ipnsdkSource: new UnityPNSDKSource())
14+
: new Pubnub(configuration, ipnsdkSource: new UnityPNSDKSource());
15+
if (unityLogging) {
16+
pubnub.SetLogger(new UnityPubNubLogger(pubnub.InstanceId));
17+
}
18+
pubnub.SetJsonPluggableLibrary(new NewtonsoftJsonUnity(configuration));
19+
return pubnub;
20+
}
21+
22+
/// <summary>
23+
/// Creates a new Pubnub instance with Unity specific settings (like WebGL setup, logging, and JSON library)
24+
/// </summary>
25+
/// <param name="configurationAsset">Pubnub configuration Scriptable Object asset</param>
26+
/// <param name="userId">Client user ID</param>
27+
/// <returns></returns>
28+
public static Pubnub NewUnityPubnub(PNConfigAsset configurationAsset, string userId) {
29+
configurationAsset.UserId = userId;
30+
var pnConfig = ((PNConfiguration)configurationAsset);
31+
return NewUnityPubnub(pnConfig, configurationAsset.EnableWebGLBuildMode, configurationAsset.LogToUnityConsole);
32+
}
33+
}
34+
}

PubNubUnity/Assets/PubNub/Runtime/Util/PubnubUnityUtils.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

PubNubUnity/Assets/PubNub/Snippets/AccessManager.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

PubNubUnity/Assets/PubNub/Snippets/AccessManager/AccessManagerSample.cs

Lines changed: 41 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
// snippet.using
22
using PubnubApi;
3+
using PubnubApi.Unity;
34

45
// snippet.end
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Threading.Tasks;
9+
10+
using UnityEngine;
511

612
class AccessManagerSample
713
{
@@ -17,46 +23,15 @@ static void PubnubInit()
1723
PublishKey = "demo"
1824
};
1925
//Create a new PubNub instance
20-
Pubnub pubnub = new Pubnub(pnConfiguration);
21-
22-
// snippet.end
23-
}
24-
25-
static async Task BasicUsage()
26-
{
27-
// snippet.basic_usage
28-
try
29-
{
30-
//Perform token granting operation
31-
PNResult<PNAccessManagerTokenResult> grantTokenResponse = await pubnub.GrantToken()
32-
.TTL(15)
33-
.AuthorizedUuid("my-authorized-uuid")
34-
.Resources(new PNTokenResources
35-
{
36-
Channels = new Dictionary<string, PNTokenAuthValues>
37-
{
38-
{ "my-channel", new PNTokenAuthValues { Read = true, Write = true } }
39-
}
40-
})
41-
.ExecuteAsync();
26+
Pubnub pubnub = PubnubUnityUtils.NewUnityPubnub(pnConfiguration);
4227

43-
//Parse operation response
44-
PNAccessManagerTokenResult grantTokenResult = grantTokenResponse.Result;
45-
PNStatus grantTokenStatus = grantTokenResponse.Status;
28+
// If you're using Unity Editor setup you can get the Pubnub instance from PNManagerBehaviour
29+
// For more details, see https://www.pubnub.com/docs/sdks/unity#configure-pubnub
30+
/*
31+
[SerializeField] private PNManagerBehaviour pubnubManager;
32+
Pubnub pubnub = pubnubManager.pubnub;
33+
*/
4634

47-
if (!grantTokenStatus.Error && grantTokenResult != null)
48-
{
49-
Console.WriteLine(pubnub.JsonPluggableLibrary.SerializeToJsonString(grantTokenResult));
50-
}
51-
else
52-
{
53-
Console.WriteLine(pubnub.JsonPluggableLibrary.SerializeToJsonString(grantTokenStatus));
54-
}
55-
}
56-
catch (Exception ex)
57-
{
58-
Console.WriteLine($"Request cannot be executed due to error: {ex.Message}");
59-
}
6035
// snippet.end
6136
}
6237

@@ -73,9 +48,9 @@ static async Task GrantTokenComplex()
7348
{ "channel-b", new PNTokenAuthValues() { Read = true, Write = true } },
7449
{ "channel-c", new PNTokenAuthValues() { Read = true, Write = true } },
7550
{ "channel-d", new PNTokenAuthValues() { Read = true, Write = true } }},
76-
ChannelGroups = new Dictionary<string, PNTokenAuthValues>() {
51+
ChannelGroups = new Dictionary<string, PNTokenAuthValues>() {
7752
{ "channel-group-b", new PNTokenAuthValues() { Read = true } } },
78-
Uuids = new Dictionary<string, PNTokenAuthValues>() {
53+
Uuids = new Dictionary<string, PNTokenAuthValues>() {
7954
{ "uuid-c", new PNTokenAuthValues() { Get = true } },
8055
{ "uuid-d", new PNTokenAuthValues() { Get = true, Update = true } }}
8156
})
@@ -84,15 +59,15 @@ static async Task GrantTokenComplex()
8459
PNStatus grantTokenStatus = grantTokenResponse.Status;
8560
if (!grantTokenStatus.Error && grantTokenResult != null)
8661
{
87-
Console.WriteLine(pubnub.JsonPluggableLibrary.SerializeToJsonString(grantTokenResult));
62+
Debug.Log(pubnub.JsonPluggableLibrary.SerializeToJsonString(grantTokenResult));
8863
}
8964
else
9065
{
91-
Console.WriteLine(pubnub.JsonPluggableLibrary.SerializeToJsonString(grantTokenStatus));
66+
Debug.Log(pubnub.JsonPluggableLibrary.SerializeToJsonString(grantTokenStatus));
9267
}
9368
// snippet.end
9469
}
95-
70+
9671
static async Task GrantTokenWithRegex()
9772
{
9873
// snippet.grant_token_regex
@@ -109,15 +84,15 @@ static async Task GrantTokenWithRegex()
10984
PNStatus grantTokenStatus = grantTokenResponse.Status;
11085
if (!grantTokenStatus.Error && grantTokenResult != null)
11186
{
112-
Console.WriteLine(pubnub.JsonPluggableLibrary.SerializeToJsonString(grantTokenResult));
87+
Debug.Log(pubnub.JsonPluggableLibrary.SerializeToJsonString(grantTokenResult));
11388
}
11489
else
11590
{
116-
Console.WriteLine(pubnub.JsonPluggableLibrary.SerializeToJsonString(grantTokenStatus));
91+
Debug.Log(pubnub.JsonPluggableLibrary.SerializeToJsonString(grantTokenStatus));
11792
}
11893
// snippet.end
11994
}
120-
95+
12196
static async Task GrantTokenComplexWithRegex()
12297
{
12398
// snippet.grant_token_complex_with_regex
@@ -147,41 +122,41 @@ static async Task GrantTokenComplexWithRegex()
147122
PNStatus grantTokenStatus = grantTokenResponse.Status;
148123
if (!grantTokenStatus.Error && grantTokenResult != null)
149124
{
150-
Console.WriteLine(pubnub.JsonPluggableLibrary.SerializeToJsonString(grantTokenResult));
125+
Debug.Log(pubnub.JsonPluggableLibrary.SerializeToJsonString(grantTokenResult));
151126
}
152127
else
153128
{
154-
Console.WriteLine(pubnub.JsonPluggableLibrary.SerializeToJsonString(grantTokenStatus));
129+
Debug.Log(pubnub.JsonPluggableLibrary.SerializeToJsonString(grantTokenStatus));
155130
}
156131
// snippet.end
157132
}
158-
133+
159134
static async Task GrantTokenOldBasicUsage()
160135
{
161136
// snippet.basic_usage_old
162137
PNResult<PNAccessManagerTokenResult> grantTokenResponse = await pubnub.GrantToken()
163138
.TTL(15)
164139
.AuthorizedUserId("my-authorized-userId")
165-
.Resources(new PNTokenResources()
140+
.Resources(new PNTokenResources()
166141
{
167142
Spaces = new Dictionary<string, PNTokenAuthValues>() {
168143
{ "my-space", new PNTokenAuthValues() { Read = true } } } // False to disallow
169-
})
144+
})
170145
.ExecuteAsync();
171146
PNAccessManagerTokenResult grantTokenResult = grantTokenResponse.Result;
172147
PNStatus grantTokenStatus = grantTokenResponse.Status;
173148
//PNAccessManagerTokenResult is a parsed and abstracted response from the server
174149
if (!grantTokenStatus.Error && grantTokenResult != null)
175150
{
176-
Console.WriteLine(pubnub.JsonPluggableLibrary.SerializeToJsonString(grantTokenResult));
151+
Debug.Log(pubnub.JsonPluggableLibrary.SerializeToJsonString(grantTokenResult));
177152
}
178153
else
179154
{
180-
Console.WriteLine(pubnub.JsonPluggableLibrary.SerializeToJsonString(grantTokenStatus));
155+
Debug.Log(pubnub.JsonPluggableLibrary.SerializeToJsonString(grantTokenStatus));
181156
}
182157
// snippet.end
183158
}
184-
159+
185160
static async Task GrantTokenOldComplex()
186161
{
187162
// snippet.grant_token_complex_old
@@ -195,7 +170,7 @@ static async Task GrantTokenOldComplex()
195170
{ "space-b", new PNTokenAuthValues() { Read = true, Write = true } },
196171
{ "space-c", new PNTokenAuthValues() { Read = true, Write = true } },
197172
{ "space-d", new PNTokenAuthValues() { Read = true, Write = true } }},
198-
Users = new Dictionary<string, PNTokenAuthValues>() {
173+
Users = new Dictionary<string, PNTokenAuthValues>() {
199174
{ "user-c", new PNTokenAuthValues() { Get = true } },
200175
{ "user-d", new PNTokenAuthValues() { Get = true, Update = true } }}
201176
})
@@ -204,15 +179,15 @@ static async Task GrantTokenOldComplex()
204179
PNStatus grantTokenStatus = grantTokenResponse.Status;
205180
if (!grantTokenStatus.Error && grantTokenResult != null)
206181
{
207-
Console.WriteLine(pubnub.JsonPluggableLibrary.SerializeToJsonString(grantTokenResult));
182+
Debug.Log(pubnub.JsonPluggableLibrary.SerializeToJsonString(grantTokenResult));
208183
}
209184
else
210185
{
211-
Console.WriteLine(pubnub.JsonPluggableLibrary.SerializeToJsonString(grantTokenStatus));
186+
Debug.Log(pubnub.JsonPluggableLibrary.SerializeToJsonString(grantTokenStatus));
212187
}
213188
// snippet.end
214189
}
215-
190+
216191
static async Task GrantTokenOldWithRegex()
217192
{
218193
// snippet.grant_token_old_regex
@@ -229,15 +204,15 @@ static async Task GrantTokenOldWithRegex()
229204
PNStatus grantTokenStatus = grantTokenResponse.Status;
230205
if (!grantTokenStatus.Error && grantTokenResult != null)
231206
{
232-
Console.WriteLine(pubnub.JsonPluggableLibrary.SerializeToJsonString(grantTokenResult));
207+
Debug.Log(pubnub.JsonPluggableLibrary.SerializeToJsonString(grantTokenResult));
233208
}
234209
else
235210
{
236-
Console.WriteLine(pubnub.JsonPluggableLibrary.SerializeToJsonString(grantTokenStatus));
211+
Debug.Log(pubnub.JsonPluggableLibrary.SerializeToJsonString(grantTokenStatus));
237212
}
238213
// snippet.end
239214
}
240-
215+
241216
static async Task GrantTokenOldComplexWithRegex()
242217
{
243218
// snippet.grant_token_complex_old_with_regex
@@ -265,15 +240,15 @@ static async Task GrantTokenOldComplexWithRegex()
265240
PNStatus grantTokenStatus = grantTokenResponse.Status;
266241
if (!grantTokenStatus.Error && grantTokenResult != null)
267242
{
268-
Console.WriteLine(pubnub.JsonPluggableLibrary.SerializeToJsonString(grantTokenResult));
243+
Debug.Log(pubnub.JsonPluggableLibrary.SerializeToJsonString(grantTokenResult));
269244
}
270245
else
271246
{
272-
Console.WriteLine(pubnub.JsonPluggableLibrary.SerializeToJsonString(grantTokenStatus));
247+
Debug.Log(pubnub.JsonPluggableLibrary.SerializeToJsonString(grantTokenStatus));
273248
}
274249
// snippet.end
275250
}
276-
251+
277252
static async Task RevokeTokenBasicUsage()
278253
{
279254
// snippet.revoke_token
@@ -285,11 +260,11 @@ static async Task RevokeTokenBasicUsage()
285260
PNStatus revokeTokenStatus = revokeTokenResponse.Status;
286261
if (!revokeTokenStatus.Error && revokeTokenResult != null)
287262
{
288-
Console.WriteLine("Revoke token success");
263+
Debug.Log("Revoke token success");
289264
}
290265
else
291266
{
292-
Console.WriteLine(pubnub.JsonPluggableLibrary.SerializeToJsonString(revokeTokenStatus));
267+
Debug.Log(pubnub.JsonPluggableLibrary.SerializeToJsonString(revokeTokenStatus));
293268
}
294269
// snippet.end
295270
}

PubNubUnity/Assets/PubNub/Snippets/AccessManager/AccessManagerSample.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)