- Home /
Problem using assetbundle to patch a game.
Hello! We're develop a game using assetbundle so that scene can be changed by modifying them. But it is another story when I need to patch my code.
Originally I thought it not possible to patch code via assetbundle. After I read this, it now seems to work. I made a test to verify if it works. After building first working version, I modified one of the scripts and build the assembly, and made an assetbundle. Then I load the assetbundle by the following script. It is already in the first working version of the game.
using UnityEngine;
using System.Collections;
public class LoadBinaryScript : MonoBehaviour {
public string assemblyBundleUrl = "http://my_site/some_path/barrel.patch";
public string patchFilename = "CodePatch";
public GUIText logMessage;
public string[] replacedClasses;
IEnumerator GetPatch()
{
WWW patch = new WWW(assemblyBundleUrl);
yield return patch;
if(patch.error != null){
Debug.Log("Patch download error: "+patch.error);
logMessage.text = "Patch download error: "+patch.error;
yield break;
}
Debug.Log("Patch loaded.");
TextAsset ta = patch.assetBundle.Load(patchFilename, typeof(TextAsset)) as TextAsset;
if(ta == null){
Debug.Log("Patch not exist: "+patchFilename);
logMessage.text = "Patch not exist: "+patchFilename;
yield break;
}
Debug.Log("Patch code extracted.");
logMessage.text = "Patch code extracted.";
var assembly = System.Reflection.Assembly.Load(ta.bytes);
bool err = false;
for(int i=0;i<replacedClasses.Length;++i){
var typ = assembly.GetType(replacedClasses[i]);
if(typ == null){
logMessage.text = "Script not found: "+replacedClasses[i];
err = true;
break;
}
}
if(!err){
logMessage.text = "All scripts found.";
}
}
// Use this for initialization
void Start () {
Debug.Log ("LoadBinaryScript Start()");
StartCoroutine(GetPatch());
}
// Update is called once per frame
void Update () {
}
}
After restarting the test game, it seems successful. Both "Patch loaded" and "Patch code extracted" are printed, and even "All scripts found" are shown on screen. However, when the modified script runs, it still print message stating it the old version. Is there something missing? Or isn't it working this way? Can anyone help me?
Your script works, but I think you are mistaken as to what it does. What you are doing is loading the assembly you downloaded, but from what I can see, you do nothing with it. The "loaded" assembly, will not replace any of the scripts you have, it will simply be a dynamically loaded assembly, which is only useful with reflection code if you want to do anything game wise (of which I see nothing)
A better method would be to have the assembly replace an already existing assembly in the project, meaning you should be able to use it like any other standard, included assembly :)
Thanks a lot. So is the only way updating a script to rebuild the whole project and to replace the DLLs?
Well, you could use a whole lot of reflection in your code, but that would be horribly slow. Besides those two options, you could also take the newly built dll's and only patch them :)
Your answer
Follow this Question
Related Questions
Android texture AssetBundles loading problem 0 Answers
Using Scripts in AssetBundles 0 Answers
Blender 2.71 - FBX exporter is not compatible 5 Answers