-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
- Loading branch information
There are no files selected for viewing
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* NatML | ||
* Copyright © 2023 NatML Inc. All rights reserved. | ||
*/ | ||
|
||
namespace NatML.Editor { | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using UnityEditor; | ||
using UnityEditor.Build; | ||
using UnityEditor.Build.Reporting; | ||
using UnityEngine; | ||
|
||
/// <summary> | ||
/// Lightweight utility for embedding scriptable objects into builds. | ||
/// </summary> | ||
public abstract class BuildEmbedHelper<T> : IPreprocessBuildWithReport, IPostprocessBuildWithReport where T : ScriptableObject { | ||
|
||
#region --Client API-- | ||
/// <summary> | ||
/// Callback order for prioritization. | ||
/// </summary> | ||
public virtual int callbackOrder => 0; | ||
|
||
/// <summary> | ||
/// Supported targets for this build embed. | ||
/// If `null` the helper will embed for all build targets. | ||
/// </summary> | ||
protected virtual BuildTarget[] SupportedTargets => null; | ||
|
||
/// <summary> | ||
/// Create embeds for the build. | ||
/// </summary> | ||
protected abstract T[] CreateEmbeds (BuildReport report); | ||
|
||
/// <summary> | ||
/// Clear embeds after the build. | ||
/// </summary> | ||
protected virtual void ClearEmbeds (BuildReport report) => ClearEmbeds<T>(); | ||
|
||
/// <summary> | ||
/// Clear any existing data embedded in the build. | ||
/// </summary> | ||
protected static void ClearEmbeds<U> () { | ||
var assets = PlayerSettings.GetPreloadedAssets()?.ToList(); | ||
if (assets == null) | ||
return; | ||
assets.RemoveAll(asset => asset && asset.GetType() == typeof(U)); | ||
PlayerSettings.SetPreloadedAssets(assets.ToArray()); | ||
} | ||
#endregion | ||
|
||
|
||
#region --Operations-- | ||
|
||
void IPreprocessBuildWithReport.OnPreprocessBuild (BuildReport report) { | ||
// Clear | ||
ClearEmbeds<T>(); | ||
// Check target | ||
var targets = SupportedTargets; | ||
if (!targets?.Contains(report.summary.platform) ?? false) | ||
return; | ||
// Create | ||
var data = CreateEmbeds(report); | ||
if (data == null) | ||
return; | ||
// Embed | ||
var assets = PlayerSettings.GetPreloadedAssets()?.ToList() ?? new List<UnityEngine.Object>(); | ||
assets.AddRange(data); | ||
PlayerSettings.SetPreloadedAssets(assets.ToArray()); | ||
} | ||
|
||
void IPostprocessBuildWithReport.OnPostprocessBuild (BuildReport report) => ClearEmbeds(report); | ||
#endregion | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
* NatML | ||
* Copyright © 2023 NatML Inc. All rights reserved. | ||
*/ | ||
|
||
namespace NatML.Editor.Importers { | ||
|
||
using UnityEditor.AssetImporters; | ||
using API.Types; | ||
|
||
[ScriptedImporter(1, @"mlmodel")] | ||
internal sealed class CoreMLImporter : GraphImporter { | ||
|
||
protected override PredictorSession CreateSession () => new PredictorSession { | ||
format = GraphFormat.CoreML | ||
}; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* NatML | ||
* Copyright © 2023 NatML Inc. All rights reserved. | ||
*/ | ||
|
||
namespace NatML.Editor.Importers { | ||
|
||
using System.IO; | ||
using UnityEngine; | ||
using UnityEditor.AssetImporters; | ||
using API.Types; | ||
|
||
public abstract class GraphImporter : ScriptedImporter { | ||
|
||
public sealed override void OnImportAsset (AssetImportContext ctx) { | ||
var modelData = ScriptableObject.CreateInstance<MLModelData>(); | ||
modelData.session = CreateSession(); | ||
modelData.graph = File.ReadAllBytes(ctx.assetPath); | ||
ctx.AddObjectToAsset("MLModelData", modelData); | ||
ctx.SetMainObject(modelData); | ||
} | ||
|
||
protected abstract PredictorSession CreateSession (); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
* NatML | ||
* Copyright © 2023 NatML Inc. All rights reserved. | ||
*/ | ||
|
||
namespace NatML.Editor.Importers { | ||
|
||
using UnityEditor.AssetImporters; | ||
using API.Types; | ||
|
||
[ScriptedImporter(1, @"onnx")] | ||
internal sealed class ONNXImporter : GraphImporter { | ||
|
||
protected override PredictorSession CreateSession () => new PredictorSession { | ||
format = GraphFormat.ONNX | ||
}; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
* NatML | ||
* Copyright © 2023 NatML Inc. All rights reserved. | ||
*/ | ||
|
||
namespace NatML.Editor.Importers { | ||
|
||
using UnityEditor.AssetImporters; | ||
using API.Types; | ||
|
||
[ScriptedImporter(1, @"tflite")] | ||
internal sealed class TFLiteImporter : GraphImporter { | ||
|
||
protected override PredictorSession CreateSession () => new PredictorSession { | ||
format = GraphFormat.TFLite | ||
}; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"name": "NatML.ML.Editor", | ||
"references": [ | ||
"NatML.ML" | ||
], | ||
"includePlatforms": [ | ||
"Editor" | ||
], | ||
"excludePlatforms": [], | ||
"allowUnsafeCode": false, | ||
"overrideReferences": false, | ||
"precompiledReferences": [], | ||
"autoReferenced": true, | ||
"defineConstraints": [], | ||
"versionDefines": [], | ||
"noEngineReferences": false | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* NatML | ||
* Copyright © 2023 NatML Inc. All rights reserved. | ||
*/ | ||
|
||
namespace NatML.Editor { | ||
|
||
using System.IO; | ||
using UnityEditor; | ||
using UnityEngine; | ||
using Internal; | ||
|
||
internal static class NatMLMenu { | ||
|
||
private const int BasePriority = -50; | ||
|
||
[MenuItem(@"NatML/NatML " + NatML.Version, false, BasePriority)] | ||
private static void Version () { } | ||
|
||
[MenuItem(@"NatML/NatML " + NatML.Version, true, BasePriority)] | ||
private static bool EnableVersion () => false; | ||
|
||
[MenuItem(@"NatML/Get Access Key", false, BasePriority + 1)] | ||
private static void OpenAccessKey () => Help.BrowseURL(@"https://hub.natml.ai/account/developers"); | ||
|
||
[MenuItem(@"NatML/Explore Predictors", false, BasePriority + 2)] | ||
private static void OpenHub () => Help.BrowseURL(@"https://hub.natml.ai"); | ||
|
||
[MenuItem(@"NatML/Join Discord Community", false, BasePriority + 3)] | ||
private static void OpenDiscord () => Help.BrowseURL(@"https://natml.ai/community"); | ||
|
||
[MenuItem(@"NatML/View NatML Docs", false, BasePriority + 4)] | ||
private static void OpenDocs () => Help.BrowseURL(@"https://docs.natml.ai/unity"); | ||
|
||
[MenuItem(@"NatML/Open a NatML Issue", false, BasePriority + 5)] | ||
private static void OpenIssue () => Help.BrowseURL(@"https://github.com/natmlx/natml-unity"); | ||
|
||
[MenuItem(@"NatML/Clear Predictor Cache", false, BasePriority + 6)] | ||
private static void ClearCache () { | ||
MLEdgeModel.ClearCache(); | ||
Debug.Log("NatML: Cleared predictor cache"); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* NatML | ||
* Copyright © 2023 NatML Inc. All rights reserved. | ||
*/ | ||
|
||
namespace NatML.Editor { | ||
|
||
using System.Threading.Tasks; | ||
using UnityEngine; | ||
using UnityEditor; | ||
using Internal; | ||
|
||
[FilePath(@"ProjectSettings/NatML.asset", FilePathAttribute.Location.ProjectFolder)] | ||
internal sealed class NatMLProjectSettings : ScriptableSingleton<NatMLProjectSettings> { | ||
|
||
#region --Data-- | ||
[SerializeField] | ||
private string accessKey; | ||
#endregion | ||
|
||
|
||
#region --Client API-- | ||
/// <summary> | ||
/// NatML access key. | ||
/// </summary> | ||
internal string AccessKey { | ||
get => accessKey; | ||
set { | ||
accessKey = value; | ||
if (NatMLSettings.Instance) | ||
NatMLSettings.Instance.accessKey = value; | ||
Save(false); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// NatML settings from the current project settings. | ||
/// </summary> | ||
internal static NatMLSettings CreateSettings () { | ||
var settings = ScriptableObject.CreateInstance<NatMLSettings>(); | ||
settings.accessKey = instance.AccessKey; | ||
return settings; | ||
} | ||
#endregion | ||
|
||
|
||
#region --Operations-- | ||
|
||
[InitializeOnLoadMethod] | ||
private static void OnLoad () => NatMLSettings.Instance = CreateSettings(); | ||
|
||
[InitializeOnEnterPlayMode] | ||
private static void OnEnterPlaymodeInEditor (EnterPlayModeOptions options) => NatMLSettings.Instance = CreateSettings(); | ||
#endregion | ||
} | ||
} |