Skip to content

Commit 6895bef

Browse files
committed
Add InitializeOnStart option to ThirdwebManager
Introduces a new InitializeOnStart serialized property to ThirdwebManagerBase and its editor, allowing initialization to occur in Start() instead of Awake().
1 parent 903d6e3 commit 6895bef

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

Assets/Thirdweb/Editor/ThirdwebManagerEditor.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public abstract class ThirdwebManagerBaseEditor<T> : UnityEditor.Editor
1010
where T : MonoBehaviour
1111
{
1212
protected SerializedProperty InitializeOnAwakeProp;
13+
protected SerializedProperty InitializeOnStartProp;
1314
protected SerializedProperty ShowDebugLogsProp;
1415
protected SerializedProperty AutoConnectLastWalletProp;
1516
protected SerializedProperty RedirectPageHtmlOverrideProp;
@@ -24,6 +25,7 @@ public abstract class ThirdwebManagerBaseEditor<T> : UnityEditor.Editor
2425
protected virtual void OnEnable()
2526
{
2627
this.InitializeOnAwakeProp = this.FindProp("InitializeOnAwake");
28+
this.InitializeOnStartProp = this.FindProp("InitializeOnStart");
2729
this.ShowDebugLogsProp = this.FindProp("ShowDebugLogs");
2830
this.AutoConnectLastWalletProp = this.FindProp("AutoConnectLastWallet");
2931
this.RedirectPageHtmlOverrideProp = this.FindProp("RedirectPageHtmlOverride");
@@ -77,6 +79,7 @@ protected virtual void DrawPreferencesTab()
7779
{
7880
EditorGUILayout.HelpBox("Set your preferences and initialization options here.", MessageType.Info);
7981
this.DrawProperty(this.InitializeOnAwakeProp, "Initialize On Awake");
82+
this.DrawProperty(this.InitializeOnStartProp, "Initialize On Start");
8083
this.DrawProperty(this.ShowDebugLogsProp, "Show Debug Logs");
8184
this.DrawProperty(this.AutoConnectLastWalletProp, "Auto-Connect Last Wallet");
8285
}

Assets/Thirdweb/Runtime/Unity/ThirdwebManager.cs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,40 +12,37 @@ public class ThirdwebManager : ThirdwebManagerBase
1212
[field: SerializeField]
1313
private string BundleId { get; set; }
1414

15-
public static new ThirdwebManager Instance
16-
{
17-
get => ThirdwebManagerBase.Instance as ThirdwebManager;
18-
}
15+
public static new ThirdwebManager Instance => ThirdwebManagerBase.Instance as ThirdwebManager;
1916

2017
protected override ThirdwebClient CreateClient()
2118
{
22-
if (string.IsNullOrWhiteSpace(ClientId))
19+
if (string.IsNullOrWhiteSpace(this.ClientId))
2320
{
2421
ThirdwebDebug.LogError("ClientId must be set in order to initialize ThirdwebManager. " + "Get your API key from https://thirdweb.com/create-api-key");
2522
return null;
2623
}
2724

28-
if (string.IsNullOrWhiteSpace(BundleId))
25+
if (string.IsNullOrWhiteSpace(this.BundleId))
2926
{
30-
BundleId = null;
27+
this.BundleId = null;
3128
}
3229

33-
BundleId ??= string.IsNullOrWhiteSpace(Application.identifier) ? $"com.{Application.companyName}.{Application.productName}" : Application.identifier;
30+
this.BundleId ??= string.IsNullOrWhiteSpace(Application.identifier) ? $"com.{Application.companyName}.{Application.productName}" : Application.identifier;
3431

3532
return ThirdwebClient.Create(
36-
clientId: ClientId,
37-
bundleId: BundleId,
33+
clientId: this.ClientId,
34+
bundleId: this.BundleId,
3835
httpClient: new CrossPlatformUnityHttpClient(),
3936
sdkName: Application.platform == RuntimePlatform.WebGLPlayer ? "UnitySDK_WebGL" : "UnitySDK",
4037
sdkOs: Application.platform.ToString(),
4138
sdkPlatform: "unity",
4239
sdkVersion: THIRDWEB_UNITY_SDK_VERSION,
43-
rpcOverrides: (RpcOverrides == null || RpcOverrides.Count == 0)
40+
rpcOverrides: (this.RpcOverrides == null || this.RpcOverrides.Count == 0)
4441
? null
45-
: RpcOverrides.ToDictionary(rpcOverride => new BigInteger(rpcOverride.ChainId), rpcOverride => rpcOverride.RpcUrl)
42+
: this.RpcOverrides.ToDictionary(rpcOverride => new BigInteger(rpcOverride.ChainId), rpcOverride => rpcOverride.RpcUrl)
4643
);
4744
}
4845

49-
public override string MobileRedirectScheme => BundleId + "://";
46+
public override string MobileRedirectScheme => this.BundleId + "://";
5047
}
5148
}

Assets/Thirdweb/Runtime/Unity/ThirdwebManagerBase.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,9 @@ public abstract class ThirdwebManagerBase : MonoBehaviour
266266
[field: SerializeField]
267267
protected bool InitializeOnAwake { get; set; } = true;
268268

269+
[field: SerializeField]
270+
protected bool InitializeOnStart { get; set; } = false;
271+
269272
[field: SerializeField]
270273
protected bool ShowDebugLogs { get; set; } = true;
271274

@@ -339,8 +342,22 @@ protected virtual void Awake()
339342
}
340343
}
341344

345+
protected virtual void Start()
346+
{
347+
if (this.InitializeOnStart)
348+
{
349+
this.Initialize();
350+
}
351+
}
352+
342353
public virtual async void Initialize()
343354
{
355+
if (this.Initialized || this.Client != null)
356+
{
357+
ThirdwebDebug.LogWarning("ThirdwebManager is already initialized.");
358+
return;
359+
}
360+
344361
this.Client = this.CreateClient();
345362
if (this.Client == null)
346363
{

0 commit comments

Comments
 (0)