加载AB包文件,加载bytes程序集资源,通过反射获取类,实例化添加组件,调用方法文章来源地址https://www.toymoban.com/news/detail-648867.html
public class LoadAB : MonoBehaviour
{
private void Update()
{
if (Input.GetKeyDown(KeyCode.H))
{
Load();
}
}
void Load()
{
string classname = "ID506_BrakeFiveStep";
string RealClassName = "DriveEventAbstractParentClassNameSpace." + classname + "_AB";
string Dll_bytesName = classname + ".bytes";
string path = "D:/Desktop/newcode/AssetBundles/DriveEventAssests/id506_brakefivestep";
AssetBundle ab = AssetBundle.LoadFromFile(path);
ab.LoadAsset(Dll_bytesName);
//将.bytes文件转化为TextAsset
TextAsset VS_Dll_To_bytes = ab.LoadAsset(Dll_bytesName, typeof(TextAsset)) as TextAsset;
//转化为byte数组
byte[] by = VS_Dll_To_bytes.bytes;
//通过反射获取想要的类
Assembly am = Assembly.Load(by);
Type type = am.GetType(RealClassName);
//实例化这个类
object obj;
if (GetComponent(type) == null)
{
gameObject.AddComponent(type);
}
obj = GetComponent(type);
//直接调用
string methodName = "InitAB";
System.Object[] paras = new System.Object[] { "{'EventID':'506','Color':'yellow','OffsetMetre':'0'}" };
MethodInfo method = type.GetMethod(methodName);
method.Invoke(obj, paras);
}
}
文章来源:https://www.toymoban.com/news/detail-648867.html
到了这里,关于加载AB包程序集,反射获取并实例化类调用方法的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!