Unity 2019.3
I am writing a few extension methods to make my life a little bit easier, but this one is bogging me down.
I am loading stuff with Unity's Addressable system, and I would like use it by extension methods.
I am passing in my variables (reference type & value type, doesn't matter) to my extension methods, hoping to set them and and hide some complexity and source of bugs, and errors. It seems to me they are passed by value, because when I am trying to use them outside the extension methods, they are not set, null, etc. Not working.
There is a callback called Completed, and I am subscribing with a lambda expression...
I tried with the ref keyword, but obviously the compiler is yelling, that she doesn't want to pass my ref, out whatever parameters to local functions... :(
I tried it out with no local functions, and it did not work without the ref keywork, but it did with it!
You get the idea! Please see my code below!
This is the one that I can't get to work...
///Trying to set the generic variable data from the Addressable assetref
public static void TryLoadData<T>(this AssetReference assetref, T data)
{
var handle = assetref.LoadAssetAsync<T>();
handle.Completed += loadedresult =>
{
T result = loadedresult.Result;
if (result == null) return;
data = result;
};
}
This one does, but only with ref...
///Setting the generic data parameter to the parameter g.
public static void SwapTest<T>(this T g,ref T data) where T : class=> data = g;
I am not sure if there is a workaround, I thought of pointers, or global variables, but not sure which one is the right way to go.
Thanks