Skip to content

Commit

Permalink
build: export unitypackage + tarball (#531)
Browse files Browse the repository at this point in the history
* feat: export as a unitypackage

* lint

* rename Mediapipe -> MediaPipeUnity

* fix version

* move the FontAwesome directory

* export sample scenes and include dependencies

* doc: add Third Party Notices

* feat: run each sample scene directly (#528)

* refactor: add StartSceneController

* choose which Bootstrap instance to use

* remove Hello World

* attach Bootstrap to Solution objects

* use LocalAssetLoader by default

* fix: missing scripts

* parse package.json

* npm pack
  • Loading branch information
homuler committed Apr 16, 2022
1 parent b7742bc commit 1c2b34a
Show file tree
Hide file tree
Showing 359 changed files with 3,847 additions and 4,530 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
/[Cc]ode[Cc]overage/
*.csproj
*.sln
*.unitypackage

*.aar
*.bc
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
29 changes: 29 additions & 0 deletions Assets/MediaPipeUnity/Editor/CreateAssetBundles.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) 2021 homuler
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

using System.IO;
using UnityEditor;
using UnityEngine;

#if UNITY_EDITOR

public class CreateAssetBundles
{
[MenuItem("Assets/Build AssetBundles")]
internal static void BuildAllAssetBundles()
{
var assetBundleDirectory = Application.streamingAssetsPath;
if (!Directory.Exists(assetBundleDirectory))
{
var _ = Directory.CreateDirectory(assetBundleDirectory);
}
_ = BuildPipeline.BuildAssetBundles(assetBundleDirectory,
BuildAssetBundleOptions.None,
EditorUserBuildSettings.activeBuildTarget);
}
}

#endif
140 changes: 140 additions & 0 deletions Assets/MediaPipeUnity/Editor/PackageExporter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// The MIT License (MIT)
//
// Copyright (c) 2019 Yoshifumi Kawai / Cysharp, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#if UNITY_EDITOR

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;

public static class PackageExporter
{
[MenuItem("Tools/Export Unitypackage")]
public static void Export()
{
var packageRoot = Path.Combine(Application.dataPath, "..", "Packages", "com.github.homuler.mediapipe");
var version = GetVersion(packageRoot);

var fileName = string.IsNullOrEmpty(version) ? "MediaPipeUnity.unitypackage" : $"MediaPipeUnity.{version}.unitypackage";
var exportPath = "./" + fileName;

var pluginAssets = EnumerateAssets(Path.Combine("Packages", "com.github.homuler.mediapipe")); // export all the files
var sampleAssets = EnumerateAssets(Path.Combine("Assets", "MediaPipeUnity", "Samples"), new string[] { ".cs", ".unity" })
.Where(x => Path.GetFileName(x) != "Start Scene.unity"); // exclude the 'Start Scene'
var assets = pluginAssets.Concat(sampleAssets).ToArray();

Debug.Log("Export below files" + Environment.NewLine + string.Join(Environment.NewLine, assets));

AssetDatabase.ExportPackage(
assets,
exportPath,
ExportPackageOptions.IncludeDependencies);

Debug.Log("Export complete: " + Path.GetFullPath(exportPath));
}

private static IEnumerable<string> EnumerateAssets(string path)
{
var projectRoot = Path.Combine(Application.dataPath, "..");
var assetRoot = Path.Combine(projectRoot, path);

return Directory.EnumerateFiles(assetRoot, "*", SearchOption.AllDirectories)
.Select(x => path + x.Replace(assetRoot, "").Replace(@"\", "/"));
}

private static IEnumerable<string> EnumerateAssets(string path, string[] extensions)
{
return EnumerateAssets(path).Where(x => Array.IndexOf(extensions, Path.GetExtension(x)) >= 0);
}

private static string GetVersion(string packagePath)
{
var version = Environment.GetEnvironmentVariable("UNITY_PACKAGE_VERSION");
var packageJsonPath = Path.Combine(packagePath, "package.json");

if (File.Exists(packageJsonPath))
{
var packageJson = JsonUtility.FromJson<PackageJson>(File.ReadAllText(packageJsonPath));

if (!string.IsNullOrEmpty(version))
{
if (packageJson.version != version)
{
var msg = $"package.json and env version are mismatched. UNITY_PACKAGE_VERSION:{version}, package.json:{packageJson.version}";

if (Application.isBatchMode)
{
Console.WriteLine(msg);
Application.Quit(1);
}

throw new Exception("package.json and env version are mismatched.");
}
}

version = packageJson.version;
}

return version;
}

public class PackageJson
{
public string name;
public string version;
public string displayName;
public string description;
public string unity;
public Author author;
public string changelogUrl;
// public Dictionary<string, string> dependencies;
public string documentationUrl;
public bool hideInEditor;
public List<string> keywords;
public string license;
public string licenseUrl;
public List<Sample> samples;
public string type;
public string unityRelease;

[Serializable]
public class Author
{
public string name;
public string email;
public string url;
}

[Serializable]
public class Sample
{
public string displayName;
public string description;
public string path;
}
}
}

#endif

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

namespace Mediapipe.Unity
{
Expand All @@ -24,9 +22,7 @@ public enum AssetLoaderType

private const string _TAG = nameof(Bootstrap);

[SerializeField] private Image _screen;
[SerializeField] private GameObject _consolePrefab;
[SerializeField] private ImageSource.SourceType _defaultImageSource;
[SerializeField] private ImageSourceType _defaultImageSource;
[SerializeField] private InferenceMode _preferableInferenceMode;
[SerializeField] private AssetLoaderType _assetLoaderType;
[SerializeField] private bool _enableGlog = true;
Expand All @@ -35,17 +31,18 @@ public enum AssetLoaderType
public bool isFinished { get; private set; }
private bool _isGlogInitialized;

private IEnumerator Start()
private void OnEnable()
{
var _ = StartCoroutine(Init());
}

private IEnumerator Init()
{
Logger.SetLogger(new MemoizedLogger(100));
Logger.minLogLevel = Logger.LogLevel.Debug;

Protobuf.SetLogHandler(Protobuf.DefaultLogHandler);

Logger.LogInfo(_TAG, "Starting console window...");
Instantiate(_consolePrefab, _screen.transform);
yield return new WaitForEndOfFrame();

Logger.LogInfo(_TAG, "Setting global flags...");
GlobalConfigManager.SetFlags();

Expand Down Expand Up @@ -101,15 +98,33 @@ private IEnumerator Start()
}

Logger.LogInfo(_TAG, "Preparing ImageSource...");
ImageSourceProvider.SwitchSource(_defaultImageSource);
DontDestroyOnLoad(GameObject.Find("Image Source"));
ImageSourceProvider.ImageSource = GetImageSource(_defaultImageSource);

DontDestroyOnLoad(gameObject);
isFinished = true;
}

Logger.LogInfo(_TAG, "Loading the first scene...");
var sceneLoadReq = SceneManager.LoadSceneAsync(1);
yield return new WaitUntil(() => sceneLoadReq.isDone);
public ImageSource GetImageSource(ImageSourceType imageSourceType)
{
switch (imageSourceType)
{
case ImageSourceType.WebCamera:
{
return GetComponent<WebCamSource>();
}
case ImageSourceType.Image:
{
return GetComponent<StaticImageSource>();
}
case ImageSourceType.Video:
{
return GetComponent<VideoSource>();
}
case ImageSourceType.Unknown:
default:
{
throw new System.ArgumentException($"Unsupported source type: {imageSourceType}");
}
}
}

private void DecideInferenceMode()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,6 @@ public override string ToString()
}
}

public enum SourceType
{
Camera = 0,
Image = 1,
Video = 2,
}

public ResolutionStruct resolution { get; protected set; }

/// <remarks>
Expand All @@ -75,7 +68,6 @@ public enum SourceType
public virtual bool isFrontFacing { get; } = false;
public virtual RotationAngle rotation { get; } = RotationAngle.Rotation0;

public abstract SourceType type { get; }
public abstract string sourceName { get; }
public abstract string[] sourceCandidateNames { get; }
public abstract ResolutionStruct[] availableResolutions { get; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) 2021 homuler
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

public enum ImageSourceType
{
WebCamera = 0,
Image = 1,
Video = 2,
Unknown = 3,
}

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
Expand Up @@ -14,7 +14,13 @@ namespace Mediapipe.Unity
public class StaticImageSource : ImageSource
{
[SerializeField] private Texture[] _availableSources;
[SerializeField] private ResolutionStruct[] _defaultAvailableResolutions;

[SerializeField]
private ResolutionStruct[] _defaultAvailableResolutions = new ResolutionStruct[] {
new ResolutionStruct(512, 512, 0),
new ResolutionStruct(640, 480, 0),
new ResolutionStruct(1280, 720, 0),
};

private Texture2D _outputTexture;
private Texture _image;
Expand All @@ -28,8 +34,6 @@ private Texture image
}
}

public override SourceType type => SourceType.Image;

public override double frameRate => 0;

public override string sourceName => image != null ? image.name : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ private VideoClip video

private VideoPlayer _videoPlayer;

public override SourceType type => SourceType.Video;

public override string sourceName => video != null ? video.name : null;

public override string[] sourceCandidateNames => _availableSources?.Select(source => source.name).ToArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,23 @@ public class WebCamSource : ImageSource

private const string _TAG = nameof(WebCamSource);

[SerializeField] private ResolutionStruct[] _defaultAvailableResolutions;
[SerializeField]
private ResolutionStruct[] _defaultAvailableResolutions = new ResolutionStruct[] {
new ResolutionStruct(176, 144, 30),
new ResolutionStruct(320, 240, 30),
new ResolutionStruct(424, 240, 30),
new ResolutionStruct(640, 360, 30),
new ResolutionStruct(640, 480, 30),
new ResolutionStruct(848, 480, 30),
new ResolutionStruct(960, 540, 30),
new ResolutionStruct(1280, 720, 30),
new ResolutionStruct(1600, 896, 30),
new ResolutionStruct(1920, 1080, 30),
};

private static readonly object _PermissionLock = new object();
private static bool _IsPermitted = false;

public override SourceType type => SourceType.Camera;

private WebCamTexture _webCamTexture;
private WebCamTexture webCamTexture
{
Expand Down
Loading

0 comments on commit 1c2b34a

Please sign in to comment.