Skip to content

Commit 03cfdf1

Browse files
committed
Add option to auto-instantiate TSScheduler
1 parent a67e8d9 commit 03cfdf1

File tree

7 files changed

+62
-2
lines changed

7 files changed

+62
-2
lines changed

Lib/TSSettings.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using UnityEngine;
2+
3+
namespace TS
4+
{
5+
public class TSSettings : ScriptableObject
6+
{
7+
[SerializeField]private bool _instantiateOnLoad = true;
8+
9+
public bool InstantiateOnLoad
10+
{
11+
get { return _instantiateOnLoad; }
12+
}
13+
}
14+
}

Lib/TSSettings.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.

Lib/TSTimeDef.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public float Progress
2727
}
2828
return (now - startTime) / duration ;
2929
}
30-
set {
30+
set {
3131
float now = Time.realtimeSinceStartup;
3232
startTime = now - value * duration;
3333
Update(now);

Resources.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.

Resources/TSSettings.asset

4.08 KB
Binary file not shown.

Resources/TSSettings.asset.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.

TSScheduler.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace TS
77
public class TSScheduler : MonoBehaviour
88
{
99
private static TSScheduler instance;
10+
private static TSSettings settings;
1011

1112
private List<TSTimeDef> tweens;
1213

@@ -50,7 +51,29 @@ public static void KillAllDelayedCallsTo(Action<object> callback)
5051
}
5152
}
5253

53-
void Awake()
54+
[RuntimeInitializeOnLoadMethod(loadType: RuntimeInitializeLoadType.BeforeSceneLoad)]
55+
static void InitializeOnLoad() {
56+
if (Settings.InstantiateOnLoad)
57+
{
58+
GameObject gameObject = new GameObject("TweenSharp");
59+
gameObject.AddComponent<TSScheduler>();
60+
}
61+
}
62+
63+
private static TSSettings Settings
64+
{
65+
get
66+
{
67+
if (settings == null)
68+
{
69+
settings = Resources.Load<TSSettings>("TSSettings");
70+
}
71+
72+
return settings;
73+
}
74+
}
75+
76+
private void Awake()
5477
{
5578
if (instance == null)
5679
{
@@ -60,6 +83,10 @@ void Awake()
6083

6184
TSKeywordParser.Init();
6285
}
86+
else
87+
{
88+
throw new Exception("TSScheduler is a singleton, but instatiated twice. Make sure only one copy exists, and consider disabling InstantiateOnLoad in TweenSharp settings.");
89+
}
6390
}
6491

6592
private void Update()

0 commit comments

Comments
 (0)