0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

UPMのPackageに依存したコードをunitypackageで配布する

Last updated at Posted at 2023-02-22

はじめに

UPMのPackageに依存したコードをunitypackageで配布する方法を記します。恐らく最適解ではないです

環境

Unity2019.4.31f1

本文

概要

unitypackageのimport時にUPMから依存しているpackageをインストールさせます

フォルダ構成

以下のようなフォルダ構成にします。配布物用とUPMImporter用に2つasmdefを作成します
image.png

UPMImporter作成

UPMImport用のScriptを作成します。今回はpackageにAlembic 2.3.0を使用します

UPMImporter.cs
using UnityEditor;
using UnityEditor.PackageManager;
using UnityEngine;

[InitializeOnLoad]
public static class UPMImporter
{
    static UPMImporter()
    {
        Install("com.unity.formats.alembic@2.3.0");
    }

    public static bool Install(string id)
    {
        var request = Client.Add(id);
        while (!request.IsCompleted) { };
        if (request.Error != null) Debug.LogError(request.Error.message);
        return request.Error == null;
    }
}

UPMImporter用のasmdef作成

UPMImporter用のasmdefの設定をします。PlatformsをEditorOnlyに設定します。

image.png

配布物用のasmdef作成

配布物用のasmdefの設定をします。Define Constraints と Version Defines に依存packageを設定し、適当なシンボルを作成します。

fixed.png

コード修正

プリプロセッサの条件付きコンパイルで作成したシンボルを使い unitypackage の import時にコンパイルエラーが起きないようにします

#if PACKAGE_IMPORTED

using UnityEditor;
using UnityEngine.Formats.Alembic.Importer;
using UnityEngine;

public static class Haihu
{
    private AlembicStreamPlayer _alembic = null;
}

#endif

おわり

あとはunitypackageに出力し、importして確認してみましょう

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?