特に必要性を感じていないですが、dllを作って使えるみたいなので試してみました。
準備する
Unityのプロジェクトを作成する
![](https://unity.t-haku.com/wp-content/uploads/2021/03/project-1024x593.png)
確認用Textを配置する
![](https://unity.t-haku.com/wp-content/uploads/2021/03/text-1024x650.png)
Layoutはnmxi styleにしています。
Hierarchyで[UI]-[Text]を作成します。
Textの設定をGame Viewで見やすいように変更します。
![](https://unity.t-haku.com/wp-content/uploads/2021/03/transform2-1024x808.png)
C#Scriptを作成する
ProjectでC#Scriptを作成してTestDllという名前にします。
HierarchyのCreate EmptyメニューでGameObjectを作成します。
TestDll C# ScriptをGameObjectにアタッチします。
![](https://unity.t-haku.com/wp-content/uploads/2021/03/script-1024x582.png)
TestDll C#ScriptをダブルクリックするとVisual Studioが起動します。
using UnityEngine; using UnityEngine.UI; public class TestDll : MonoBehaviour { [SerializeField] Text text; // Inspectorからアタッチ void Start() { text.text = "Hello!"; } }
![](https://unity.t-haku.com/wp-content/uploads/2021/03/gameobject-1024x582.png)
GameObjectのInspectorでTest Dll (Script)のTextにText GameObjectをアタッチします。
Playボタンを押すとTextが”Hello!”になります。
![](https://unity.t-haku.com/wp-content/uploads/2021/03/hellopng-1024x582.png)
Dllを作成する
Visual Studioでクラスライブラリプロジェクトを作成する
Visual Studioのソリューション エクスプローラーでソリューションのコンテキストメニュー[追加]-[新しいプロジェクト]を選択します。
![](https://unity.t-haku.com/wp-content/uploads/2021/03/vsproject.png)
![](https://unity.t-haku.com/wp-content/uploads/2021/03/classlibrary.png)
クラスライブラリ (.NET Standard)を選択して[次へ]ボタンを押します。
![](https://unity.t-haku.com/wp-content/uploads/2021/03/projectname.png)
プロジェクト名と場所を指定して[作成]ボタンを押します。
C#のコードを書く
ファイル名をClassOnDll.csとし次のコードを書きます。
using System; namespace UnityLibrary { public class ClassOnDll { public string message = "Hello Dll World!"; } }
ビルドする
コードを書いて[ビルド]-[ソリューションのビルド]メニューでビルドします。
できたdllをUnit Editorにドラッグ&ドロップする
![](https://unity.t-haku.com/wp-content/uploads/2021/03/explor-1024x739.png)
エクスプローラを開きbin/Debug/netstandard2.0フォルダに移動します。
![](https://unity.t-haku.com/wp-content/uploads/2021/03/dll.png)
dllファイルをUnity EditorのProject windowのAssetsのドラッグ&ドロップします。
![](https://unity.t-haku.com/wp-content/uploads/2021/03/icon-1024x779.png)
Dllのクラスを使ってみる
TestDllからClassOnDllを使ってみます。
using UnityEngine; using UnityEngine.UI; using UnityLibrary; public class TestDll : MonoBehaviour { [SerializeField] Text text; // Inspectorからアタッチ void Start() { var classOnDll = new ClassOnDll(); text.text = classOnDll.message; } }
結果を確認する
Playボタンを押します。
![](https://unity.t-haku.com/wp-content/uploads/2021/03/hellodllworld-1024x779.png)
dllのClassOnDllから取得した文字列が表示されました。
DllでUnityEngineを使う
純粋なC#は使えたので、UnityEngineも使えるか確認します。
参照を追加する
Visual Studioのソリューション エクスプローラのUnityLibraryプロジェクトの依存関係のコンテキストメニューから[プロジェクト参照の追加]を選択します。
![](https://unity.t-haku.com/wp-content/uploads/2021/03/reference.png)
![](https://unity.t-haku.com/wp-content/uploads/2021/03/reference2.png)
下の[参照]ボタンから次のパスのUnityEngine.dllを追加します。※ UnityEditor.dllと似ているので間違いやすいです。
C:\Program Files\Unity\Hub\Editor\<使用バージョン>\Editor\Data\Managed\UnityEngine.dll
C#でUnityEngineを使う
ClassOnDll.csにusing UnityEngine;を追加し、GameObjectを生成するコードを追加します。
using System; using UnityEngine; namespace UnityLibrary { public class ClassOnDll { public string message = "Hello Dll World!"; public GameObject CreateGameObject() { var cube = GameObject.CreatePrimitive(PrimitiveType.Cube); var meshRenderer = cube.GetComponent<MeshRenderer>(); meshRenderer.material.color = new Color(0.5f, 0.8f, 1); return cube; } } }
ビルドしてUnityLibrary.dllをUnity Editorを通さずエクスプローラからAssetsのUnityLibrary.dllに上書きします。
追加したメソッドを呼び出す
追加したメソッドをTestDllから呼び出します。
using UnityEngine; using UnityEngine.UI; using UnityLibrary; public class TestDll : MonoBehaviour { [SerializeField] Text text; // Inspectorからアタッチ void Start() { var classOnDll = new ClassOnDll(); text.text = classOnDll.message; classOnDll.CreateGameObject(); var sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere); sphere.transform.position = new Vector3(0, 1, 0); var meshRenderer = sphere.GetComponent<MeshRenderer>(); meshRenderer.material.color = new Color(0.5f, 0.8f, 1); } }
ClassOnDllではCubeをTestDll側ではSphereを生成しました。
結果を確認する
Playボタンで確認する。
![](https://unity.t-haku.com/wp-content/uploads/2021/03/play-1024x779.png)
Dllで作成したCube GameObjectが表示されました。
PC上で結果を確認する
[ESC]でアプリが終了するようにコードを追加します。
using UnityEngine; using UnityEngine.UI; using UnityLibrary; public class TestDll : MonoBehaviour { [SerializeField] Text text; // Inspectorからアタッチ void Start() { var classOnDll = new ClassOnDll(); text.text = classOnDll.message; classOnDll.CreateGameObject(); var sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere); sphere.transform.position = new Vector3(0, 1, 0); var meshRenderer = sphere.GetComponent<MeshRenderer>(); meshRenderer.material.color = new Color(0.5f, 0.8f, 1); } void Update() { if (Input.GetKey(KeyCode.Escape)) { Application.Quit(); } } }
[File]-[Build Setting]でPlatfomにPCを選び[Biuld]ボタンを押します。
![](https://unity.t-haku.com/wp-content/uploads/2021/03/build-setting.png)
Build先のフォルダを選択します。
![](https://unity.t-haku.com/wp-content/uploads/2021/03/exe-dll.png)
作成されたexeファイルをダブルクリックで実行します。
![](https://unity.t-haku.com/wp-content/uploads/2021/03/pink-1024x576.png)
Unity EditorでのPlayとは異なるピンク色で表示されました。
![](https://unity.t-haku.com/wp-content/uploads/2021/03/sphere-1024x779.png)
HierarchyでSphereを生成して非アクティブにしたら色が付きました。
![](https://unity.t-haku.com/wp-content/uploads/2021/03/blue-1024x576.png)
Shaderに問題があるようなのでShaderの名前を表示してみました。
using System; using UnityEngine; using UnityEngine.UI; using UnityLibrary; public class TestDll : MonoBehaviour { [SerializeField] Text text; // Inspectorからアタッチ void Start() { var classOnDll = new ClassOnDll(); text.text = classOnDll.message; classOnDll.CreateGameObject(); try { var sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere); sphere.transform.position = new Vector3(0, 1, 0); var meshRenderer = sphere.GetComponent<MeshRenderer>(); meshRenderer.material.color = new Color(0.5f, 0.8f, 1); text.text = meshRenderer.material.shader.name; } catch (Exception ex) { text.text = ex.Message; } } void Update() { if (Input.GetKey(KeyCode.Escape)) { Application.Quit(); } } }
![](https://unity.t-haku.com/wp-content/uploads/2021/03/standard-1024x700.png)
![](https://unity.t-haku.com/wp-content/uploads/2021/03/internalerror-1024x576.png)
Unity EditorのPlayでは”Standard”ですが、PC上では”Hidden/InternalErrorShader”と表示されました。
Standard Shaderを常にincludeする
TestDllを元に戻しました。
using System; using UnityEngine; using UnityEngine.UI; using UnityLibrary; public class TestDll : MonoBehaviour { [SerializeField] Text text; // Inspectorからアタッチ void Start() { var classOnDll = new ClassOnDll(); text.text = classOnDll.message; classOnDll.CreateGameObject(); var sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere); sphere.transform.position = new Vector3(0, 1, 0); var meshRenderer = sphere.GetComponent<MeshRenderer>(); var material = meshRenderer.material; material.color = new Color(0.5f, 0.8f, 1); } void Update() { if (Input.GetKey(KeyCode.Escape)) { Application.Quit(); } } }
[Edit]-[Project Settings]-[Graphics]の[Allways Included Shaders]のSizeを増やし、[Standard]を追加し、[Save to asset]ボタンを押しました。
![](https://unity.t-haku.com/wp-content/uploads/2021/03/add_standard.png)
[Build]-[Build Settings]で[Build]しました。かなり長い時間ビルドしていましたが、表示されるようになりました。
![](https://unity.t-haku.com/wp-content/uploads/2021/03/blue2-1024x576.png)
コメント