C#

Fools ignore complexity; pragmatists suffer it; experts avoid it; CREATIVES remove it.

Friday, November 23, 2018

Does it implement this interface? (C# Unity)


Does it implement this interface?

Sometimes when using Unity, you need to know, in realtime, if your object has implemented such interface, here is a function that can help.


    static public T HasImplementedInterface(GameObject o)
    {
        return o.GetComponents().OfType().FirstOrDefault();
    }

if you don't want to use RTT System.linq, you can do it even easier:


//imagine an interface called ICarPanel
ICarPanel cp = obj as ICarPanel;
if (cp!=null)
Debug.Log("Implements ICarPanel");


:) !

No comments:

Powered By Blogger