oleg-shilo / cs-script

C# scripting platform
http://www.cs-script.net
MIT License
1.61k stars 235 forks source link

Error with Unity on Android #348

Open yt6983138 opened 11 months ago

yt6983138 commented 11 months ago

I tried to use this on Unity on Android, and I end up with error like this:

11-04 18:47:23.718  4156  4301 E Unity   : TypeLoadException: Could not load type of field 'CSScriptLib.Runtime:CreateUnloadableAssemblyLoadContext' (2) due to: Could not resolve type with token 0100003e from typeref (expected class 'System.Runtime.Loader.AssemblyLoadContext' in assembly 'System.Runtime.Loader, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a') assembly:System.Runtime.Loader, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a type:System.Runtime.Loader.AssemblyLoadContext member:(null)
11-04 18:47:23.718  4156  4301 E Unity   :   at CSScriptLib.CodeDomEvaluator+<>c__DisplayClass7_0.<ReferenceAssembly>b__0 (System.String x) [0x00000] in <2d2b17d233f24918b1e8f6536596dfe2>:0
11-04 18:47:23.718  4156  4301 E Unity   :   at System.Linq.Enumerable.TryGetFirst[TSource] (System.Collections.Generic.IEnumerable`1[T] source, System.Func`2[T,TResult] predicate, System.Boolean& found) [0x0003f] in <22d097c9c77e4fb3988dec23d55249ed>:0
11-04 18:47:23.718  4156  4301 E Unity   :   at System.Linq.Enumerable.FirstOrDefault[TSource] (System.Collections.Generic.IEnumerable`1[T] source, System.Func`2[T,TResult] predicate) [0x00000] in <22d097c9c77

However, it runs completely fine on windows, here my code is:

IScript script = (IScript)CSScript.Evaluator.LoadCode(
    (string)Skins.ReadSkin().Properties[AssignedObject.name][Resource.keyRefrence["Execute"]]
);
AssignedObject.GetComponent<Button>().onClick.AddListener(delegate
{
    script.Execute(AssignedObject);
});
// Can be turned into this(simplified code): --------------------------------------------------------
IScript script = (IScript)CSScript.Evaluator.LoadCode(
    "using System; using UnityEngine; using UnityEngine.SceneManagement; class Script : IScript { public void Execute(GameObject obj) { SceneManager.LoadScene(\"Settings\"); } }" 
    //sorry for 1 line code
);
script.Execute(GameObjectHere); // type GameObject is defined in UnityEngine
// interface IScript -------------------------------------------------------
public interface IScript
{
    public void Execute(GameObject obj);
}

Also I tried both roslyn and codedom evaluator, neither of them works. btw, what I'm trying to do is make a scripting engine on my game, which user can define what to do when button clicked. Configuration: Mono compiler(didn't try il2cpp because obviously it wouldn't work), android 11 with root access, 6g ram on phone, Unity 2022.3.10f1

ps: if i didnt reply for a long time its prob because 11.5hrs of school every weekday 😭

oleg-shilo commented 11 months ago

It looks like there is a difference between an Android and a desktop .NET distro. Not sure if there is a work around

yt6983138 commented 11 months ago

aw, i guess ill have to leave scripting feature in my game unimplemented thanks for the reply anyway