[英]Unity C# How to check if library is existing in the project
有没有办法检查项目中是否存在使用脚本的统一库。 例如:
using UnityEngine.Advertisements;
如果它存在,那么做一些动作,如:
Advertisements.IsReady();
谢谢您的帮助!
查看有关方法Assembly.GetReferencedAssemblies()
的MSDN文档 。 此方法返回一个AssemblyName[]
其中包含此程序集引用的所有程序集的名称。 换言之,所引用的组件例如是库。 然后,您可以轻松地将代码包装在if
语句中,以便在所需的库(名称)包含在数组中时执行代码,或者不执行。
示例代码:
Assembly currentAssembly = typeof(/*enter the class name which contains the assembly references*/).Assembly;
AssemblyName[] referencedAssemblies = currentAssembly.GetReferencedAssemblies();
foreach (var refAssembly in referencedAssemblies)
{
if (assembly == /*the name of the assembly you want to check that has to exist*/)
{
//...do your work if the assembly exists
break;
}
}
这背后的技术称为Reflection
,我强烈建议你阅读,它非常有趣。
在这种情况下你不能使用if else语句,因为编译器会抱怨这个,例如if(false) Advertisements.IsReady();
编译器找不到类型广告。
但另一方面,您可以使用Reflection从程序集中获取Type。 然后得到方法来调用,但这仍然不是实用的方法。
您可能希望使用常量,或使用ClassAttribute来告诉此脚本启用广告,但using UnityEngine.Advertisements;
是必须的,您可以为此创建自定义monobehaviour,然后将其继承到您的应用程序,以便您可以using UnityEngine.Advertisements;
隐藏此行using UnityEngine.Advertisements;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.