Skip to content

Instantly share code, notes, and snippets.

@rhys-vdw
Created June 2, 2019 06:30
Show Gist options
  • Save rhys-vdw/230820574d8c2bd35549f9ccb101fea9 to your computer and use it in GitHub Desktop.
Save rhys-vdw/230820574d8c2bd35549f9ccb101fea9 to your computer and use it in GitHub Desktop.
Script to auto-generate assemblies for back-compatibility with Unity3D's "Editor" folder magic
// Adapted from
// https://www.3delement.com/?p=586
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using System.IO;
using Cybermonk;
class AssemblyDefinitionType // type used to load the .asmdef as json
{
public string name;
public List<string> references = new List<string>();
public List<string> includePlatforms = new List<string>();
public List<string> excludePlatforms = new List<string>();
public bool allowUnsafeCode = true;
public bool overrideReferences = false;
public List<string> precompiledReferences = new List<string>();
public bool autoReferenced = true;
public List<string> defineConstraints = new List<string>();
public List<string> versionDefines = new List<string>();
}
public class AssemblyDefinitionSwitch
{
[MenuItem("Tools/Create EditorAssemblyDefFiles")]
public static void TurnOnAssembly()
{
CreateEditorFiles(new DirectoryInfo(Application.dataPath), null, false);
AssetDatabase.Refresh();
}
[MenuItem("Tools/Delete EditorAssemblyDefFiles")]
public static void TurnOffAssembly()
{
CreateEditorFiles(new DirectoryInfo(Application.dataPath), null, true);
AssetDatabase.Refresh();
}
static List<string> EditorAssemblySibilings;
static int duplicateNum = 0;
static string AssetGuid(string path) {
var assetRoot = Application.dataPath.Remove(Application.dataPath.Length - "Assets".Length);
var assetPath = path.Remove(0, assetRoot.Length);
Debug.Log($"path={path} assetRoot={assetRoot} assetPath={assetPath}");
var asset = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(assetPath);
if (AssetDatabase.TryGetGUIDAndLocalFileIdentifier(asset, out var guid, out long _)) {
return guid;
}
throw new System.InvalidOperationException($"Could not find asset at {path}");
}
static void CreateEditorFiles(DirectoryInfo Dir, FileInfo parentAsmdef, bool Remove)
{
// var cybermonkAsmdef = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>("Cybermonk/Cybermonk.asmdef");
// var everythingAsmdef = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>("Everything.asmdef");
FileInfo[] files = Dir.GetFiles();
foreach (FileInfo file in files)
{
if (file.Extension == ".asmdef")
{
parentAsmdef = file;
EditorAssemblySibilings = new List<string>();
duplicateNum = 1;
}
}
if (parentAsmdef != null)
{
DirectoryInfo[] dirs = Dir.GetDirectories();
foreach (DirectoryInfo subdir in dirs)
{
if (subdir.Name == "Editor")
{
FileInfo[] editorfiles = subdir.GetFiles();
foreach (FileInfo file in editorfiles)
{
// Delete old asmdef editor folder files
if (file.Extension == ".asmdef")
{
File.Delete(file.FullName);
}
}
if (!Remove)
{
// create a assemb file based on the Parent one, only with includePlatforms changed to Editor
// assembly name is the parent assembly name + directory that contains the editor folder + "Editor"
AssemblyDefinitionType EditorAssemb = JsonUtility.FromJson<AssemblyDefinitionType>(File.ReadAllText(parentAsmdef.FullName));
EditorAssemb.references.Add($"GUID:{AssetGuid(parentAsmdef.FullName)}"); // add parent assembly as a reference
// EditorAssemb.references.Add(EditorAssemb.name); // add parent assembly as a reference
EditorAssemb.references.AddRange(EditorAssemblySibilings); // for subeditor folders to reference the above editor assemblies
EditorAssemb.name = "z_" + EditorAssemb.name + Dir.Name + "Editor"; // added z_ to be at bottom of solution explorer
if (EditorAssemblySibilings.Contains(EditorAssemb.name))
{
EditorAssemb.name += duplicateNum;
duplicateNum += 1;
}
EditorAssemblySibilings.Add(EditorAssemb.name);
EditorAssemb.includePlatforms = new List<string>();
EditorAssemb.includePlatforms.Add("Editor");
File.WriteAllText(Path.Combine(subdir.FullName, EditorAssemb.name + ".asmdef"), JsonUtility.ToJson(EditorAssemb));
}
}
}
}
DirectoryInfo[] dirs2 = Dir.GetDirectories();
foreach (DirectoryInfo subdir in dirs2)
{
if (subdir.Name != "Editor")
CreateEditorFiles(subdir, parentAsmdef, Remove);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment