Search Unity

How to pass a function as an argument in Unity

Discussion in 'Scripting' started by AnalogUniverse, Dec 13, 2018.

  1. AnalogUniverse

    AnalogUniverse

    Joined:
    Aug 10, 2018
    Posts:
    64
    Hi All

    Im currently trying to create my own Tween script and I want to be able to set the property / method the script acts on for example the position or the scale or the rotation or the Alpha or any other property I might want, of any argument type int float ect, In objective C this was done with Selectors where you would pass in a string representing the property / method name which you would then convert into a selector, but Im struggle to find how to do this in Unity and C# I also want to be able to set this from a custom inspector.

    so my public properties would be something like
    float startValue;
    float endValue;
    double duration;
    ???? functionAndValueToUpdate;

    What would be the accepted best most efficient way of doing this in Unity Any help greatly appreciated
     
  2. NoDumbQuestion

    NoDumbQuestion

    Joined:
    Nov 10, 2017
    Posts:
    186
    I am not entirely sure what you trying to do but this is closest thing you might get

    Code (CSharp):
    1.     public void SomeFunction(params object[] args)
    2.     {
    3.         foreach (var arg in args)
    4.         {
    5.             if (arg is float)
    6.             {
    7.                 // Do something with float?
    8.             }
    9.             else if (arg is customData)
    10.             {
    11.                 // Do something with customData?
    12.             }
    13.         }
    14.     }
    Edit: you might need to make custom data struct or class to pass value to the function
     
  3. Deleted User

    Deleted User

    Guest

    Or try a lambda function. Same diff.
     
  4. AnalogUniverse

    AnalogUniverse

    Joined:
    Aug 10, 2018
    Posts:
    64
    Thanks for the replies
    Are delegates the accepted way of doing this that is to say do the existing tween engines such as DoTween use delegates, or do they use c# actions or Unity events baring in mind I want the property to appear in my custom inspector, ideally as a drop down menu listing the available methods of a given object

    Thanks
     
  5. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    In C#, "action" is just a shortcut for defining delegate types.
    https://docs.microsoft.com/en-us/dotnet/api/system.action-1?view=netframework-4.7.2

    In C#, if you want to pass a callable function as an argument to another function, the normal way to do that is with delegates. I can't speak to DoTween specifically, but LeanTween uses delegates.

    I'm not sure what you'll need to do to write your custom inspector, though. If you want to generate a dropdown list of available methods, you'll probably need to rely on reflection. And you'll presumably need to serialize your data at some point, and I don't think you can directly serialize a delegate (at least not in a way that will remain compatible if the source code you're referring to changes), so you might need to fall back on function names or something.

    Unity has presumably already figured out a way to handle serialization for UnityActions in order to make their own inspectors work, so you might have an easier time using that, but I'm not familiar with any of the details.