?加載AB包文件,加載bytes程序集資源,通過反射獲取類,實例化添加組件,調用方法
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文件轉化為TextAssetTextAsset 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);}
}