Hello there, I am trying to use my internal framework inside Unity and things work correctly in the editor but fail to build on the IL2CPP scripting backend.
The method is a simple ‘public void Set(in T component = default)’ defined on a struct (so it should not be the interface generic method limitation of IL2CPP) and if its usage is removed (while keeping usage of other generic methods of this struct without the in parameter) the project build successfully. I tried adding a link.xml file to preserve all my external assembly but it gives the same error.
I am on Unity 2020.2.2f1 using .NET Standard 2.0 api compatibility level by the way.
So either Unity doesn’t like generic in parameters or I am missing something else Thank you for your help.
I have other non generic methods with in parameter used and IL2CPP doesn’t seem to complain. Also default or default(T) should just be syntactic sugar, since my framework is referenced as a dll the IL should be the same.
Just to be sure I will try to make a empty unity project with such methods to see how it behave.
So I finally took some time to do a small code repro and it seems IL2CPP doesn’t like neither default nor default(T). Is this expected?
using UnityEngine;
public static class Container<T>
{
private static T _value;
// uncomment this and the build fail
//public static void Set1(in T value = default(T)) => Set2(value);
public static void Set2(in T value)
{
_value = value;
}
public static void Set3() => Set2(default);
public static ref T Get() => ref _value;
}
public class Test : MonoBehaviour
{
private SpriteRenderer _renderer;
// Start is called before the first frame update
void Start()
{
_renderer = GetComponent<SpriteRenderer>();
Container<Color>.Set3();
Container<Color>.Set2(Color.red);
}
// Update is called once per frame
void Update()
{
_renderer.color = Container<Color>.Get();
}
}