/// <summary> /// Returns the <see cref="Type"/> of the class implemented by this script. Works for types not derived from /// <see cref="UnityEngine.Object"/> and generic classes (the file must be named by the "GenericClass`1.cs" template). /// </summary> /// <param name="script">The script to get the type from.</param> /// <returns>The <see cref="Type"/> of the class implemented by this script or <see langword="null"/>, /// if the type was not found.</returns> [PublicAPI, CanBeNull] public static Type GetClassType(this MonoScript script) { Type simpleType = script.GetClass(); if (simpleType != null) { return(simpleType); } string className = GetFirstClassFromText(script.text); if (string.IsNullOrEmpty(className)) { return(null); } string assemblyName = script.GetAssemblyName(); Assembly assembly; try { assembly = Assembly.Load(assemblyName); } catch (Exception e) { // Whatever caused this exception, the type cannot be loaded, so disregard it as null. if (e is FileNotFoundException || e is FileLoadException) { return(null); } throw; } string namespaceName = script.GetNamespaceName(); string fullTypeName = namespaceName == string.Empty ? className : $"{namespaceName}.{className}"; Type type = assembly.GetType(fullTypeName); return(type); }
public static string Internal_GetAssemblyName(this MonoScript script) => script.GetAssemblyName();