AssetBundle-尝试使用反照绑定脚本到游戏对象以失败告终

AssetBundle-尝试使用反射绑定脚本到游戏对象以失败告终

 

我想搞增量更新脚本。。

 

这是我参考的官网地址:

http://docs.unity3d.com/Manual/scriptsinassetbundles.html

 

哎。结果没有绑定成功。。悲剧啊。。。哎。。

 

Man.cs

 

using UnityEngine;
using System.Collections;

/**
 * Date:2014-07-25
 * Des:代码描述
 * Author:小阿哥
 **/
public class Main : MonoBehaviour
{

	private Texture t0;
	private GameObject cub;

	void Awake ()
	{
    
	}
    
	// Use this for initialization
	void Start ()
	{
		cub = GameObject.Find("Cube");
	}
    
	// Update is called once per frame
	void Update ()
	{
    
	}
    
	void FixedUpdate ()
	{
    
	}
    
	void OnGUI ()
	{
		if(t0 != null)
		{
			GUI.DrawTexture(new Rect (100, 100, t0.width, t0.height),t0);
		}
		if(GUILayout.Button("加载-AB-文本-尝试反射"))
		{
			StartCoroutine(loadAssetBundleTxt("Move.rui"));
		}
		if(GUILayout.Button("加载-AB-纹理贴图"))
		{
			StartCoroutine(loadAssetBundleTexture("290.rui"));
		}
	}

	private IEnumerator loadAssetBundleTxt (string name)
	{
		string fileFullName = "file://" + Application.dataPath + "/AssetBundle/" + name;
		WWW data = new WWW (fileFullName);
		yield return data;

		if(data == null)
		{       
			Debug.Log(".unity3d格式的文件:" + fileFullName + "___文件data==null"); 
		}
		else
		{
			Debug.Log(".unity3d格式的文件:" + fileFullName + "___文件大小:" + data.bytes.Length + "__字节"); 
		}

		if(data.assetBundle == null)
		{
			Debug.Log("data.assetBundle==null"); 
		}
		else
		{
			Debug.Log("data.assetBundle.name:" + data.assetBundle.name + "___ID:" + data.assetBundle.GetInstanceID()); 
		}

		if(data.assetBundle.mainAsset == null)
		{
			Debug.Log("data.assetBundle.mainAsset==null"); 

		}
		else
		{
			Debug.Log("data.assetBundle.mainAsset.name=" + data.assetBundle.mainAsset.name);
		}

		TextAsset txt = data.assetBundle.mainAsset as TextAsset;

		if(txt == null)
		{
			Debug.Log("mainAsset 不是 TextAsset对象");
		}

		byte[] fileContent = txt.bytes;
		if(fileContent == null)
		{
			Debug.Log("打到.unity3d之前的文件的字节数组=null");
		}
		else
		{
			Debug.Log("打到.unity3d之前的文件的大小:" + fileContent.Length);
		}

		if(fileContent.Length == 0)
		{
			Debug.Log("!!!!!!!!!!!擦!!!!!!!!!!");
		}

		try
		{
			System.Reflection.Assembly assembly = System.Reflection.Assembly.Load(fileContent);
            
			if(assembly == null)
			{
				Debug.Log("反射对象 assembly:" + assembly);
			}
			else
			{
				Debug.Log("反射对象 assembly:" + assembly.GetHashCode());
			}

			cub.AddComponent("Move");
		}
		catch(UnityException)
		{
			Debug.Log("反射时报错了。。");
		}
		finally
		{
			string ss = readUTF8Text(fileContent);
			Debug.Log("读取到的内容:" + ss);
            
			data.assetBundle.Unload(false);
		}
	}

	private string readUTF8Text (byte[] file)
	{
		return System.Text.Encoding.UTF8.GetString(file);
	}

	private IEnumerator loadAssetBundleTexture (string name)
	{
		WWW data = new WWW ("file://" + Application.dataPath + "/AssetBundle/" + name);
		yield return data;
        
		Texture t = data.assetBundle.mainAsset as Texture;
		t0 = t;

		data.assetBundle.Unload(false);
	}

	void OnDestroy ()
	{
    
	}
        
}

 

 CreateBundle.cs

 

using UnityEngine;
using System.Collections;
using UnityEditor;

/**
 * Date:2014-**-**
 * Des:代码描述
 * Author:小阿哥
 **/
public class CreateBundle  {


	[MenuItem("创建Ass/创建单独文件")]
	static void createBundle()
	{
		string outDir=""+Application.dataPath+"/AssetBundle/";
		UnityEngine.Object[] selectOb=Selection.GetFiltered(typeof(Object),SelectionMode.DeepAssets);
		if(selectOb!=null)
		{
			for(int i=0;i<selectOb.Length;i++)
			{
				if(selectOb[i]!=null)
				{
					bool isOk=BuildPipeline.BuildAssetBundle(selectOb[i],null,outDir+selectOb[i].name+".rui",BuildAssetBundleOptions.CollectDependencies,BuildTarget.StandaloneWindows);
					Debug.Log("创建AssetBundle Name:"+selectOb[i].name+"_____:"+isOk);
				}
			}
		}
	}
		
}

 

 

 

运行结果:

AssetBundle-尝试使用反照绑定脚本到游戏对象以失败告终

 

哎。哎。。想交流的请留言。。