Skip to content

Commit

Permalink
Change OnAwake to OnStart, add IsValidGenericTypeDriver and StringToT…
Browse files Browse the repository at this point in the history
…ypeDriver
  • Loading branch information
Nytra committed Jul 11, 2024
1 parent 8d842fb commit 36c4005
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ private void Unsubscribe(Slot s)
s.ComponentRemoved -= OnSlotComponentRemoved;
}

protected override void OnAwake()
protected override void OnStart()
{
base.OnAwake();
base.OnStart();
_lastSlot = TargetSlot.Target;
if (_lastSlot != null)
{
Expand Down
28 changes: 28 additions & 0 deletions ProjectObsidian/Components/Utility/IsValidGenericTypeDriver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Elements.Core;
using FrooxEngine;
using System;
using System.Linq;

namespace Obsidian;

[Category(new string[] { "Obsidian/Utility" })]
public class IsValidGenericTypeDriver : Component
{
public readonly SyncType Type;

public readonly FieldDrive<bool> Target;

protected override void OnChanges()
{
base.OnChanges();
if (!Target.IsLinkValid) return;
if (Type.Value == null || !Type.Value.IsGenericType)
{
Target.Target.Value = false;
}
else
{
Target.Target.Value = Type.Value.IsValidGenericType(validForInstantiation: true);
}
}
}
36 changes: 36 additions & 0 deletions ProjectObsidian/Components/Utility/StringToTypeDriver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Elements.Core;
using FrooxEngine;
using System;

namespace Obsidian;

[Category(new string[] { "Obsidian/Utility" })]
public class StringToTypeDriver : Component
{
public readonly Sync<string> Text;

public readonly FieldDrive<Type> Target;

protected override void OnChanges()
{
base.OnChanges();
if (!Target.IsLinkValid) return;
if (string.IsNullOrWhiteSpace(Text.Value))
{
Target.Target.Value = null;
}
else
{
try
{
var parsedType = WorkerManager.ParseNiceType(Text.Value);
Target.Target.Value = parsedType;
}
catch (Exception ex)
{
UniLog.Warning("Exception when parsing type from string:\n" + ex.ToString());
Target.Target.Value = null;
}
}
}
}

0 comments on commit 36c4005

Please sign in to comment.