using UnityEngine;
using System.Collections;
using System.Reflection;
using System;
using System.IO;
///
/// 符号集管理类
///
public class AssemblyHelper : MonoSingleton
{
///
/// 符号集
///
private Assembly mAssembly = null;
///
/// 对象苏醒
///
protected override void OnAwake()
{
base.OnAwake();
//DontDestroyOnLoad(this);
}
///
/// 对象销毁
///
protected override void DoOnDestroy()
{
base.DoOnDestroy();
mAssembly = null;
}
///
/// 初始化符号集
///
/// assetbundle文件完整路径
/// 资源名称
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;
}
}
///
/// 绑定脚本到指定对象上
///
/// 反射名称
/// 附加到的目标对象
/// 绑定成功后的组件对象
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);
}
///
/// 读取指定文件字节流
///
/// 文件路径
/// 失败null,否则字节流
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;
}
///
/// 加载StreamingAssets下的代码
///
///
public void LoadStreamingAssetsCode(Action ac)
{
StartCoroutine(IELoadStreamingAssetsCode(ac));
}
///
/// 协同加载StreamingAssets下的code
///
/// Lambda返回值
///
private IEnumerator IELoadStreamingAssetsCode(Action 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);
}
}
///
/// 获取加载资源路径(用于 LoadFromFile 的同步和异步加载)
///
/// 资源名称
/// 返回实际路径
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;
}
}