1
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 3 years have passed since last update.

【Unity】AssemblyDefinition.asmdefをスクリプトから作成する

Posted at

はじめに

スクリプトからAssemblyDefinitionを生成する機会があったのですがAssemblyDefinitionAssetsealedになっていますしコンストラクタもなくてAssetDatabase.CreateAssetで生成できませんでした。

 AssetDatabase.CreateAsset(new AssemblyDefinitionAsset());

image.png

jsonから生成する

幸いAssemblyDefinitionAssetの中身はjsonなのでFile.WriteAllTextから生成することにしました。

asmdefの中身.json
{
    "name": "SampleAssemblyDefinition",
    "rootNamespace": "",
    "references": [],
    "includePlatforms": [],
    "excludePlatforms": [],
    "allowUnsafeCode": false,
    "overrideReferences": false,
    "precompiledReferences": [],
    "autoReferenced": true,
    "defineConstraints": [],
    "versionDefines": [],
    "noEngineReferences": false
}
JsonAssemblyDefinition.cs
public class JsonAssemblyDefinition
{
    public string name;
    public string rootNamespace;
    public object[] references;
    public object[] includePlatforms;
    public object[] excludePlatforms;
    public bool allowUnsafeCode;
    public bool overrideReferences;
    public object[] precompiledReferences;
    public bool autoReferenced;
    public object[] defineConstraints;
    public object[] versionDefines;
    public bool noEngineReferences;
}

スクリプトから生成します。

EditorCreateAssemblyDefinition.cs
using System.IO;
using UnityEditor;
using UnityEngine;

public class EditorCreateAssemblyDefinition : Editor
{

    [MenuItem("Qiita/CreateAssemblyReference")]
    private static void CheckSelectObject()
    {
        JsonAssemblyDefinition json = new JsonAssemblyDefinition()
        {
            name = "JsonAssemblyDefinition",
        };

        string text = JsonUtility.ToJson(json);
        string path = Path.Combine(Application.dataPath, "Scenes/EditorCreateAssemblyReference/JsonAssemblyDefinition.asmdef");

        File.WriteAllText(path, text);
        AssetDatabase.ImportAsset("Assets/Scenes/EditorCreateAssemblyReference/JsonAssemblyDefinition.asmdef", ImportAssetOptions.ForceUpdate);
    }
}

無事生成できました。

image.png

おわりに

正式にアセットを作成できる方法があるのかもしれませんが、この方法だとほかのクラスのアセットでもスクリプトから作成できると思います。

参考

AssemblyDefinitionAsset

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