What InternalCall means ?

Hello,

I’m trying to understand what the EditorGUIUtility.SetIconForObject do.
When i check in the UnityEditor.dll, i can found this :

    [MethodImpl(MethodImplOptions.InternalCall)]
    internal static extern void SetIconForObject(UnityEngine.Object obj, Texture2D icon);

It looks like it’s the CLR function that is call. But what does it means ?
How to know what this function do exactly ?
How “Unity” known that this function exists and how to use it ?

Thanks for your help.

Unity use Mono as it’s CLR. In Mono, it use mono_add_internal_call method to register native function. For example, you have a class below:

namespace MyNamespace
{
    public class MyClass
    {
        [MethodImpl(MethodImplOptions.InternalCall)]
        public extern static void MyMethod();
    }
}

So to add internal call, you need to do something like this in your C++ code:

void MyMethod()
{
    /* Some code there */
    int a = 1;
}

void function_to_add_internal_call()
{
    mono_add_internal_call("MyNamespace.MyClass::MyMethod", MyMethod);
}

https://www.mono-project.com/docs/advanced/embedding

1 Like

In general if you are looking to call into native the recommended way is to use PInvoke rather than Mono’s icall.

I found that Unity can pass a List or return an array from native code. Is this functionality only achievable through internal calls? I can’t do it with P/Invoke.

I tried it, and it seems to work without the FreeFunction attribute. I was also able to define an extern method inside a non-static class. When calling the method, the current C# object is passed as a MonoObject* to native code, allowing me to call C# methods back from C++ using the field. This wouldn’t work with P/Invoke, which makes it really cool!

However, I encountered an issue: when I make changes to the C# code, the DLL used by my native plugin doesn’t get refreshed unless I restart Unity. Do you have any advice on how to reload the DLL for my native plugin?