// 通过 scriptRef 还原脚本绑定关系 public bool CreateScriptInstance() { if (!_isScriptInstanced) { if (!string.IsNullOrEmpty(_scriptRef.modulePath) && !string.IsNullOrEmpty(_scriptRef.className)) { var runtime = ScriptEngine.GetRuntime(); if (runtime != null) { var context = runtime.isInitialized ? runtime.GetMainContext() : null; if (context != null) { var ctx = (JSContext)context; var snippet = $"require('{_scriptRef.modulePath}')['{_scriptRef.className}']"; var bytes = System.Text.Encoding.UTF8.GetBytes(snippet); var typeValue = ScriptRuntime.EvalSource(ctx, bytes, _scriptRef.sourceFile, false); if (JSApi.JS_IsException(typeValue)) { var ex = ctx.GetExceptionString(); Debug.LogError(ex); SetUnresolvedScriptInstance(); } else { var instValue = SetScriptInstance(ctx, typeValue, false); JSApi.JS_FreeValue(ctx, instValue); JSApi.JS_FreeValue(ctx, typeValue); // if (!instValue.IsObject()) // { // Debug.LogError("script instance error"); // } } } } else { if (!_isWaitingRuntime) { _isWaitingRuntime = true; ScriptEngine.RuntimeCreated += OnRuntimeCreated; } // Debug.LogError("script runtime not ready"); } } else { SetUnresolvedScriptInstance(); } } return(_isScriptInstanced); }
// 通过 scriptRef 还原脚本绑定关系 public bool SetScriptInstance() { if (!_isScriptInstanced) { if (!string.IsNullOrEmpty(scriptRef.modulePath) && !string.IsNullOrEmpty(scriptRef.className)) { var runtime = ScriptEngine.GetRuntime(); if (runtime != null && runtime.mainScriptRun) { var context = runtime.GetMainContext(); if (context != null) { var ctx = (JSContext)context; var snippet = $"require('{scriptRef.modulePath}')['{scriptRef.className}']"; var bytes = System.Text.Encoding.UTF8.GetBytes(snippet); var typeValue = ScriptRuntime.EvalSource(ctx, bytes, scriptRef.sourceFile, false); if (JSApi.JS_IsException(typeValue)) { var ex = ctx.GetExceptionString(); Debug.LogError(ex); CreateUnresolvedScriptInstance(); } else { var instValue = CreateScriptInstance(ctx, typeValue, false); JSApi.JS_FreeValue(ctx, instValue); JSApi.JS_FreeValue(ctx, typeValue); if (!instValue.IsObject()) { Debug.LogError("script instance error"); } } } } else { Debug.LogError("script runtime not ready"); } } else { CreateUnresolvedScriptInstance(); } } return(_isScriptInstanced); }