1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

VPM開発: 基礎のNDMFを実装

Last updated at Posted at 2024-09-07

これのつづき

VPM開発する環境ができたのでNDMFを実装してみよう

基本的に普通のAssets開発と変わりませんが、最初だけasmdefというファイルの定義が必要です

空のRuntimeの実装

  • Packages/現在開発中のVPM名フォルダ直下にRuntimeフォルダを作る
    image.png

  • Projectビューを右クリックして Assembly Definitionを作成
    image.png

  • 名称をVPMの逆ドメイン名.runtimeにする。ここでは「com.github.pandrabox.emoteprefab.runtime」
    image.png

  • ダブルクリックで開き、次を参考に設定(これはMAを参考に設定した) asmdef公式情報

{
    "name": "com.github.pandrabox.emoteprefab.runtime",
    "references": [
        "nadena.dev.ndmf.runtime"
    ],
    "includePlatforms": [],
    "excludePlatforms": [],
    "allowUnsafeCode": true,
    "overrideReferences": true,
    "precompiledReferences": [
        "VRCSDKBase.dll",
        "VRCSDK3A.dll",
        "VRC.Dynamics.dll",
        "VRC.SDK3.Dynamics.Contact.dll",
        "VRC.SDK3.Dynamics.PhysBone.dll",
        "System.Collections.Immutable.dll"
    ],
    "autoReferenced": true,
    "defineConstraints": [],
    "versionDefines": [],
    "noEngineReferences": false
}
  • ProjectビューでC# Scriptを作成
    image.png

  • 名称をMyTestにする(他の名前でも良いが、覚えておく)
    image.png

  • ダブルクリックして、下を参考にコードを作成

MyTest.cs
using UnityEngine;

namespace com.github.pandrabox.emoteprefab.runtime
{
    [AddComponentMenu("Pan/MyTest")]
    public class MyTest : MonoBehaviour, VRC.SDKBase.IEditorOnly
    {
    }
}
  • この時点でソリューション エクスプローラーを見ると、恐らくasmdefに設定したプロジェクトが作成されているはず。出来ていなかったら色々トラブルシュートが必要(ここでは扱わない)
    image.png

  • HierarchyにGameObjectを作成し、今作ったものをAdd Componentできることを確認
    image.png

VPMの更新をしてテスト環境で確認

  • この程度別にいいやと思ったら飛ばして下さい
  • package.jsonのバージョンを上げる
    image.png
  • Github desktopでCommit→Push
    image.png
  • 適当な別プロジェクトで開発しているVPMを使用設定しておく(下は既にしてあったので今あげたバージョンになった)
    image.png
  • さっき作ったRuntimeが動いている。OK。これは閉じてPackage開発用のUnityに戻る
    image.png

Editorの開発

  • Runtime同様にフォルダ、asmdefを作成する。今度の内容は次を参考に
{
    "name": "com.github.pandrabox.emoteprefab.editor",
    "references": [
        "nadena.dev.modular-avatar.core",
        "nadena.dev.modular_avatar.core.editor",
        "VRC.SDK3A",
        "VRC.SDKBase",
        "nadena.dev.ndmf",
        "nadena.dev.ndmf.vrchat",
        "VRC.SDK3A.Editor",
        "com.github.pandrabox.emoteprefab.runtime"
    ],
    "includePlatforms": [
        "Editor"
    ],
    "excludePlatforms": [],
    "allowUnsafeCode": false,
    "overrideReferences": true,
    "precompiledReferences": [
        "Newtonsoft.Json.dll",
        "System.Collections.Immutable.dll",
        "VRCSDKBase.dll",
        "VRCSDKBase-Editor.dll",
        "VRCSDK3A.dll",
        "VRCSDK3A-Editor.dll",
        "VRC.Dynamics.dll",
        "VRC.SDK3.Dynamics.Contact.dll",
        "VRC.SDK3.Dynamics.Contact.Editor.dll",
        "VRC.SDK3.Dynamics.PhysBone.dll",
        "VRC.SDK3.Dynamics.PhysBone.Editor.dll",
        "VRCCore-Editor.dll"
    ],
    "autoReferenced": false,
    "defineConstraints": [],
    "versionDefines": [],
    "noEngineReferences": false
}
  • C#を作って、開く。プロジェクトが作られていることを確認
    image.png

  • 適当にEditorを書く

MyTest.cs
using System;
using System.Linq;
using System.Collections.Generic;
using nadena.dev.ndmf;
using nadena.dev.modular_avatar.core;
using nadena.dev.modular_avatar.core.editor;
using UnityEngine;
using UnityEditor;
using UnityEditor.Animations;
using VRC.SDK3.Avatars.Components;
using com.github.pandrabox.emoteprefab.runtime;
using com.github.pandrabox.emoteprefab.editor;

[assembly: ExportsPlugin(typeof(MyTestPass))]

namespace com.github.pandrabox.emoteprefab.editor
{
    /// <summary>
    /// To call from Unity menu (Debug Only, Comment out upon release.)
    /// </summary>
    public class MyTestUnityMenu : MonoBehaviour
    {
        [MenuItem("PanDev/MyTest")]
        static void GenMyTest()
        {
            new MyTestMain().Run(Selection.activeGameObject);
        }
    }
    /// <summary>
    /// To call from NDMF
    /// </summary>
    public class MyTestPass : Plugin<MyTestPass>
    {
        protected override void Configure()
        {
            try
            {
                InPhase(BuildPhase.Transforming).BeforePlugin("nadena.dev.modular-avatar").Run("PanMyTest", ctx =>
                {
                    var TargetComponents = ctx.AvatarRootTransform.GetComponentsInChildren<MyTest>(false);
                    foreach (var T in TargetComponents)
                    {
                        new MyTestMain().Run(T.gameObject);
                    }
                });
            }
            catch (Exception e)
            {
                Debug.LogError($@"[Pan:MyTest]{e}");
            }
        }
    }
    /// <summary>
    /// Actual operation
    /// Retrieving ctx is not recommended to maintain debug availability.
    /// </summary>
    public class MyTestMain : MonoBehaviour
    {
        public void Run(GameObject Target)
        {
            Target.SetActive(false);
            Debug.LogWarning($@"MyTest:{Target.name}");
        }
    }
}

  • 動作チェックする

    • 上のコードのままなら
      • Hierarchyで何かをアクティブにして上部メニューPanDev→MyTestを実行するとそのオブジェクトが非表示になる
      • アバター内のなにかにMyTestをAdd ComponentしてAv3有効でPlayにする等するとそのオブジェクトが非表示になる
  • Commit,Push
    image.png

  • 適当な他のプロジェクトで動作確認(2019で開発して2022で確認が最も有意義そう)

1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?