123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371 |
- using UnityEngine;
- using System.Collections;
- using System.Reflection;
- using System;
- using System.IO;
- /// <summary>
- /// 符号集管理类
- /// </summary>
- public class AssemblyHelper : MonoSingleton<AssemblyHelper>
- {
- /// <summary>
- /// 符号集
- /// </summary>
- private Assembly mAssembly = null;
- /// <summary>
- /// 对象苏醒
- /// </summary>
- protected override void OnAwake()
- {
- base.OnAwake();
- //DontDestroyOnLoad(this);
- }
- /// <summary>
- /// 对象销毁
- /// </summary>
- protected override void DoOnDestroy()
- {
- base.DoOnDestroy();
- mAssembly = null;
- }
- /// <summary>
- /// 初始化符号集
- /// </summary>
- /// <param name="file">assetbundle文件完整路径</param>
- /// <param name="asset">资源名称</param>
- public void InitializeAssembly(string file, string asset, Action ac)
- {
- switch (Application.platform)
- {
- case RuntimePlatform.Android:
- {
- Debug.Log("安卓下初始化符号集" + file);
- // 后期考虑dll加密
- byte[] ctx = ReadFileBytes(file);
- if (null != ctx)
- {
- AssetBundle bundle = AssetBundle.LoadFromMemory(ctx);
- TextAsset text = bundle.LoadAsset(asset, typeof(TextAsset)) as TextAsset;
- mAssembly = Assembly.Load(text.bytes);
- if (null == mAssembly)
- {
- Debug.LogError("安卓下初始化符号集" + name + "失败");
- }
- bundle.Unload(false);
- if (ac != null)
- {
- ac();
- }
- }
- else
- {
- LoadStreamingAssetsCode((data) =>
- {
- if (data != null)
- {
- AssetBundle bundle = AssetBundle.LoadFromMemory(data);
- TextAsset text = bundle.LoadAsset(asset, typeof(TextAsset)) as TextAsset;
- mAssembly = Assembly.Load(text.bytes);
- if (null == mAssembly)
- {
- Debug.LogError("安卓下初始化符号集" + name + "失败");
- }
- bundle.Unload(false);
- if (ac != null)
- {
- ac();
- }
- }
- else
- {
- Debug.LogError("无法查找到文件:" + file);
- }
- });
- }
- }
- break;
- case RuntimePlatform.WindowsEditor:
- {
- Debug.Log("编辑器下初始化符号集" + file);
- // 后期考虑dll加密
- byte[] ctx = ReadFileBytes(file);
- if (null != ctx)
- {
- AssetBundle bundle = AssetBundle.LoadFromMemory(ctx);
- TextAsset text = bundle.LoadAsset(asset, typeof(TextAsset)) as TextAsset;
- mAssembly = Assembly.Load(text.bytes);
- if (null == mAssembly)
- {
- Debug.LogError("编辑器下初始化符号集" + name + "失败");
- }
- bundle.Unload(false);
- if (ac != null)
- {
- ac();
- }
- }
- else
- {
- LoadStreamingAssetsCode((data) =>
- {
- if (data != null)
- {
- AssetBundle bundle = AssetBundle.LoadFromMemory(data);
- TextAsset text = bundle.LoadAsset(asset, typeof(TextAsset)) as TextAsset;
- mAssembly = Assembly.Load(text.bytes);
- if (null == mAssembly)
- {
- Debug.LogError("编辑器下初始化符号集" + name + "失败");
- }
- bundle.Unload(false);
- if (ac != null)
- {
- ac();
- }
- }
- else
- {
- Debug.LogError("无法查找到文件:" + file);
- }
- });
- }
- }
- break;
- case RuntimePlatform.IPhonePlayer:
- case RuntimePlatform.OSXEditor:
- {
- Debug.Log("IOS下初始化符号集" + file);
- // 后期考虑dll加密
- byte[] ctx = ReadFileBytes(file);
- if (null != ctx)
- {
- AssetBundle bundle = AssetBundle.LoadFromMemory(ctx);
- TextAsset text = bundle.LoadAsset(asset, typeof(TextAsset)) as TextAsset;
- mAssembly = Assembly.Load(text.bytes);
- if (null == mAssembly)
- {
- Debug.LogError("IOS下初始化符号集" + name + "失败");
- }
- bundle.Unload(false);
- if (ac != null)
- {
- ac();
- }
- }
- else
- {
- LoadStreamingAssetsCode((data) =>
- {
- if (data != null)
- {
- AssetBundle bundle = AssetBundle.LoadFromMemory(data);
- TextAsset text = bundle.LoadAsset(asset, typeof(TextAsset)) as TextAsset;
- mAssembly = Assembly.Load(text.bytes);
- if (null == mAssembly)
- {
- Debug.LogError("IOS下初始化符号集" + name + "失败");
- }
- bundle.Unload(false);
- if (ac != null)
- {
- ac();
- }
- }
- else
- {
- Debug.LogError("无法查找到文件:" + file);
- }
- });
- }
- }
- break;
- }
- }
- /// <summary>
- /// 绑定脚本到指定对象上
- /// </summary>
- /// <param name="name">反射名称</param>
- /// <param name="parent">附加到的目标对象</param>
- /// <returns>绑定成功后的组件对象</returns>
- public Component BindScript(string name, GameObject parent)
- {
- switch (Application.platform)
- {
- case RuntimePlatform.Android:
- {
- if (null == mAssembly)
- {
- //Debug.LogError("!!!!安卓下绑定脚本失败,符号集为空.转向本地绑定!!!_" + name);
- // 转向本地化处理
- Type val = Type.GetType(name);
- return parent.AddComponent(val);
- }
- System.Type script = mAssembly.GetType(name);
- if (script == null)
- {
- Type val = Type.GetType(name);
- return parent.AddComponent(val);
- }
- return parent.AddComponent(script);
- }
- case RuntimePlatform.WindowsEditor:
- {
- if (null == mAssembly)
- {
- // Debug.LogError("!!!!编辑器下绑定脚本失败,符号集为空.转向本地绑定!!!_" + name);
- // 转向本地化处理
- Type val = Type.GetType(name);
- return parent.AddComponent(val);
- }
- System.Type script = mAssembly.GetType(name);
- if (script == null)
- {
- Type val = Type.GetType(name);
- return parent.AddComponent(val);
- }
- // Debug.Log(script + " n " +name);
- return parent.AddComponent(script);
- }
- case RuntimePlatform.IPhonePlayer:
- case RuntimePlatform.OSXEditor:
- {
- //if (null == mAssembly)
- //{
- // Type val = Type.GetType(name);
- // return parent.AddComponent(val);
- //}
- //System.Type script = mAssembly.GetType(name);
- //return parent.AddComponent(script);
- if (null == mAssembly)
- {
- // Debug.LogError("!!!!编辑器下绑定脚本失败,符号集为空.转向本地绑定!!!_" + name);
- // 转向本地化处理
- Type val = Type.GetType(name);
- return parent.AddComponent(val);
- }
- System.Type script = mAssembly.GetType(name);
- if (script == null)
- {
- Type val = Type.GetType(name);
- return parent.AddComponent(val);
- }
- // Debug.Log(script + " n " +name);
- return parent.AddComponent(script);
- }
- }
- // 其他环境下处理
- Type tp = Type.GetType(name);
- return parent.AddComponent(tp);
- }
- /// <summary>
- /// 读取指定文件字节流
- /// </summary>
- /// <param name="file">文件路径</param>
- /// <returns>失败null,否则字节流</returns>
- private byte[] ReadFileBytes(string file)
- {
- if (false == File.Exists(file))
- {
- return null;
- }
- FileStream fs = new FileStream(file, FileMode.Open);
- long size = fs.Length;
- byte[] ctx = new byte[size];
- fs.Read(ctx, 0, ctx.Length);
- fs.Close();
- return ctx;
- }
- /// <summary>
- /// 加载StreamingAssets下的代码
- /// </summary>
- /// <param name="ac"></param>
- public void LoadStreamingAssetsCode(Action<byte[]> ac)
- {
- StartCoroutine(IELoadStreamingAssetsCode(ac));
- }
- /// <summary>
- /// 协同加载StreamingAssets下的code
- /// </summary>
- /// <param name="ac"> Lambda返回值</param>
- /// <returns></returns>
- private IEnumerator IELoadStreamingAssetsCode(Action<byte[]> ac)
- {
- Debug.Log("开始加载StreamingAssets下的代码");
- WWW www = new WWW(GetPath());
- yield return www;
- if (www.error == null)
- {
- if (ac != null)
- {
- ac(www.bytes);
- Debug.Log("StreamingAssets下的代码加载完成");
- }
- www.assetBundle.Unload(true);
- www.Dispose();
- }
- else
- {
- if (ac != null)
- {
- ac(null);
- }
- Debug.LogWarning("加载错误:" + www.error);
- }
- }
- /// <summary>
- /// 获取加载资源路径(用于 LoadFromFile 的同步和异步加载)
- /// </summary>
- /// <param name="strName">资源名称</param>
- /// <returns>返回实际路径</returns>
- public static string GetPath(string strName = "Core.unity3d")
- {
- string strPath = string.Empty;
- if (RuntimePlatform.Android == Application.platform)
- {
- strPath = Application.streamingAssetsPath + "/Res/" + strName;
- }
- else
- {
- strPath = "file://" + Application.streamingAssetsPath + "/Res/" + strName;
- }
- return strPath;
- }
- }
|