vexe / VFW

MIT License
492 stars 67 forks source link

Added GetComponent Alternatives for Interfaces #54

Closed james7132 closed 9 years ago

james7132 commented 9 years ago

GetIComponent and its variants works just like their normal UnityEngine counterparts (generic method, gets you components of a type), except that they also work with any applicable type.

For example:

// Interface type
public interface IColorable 
{
       ...
}

// Implementor
public class Wall : MonoBehaviour, IColorable 
{
      // Implementation
}

// Other component looking for any Implementor
public class Paint : MonoBehaviour 
{
      void OnCollisionEnter(Collision col)
      {
           var colorable = col.collider.gameObject.GetIComponent<IColorable>().
           ...
      }
}

I have also changed a number of the similar functions in the GameObjectExtensions class to utilize it.

vexe commented 9 years ago

I had a GetComponent extension that worked with interfaces but at some point (Unity 5 I think) I removed them because that's what the default GetComponent did. Unless they changed it back, there's no need for those extensions.

james7132 commented 9 years ago

Oh I didn't notice that they removed the generic constraint from the GetComponent variants. Though, with that in mind, the HasComponent methods shouldn't have the Component constraint then. Closing.