Skip to content

Commit 50edabe

Browse files
committed
initialize project
1 parent 6d21377 commit 50edabe

File tree

1,936 files changed

+198386
-4
lines changed

Some content is hidden

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

1,936 files changed

+198386
-4
lines changed

Nakama+Hiro/local.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ console:
22
max_message_size_bytes: 409600
33
leaderboard:
44
blacklist_rank_cache:
5-
- "*"
5+
- "*"
66
logger:
77
level: "DEBUG"
88
runtime:
99
env:
10-
- "ENV=dev1"
11-
- "HIRO_LICENSE="
10+
- "ENV=dev1"
11+
- "HIRO_LICENSE="
1212
session:
1313
token_expiry_sec: 86400 # 24 hours
1414
refresh_token_expiry_sec: 604800 # 7 days

Nakama+Hiro/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ func InitModule(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runti
6464
hiro.WithLeaderboardsSystem(fmt.Sprintf("definitions/%s/base-leaderboards.json", env), true),
6565
hiro.WithChallengesSystem(fmt.Sprintf("definitions/%s/base-challenges.json", env), true),
6666
hiro.WithEconomySystem(fmt.Sprintf("definitions/%s/base-economy.json", env), true),
67-
hiro.WithEventLeaderboardsSystem(fmt.Sprintf("definitions/%s/base-event-leaderboards.json", env), true))
67+
hiro.WithEventLeaderboardsSystem(fmt.Sprintf("definitions/%s/base-event-leaderboards.json", env), true),
68+
hiro.WithTeamsSystem(fmt.Sprintf("definitions/%s/base-teams.json", env), true))
6869
if err != nil {
6970
return err
7071
}

UnityHiroTeams/.gitignore

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
# Created by https://www.gitignore.io/api/unity
3+
# Edit at https://www.gitignore.io/?templates=unity
4+
5+
# Jetbrain Rider Cache
6+
.idea/
7+
Assets/Plugins/Editor/JetBrains*
8+
9+
# Visual Studio Code
10+
.vscode/
11+
12+
13+
### Unity ###
14+
/[Ll]ibrary/
15+
/[Tt]emp/
16+
/[Oo]bj/
17+
/[Bb]uild/
18+
/[Bb]uilds/
19+
/[Ll]ogs/
20+
/[Uu]ser[Ss]ettings/
21+
Assets/AssetStoreTools*
22+
# Unity local user project setting
23+
UserSettings/
24+
25+
# Visual Studio cache directory
26+
.vs/
27+
28+
# Autogenerated VS/MD/Consulo solution and project files
29+
ExportedObj/
30+
.consulo/
31+
*.csproj
32+
*.unityproj
33+
*.sln
34+
*.suo
35+
*.tmp
36+
*.user
37+
*.userprefs
38+
*.pidb
39+
*.booproj
40+
*.svd
41+
*.pdb
42+
*.opendb
43+
*.VC.db
44+
45+
# Unity3D generated meta files
46+
*.pidb.meta
47+
*.pdb.meta
48+
49+
# Unity3D Generated File On Crash Reports
50+
sysinfo.txt
51+
52+
# Builds
53+
*.apk
54+
*.unitypackage
55+
56+
# End of https://www.gitignore.io/api/unity

UnityHiroTeams/Assets/UnityHiroEventLeaderboards.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.

UnityHiroTeams/Assets/UnityHiroTeams.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.

UnityHiroTeams/Assets/UnityHiroTeams/Editor.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.
Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
// Copyright 2025 The Nakama Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using System;
16+
using System.Collections.Generic;
17+
using System.Text;
18+
using Nakama;
19+
using Hiro;
20+
using Hiro.Unity;
21+
using UnityEditor;
22+
using UnityEngine;
23+
using UnityEngine.SceneManagement;
24+
using UnityEngine.UIElements;
25+
26+
namespace HiroTeams.Editor
27+
{
28+
public class AccountSwitcherEditor : EditorWindow
29+
{
30+
[SerializeField] private VisualTreeAsset tree;
31+
32+
private DropdownField accountDropdown;
33+
private Label usernamesLabel;
34+
35+
private readonly SortedDictionary<string, string> accountUsernames = new();
36+
37+
private const string AccountUsernamesKey = "AccountSwitcher_Usernames";
38+
39+
[MenuItem("Tools/Nakama/Account Switcher")]
40+
public static void ShowWindow()
41+
{
42+
var inspectorType = typeof(UnityEditor.Editor).Assembly.GetType("UnityEditor.InspectorWindow");
43+
var window = GetWindow<AccountSwitcherEditor>("Account Switcher", inspectorType);
44+
45+
window.Focus();
46+
}
47+
48+
[MenuItem("Tools/Nakama/Clear Test Accounts")]
49+
public static void ClearSavedAccounts()
50+
{
51+
EditorPrefs.DeleteKey(AccountUsernamesKey);
52+
Debug.Log("Cleared all saved account usernames");
53+
54+
// Refresh any open Account Switcher windows
55+
var windows = Resources.FindObjectsOfTypeAll<AccountSwitcherEditor>();
56+
foreach (var window in windows)
57+
{
58+
window.accountUsernames.Clear();
59+
window.UpdateUsernameLabels();
60+
}
61+
}
62+
63+
private void CreateGUI()
64+
{
65+
tree.CloneTree(rootVisualElement);
66+
67+
accountDropdown = rootVisualElement.Q<DropdownField>("account-dropdown");
68+
accountDropdown.RegisterValueChangedCallback(SwitchAccount);
69+
70+
usernamesLabel = rootVisualElement.Q<Label>("usernames");
71+
72+
// Load saved usernames on startup
73+
LoadAccountUsernames();
74+
UpdateUsernameLabels();
75+
76+
if (!EditorApplication.isPlaying) return;
77+
78+
var rootGameObjects = SceneManager.GetActiveScene().GetRootGameObjects();
79+
foreach (var rootGameObject in rootGameObjects)
80+
{
81+
if (!rootGameObject.TryGetComponent<HiroTeamsController>(out var teamsController)) continue;
82+
83+
if (HiroCoordinator.Instance.GetSystem<NakamaSystem>().Session is Session session)
84+
{
85+
OnControllerInitialized(session);
86+
}
87+
else
88+
{
89+
teamsController.OnInitialized += OnControllerInitialized;
90+
}
91+
}
92+
}
93+
94+
private void OnControllerInitialized(ISession session, HiroTeamsController teamsController = null)
95+
{
96+
accountUsernames[accountDropdown.choices[0]] = session.Username;
97+
UpdateUsernameLabels();
98+
99+
if (teamsController != null)
100+
{
101+
teamsController.OnInitialized -= OnControllerInitialized;
102+
}
103+
}
104+
105+
private void LoadAccountUsernames()
106+
{
107+
var savedUsernames = EditorPrefs.GetString(AccountUsernamesKey, "");
108+
if (string.IsNullOrEmpty(savedUsernames)) return;
109+
110+
try
111+
{
112+
var usernameData = JsonUtility.FromJson<SerializableStringDictionary>(savedUsernames);
113+
accountUsernames.Clear();
114+
115+
foreach (var item in usernameData.items)
116+
{
117+
accountUsernames[item.key] = item.value;
118+
}
119+
}
120+
catch (Exception ex)
121+
{
122+
Debug.LogWarning($"Failed to load saved account usernames: {ex.Message}");
123+
}
124+
}
125+
126+
private void SaveAccountUsernames()
127+
{
128+
try
129+
{
130+
var usernameData = new SerializableStringDictionary();
131+
foreach (var kvp in accountUsernames)
132+
{
133+
usernameData.items.Add(new SerializableKeyValuePair { key = kvp.Key, value = kvp.Value });
134+
}
135+
136+
var json = JsonUtility.ToJson(usernameData);
137+
EditorPrefs.SetString(AccountUsernamesKey, json);
138+
}
139+
catch (Exception ex)
140+
{
141+
Debug.LogWarning($"Failed to save account usernames: {ex.Message}");
142+
}
143+
}
144+
145+
private async void SwitchAccount(ChangeEvent<string> changeEvt)
146+
{
147+
if (!EditorApplication.isPlaying) return;
148+
149+
var previousValue = changeEvt.previousValue;
150+
var newValue = changeEvt.newValue;
151+
152+
var rootGameObjects = SceneManager.GetActiveScene().GetRootGameObjects();
153+
foreach (var rootGameObject in rootGameObjects)
154+
{
155+
if (!rootGameObject.TryGetComponent<HiroTeamsController>(out var teamsController)) continue;
156+
157+
var coordinator = HiroCoordinator.Instance as HiroTeamsCoordinator;
158+
if (coordinator == null) return;
159+
var nakamaSystem = coordinator.GetSystem<NakamaSystem>();
160+
161+
// Save username before switching
162+
if (!string.IsNullOrEmpty(previousValue))
163+
{
164+
accountUsernames[previousValue] = nakamaSystem.Session.Username;
165+
}
166+
167+
try
168+
{
169+
var newSession = await HiroTeamsCoordinator.NakamaAuthorizerFunc(accountDropdown.index)
170+
.Invoke(nakamaSystem.Client);
171+
(nakamaSystem.Session as Session)?.Update(newSession.AuthToken, newSession.RefreshToken);
172+
await nakamaSystem.RefreshAsync();
173+
accountUsernames[newValue] = newSession.Username;
174+
teamsController.SwitchComplete(newSession);
175+
176+
SaveAccountUsernames();
177+
break;
178+
}
179+
catch (ApiResponseException e)
180+
{
181+
Debug.LogWarning($"Error authenticating with Device ID: {e.Message}");
182+
return;
183+
}
184+
}
185+
186+
UpdateUsernameLabels();
187+
}
188+
189+
private void UpdateUsernameLabels()
190+
{
191+
var sb = new StringBuilder();
192+
var index = 1;
193+
194+
foreach (var kvp in accountUsernames)
195+
{
196+
sb.Append(index);
197+
sb.Append(": ");
198+
sb.Append(kvp.Value);
199+
sb.AppendLine();
200+
index++;
201+
}
202+
203+
usernamesLabel.text = sb.ToString();
204+
}
205+
206+
[Serializable]
207+
private class SerializableStringDictionary
208+
{
209+
public List<SerializableKeyValuePair> items = new();
210+
}
211+
212+
[Serializable]
213+
private class SerializableKeyValuePair
214+
{
215+
public string key;
216+
public string value;
217+
}
218+
}
219+
}

UnityHiroTeams/Assets/UnityHiroTeams/Editor/AccountSwitcher.cs.meta

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<ui:UXML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" noNamespaceSchemaLocation="../../UIElementsSchema/UIElements.xsd" editor-extension-mode="True">
2+
<ui:VisualElement style="flex-grow: 1; padding-top: 8px; padding-right: 8px; padding-bottom: 0; padding-left: 8px; align-items: stretch; justify-content: flex-start;">
3+
<ui:Label text="Nakama Account Switcher" style="-unity-text-align: upper-center; -unity-font-style: normal; font-size: 24px; margin-bottom: 4px; margin-top: 8px; -unity-font-definition: url(&quot;project://database/Assets/UnityNakamaFriends/HeroicUI/Fonts/Plus_Jakarta_Sans/static/PlusJakartaSans-Bold.ttf?fileID=12800000&amp;guid=b0eab255566194e3c923ef0f1d4571da&amp;type=3#PlusJakartaSans-Bold&quot;); white-space: normal;" />
4+
<ui:Label text="The Account Switcher lets you explore the project as different users without managing multiple builds.&#10;&#10;Use the dropdown to switch between up to four different test accounts. An account will be automatically created the first time you select it.&#10;&#10;Note: Account switching only works during play mode." style="white-space: normal; -unity-font-definition: url(&quot;project://database/Assets/UnityNakamaFriends/HeroicUI/Fonts/Plus_Jakarta_Sans/PlusJakartaSans-VariableFont_wght.ttf?fileID=12800000&amp;guid=fbe3b99011ddc4d419b127c34c0e03ab&amp;type=3#PlusJakartaSans-VariableFont_wght&quot;);" />
5+
<ui:VisualElement style="margin-top: 8px;" />
6+
<ui:DropdownField label="Choose account:" name="account-dropdown" index="0" choices="ACCOUNT 1,ACCOUNT 2,ACCOUNT 3,ACCOUNT 4" style="-unity-font-definition: url(&quot;project://database/Assets/UnityNakamaFriends/HeroicUI/Fonts/Plus_Jakarta_Sans/PlusJakartaSans-VariableFont_wght.ttf?fileID=12800000&amp;guid=fbe3b99011ddc4d419b127c34c0e03ab&amp;type=3#PlusJakartaSans-VariableFont_wght&quot;);" />
7+
<ui:VisualElement name="VisualElement" style="flex-grow: 1; height: 8px; align-items: center; justify-content: flex-start; align-self: flex-start; align-content: flex-start; margin-top: 8px;">
8+
<ui:Label text="Account Usernames" style="-unity-text-align: upper-center; -unity-font-style: bold; white-space: pre; -unity-font-definition: url(&quot;project://database/Assets/UnityNakamaFriends/HeroicUI/Fonts/Plus_Jakarta_Sans/PlusJakartaSans-VariableFont_wght.ttf?fileID=12800000&amp;guid=fbe3b99011ddc4d419b127c34c0e03ab&amp;type=3#PlusJakartaSans-VariableFont_wght&quot;);" />
9+
<ui:Label name="usernames" selectable="true" style="align-self: flex-start;" />
10+
</ui:VisualElement>
11+
</ui:VisualElement>
12+
</ui:UXML>

UnityHiroTeams/Assets/UnityHiroTeams/Editor/AccountSwitcher.uxml.meta

Lines changed: 10 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)