Exemple #1
0
    IEnumerator LoadHotFixAssembly()
    {
        //***创建flashplayer***
        //***建议全局只使用一个as3运行时。因此将此脚本设置为一直存在,并将flashplayer保存在此对象中。
        var flashplayer = new ASRuntime.Player();

        //此处从StreamingAssetsPath中加载字节码,实际热更可从网络下载后执行。
        //The byte code is loaded from the Streamingassetspath, and the actual project can be downloaded from the network and executed.
#if UNITY_ANDROID
        WWW www = new WWW(Application.streamingAssetsPath + "/hotfix.cswc");
#else
        WWW www = new WWW("file:///" + Application.streamingAssetsPath + "/hotfix.cswc");
#endif
        while (!www.isDone)
        {
            yield return(null);
        }
        if (!string.IsNullOrEmpty(www.error))
        {
            UnityEngine.Debug.LogError(www.error);
        }

        //加载as3编译器生成的字节码。
        //Loads the byte code generated by the compiler.
        ASBinCode.CSWC swc = ASBinCode.CSWC.loadFromBytes(www.bytes);
        www.Dispose();

        ASRuntime.nativefuncs.BuildInFunctionLoader.loadBuildInFunctions(swc);

        var extfunctions = new ASRuntime.extFunctions();
        //加载导出的Unity API
        //Loading exported APIs
        var regenumerator = extfunctions.registrationFunction(swc);

        //**注册本地代码有可能非常之多。所以提供了一个进度条**
        //API code can be very much. So a progress bar is provided
        int functioncount = 0;
        while (regenumerator.MoveNext())
        {
            functioncount++;
            if (functioncount % 50 == 0)
            {
                if (progress != null)
                {
                    progress.value     = extfunctions.progress;
                    progressValue.text = "loading:" + extfunctions.progress * 100 + "%";
                }
                yield return(null);
            }
        }
        if (progress != null)
        {
            progress.value     = extfunctions.progress;
            progressValue.text = "loading:" + extfunctions.progress * 100 + "%";
        }
        yield return(null);


        //注册额外的代码,这些代码为MonoBehaviour提供支持
        //Register additional code that provides support for MonoBehaviour
        GObj_Special.registSpecialFunctions(swc);

        if (progress != null)
        {
            progress.gameObject.SetActive(false);
            progressValue.gameObject.SetActive(false);
        }

        this.player = flashplayer;

        flashplayer.loadCode(swc);

        if (string.IsNullOrEmpty(DocumentClass))
        {
            Debug.LogError("请指定AS3入口文档类");
            yield break;
        }

        main         = flashplayer.createInstance(DocumentClass);
        updatemethod = flashplayer.getMethod(main, "update");
    }
        protected void createAs3Object(string prototype)
        {
            as3Object = player.createInstance(prototype);

            afterMergeFrom();
        }