using UnityEngine;
using System.Collections.Generic;

public static class GameObjectExtensions
{
	static public T GetInterface<T>(this GameObject value) where T : class
	{
		return value.GetComponent(typeof(T)) as T;
	}
	
	static public T GetInterfaceInParent<T>(this GameObject value) where T : class
	{
		return value.GetComponentInParent(typeof(T)) as T;
	}

    public static void BroadcastMessageToAll(string methodName, object parameter)
    {
        GameObject[] gameObjects = (GameObject[])GameObject.FindObjectsOfType(typeof(GameObject));
        foreach (GameObject gameObject in gameObjects)
        {
            if (gameObject && gameObject.transform.parent == null)
            {
                gameObject.gameObject.BroadcastMessage(methodName, parameter, SendMessageOptions.DontRequireReceiver);
            }
        }
    }

    public static void BroadcastMessageToAll(string methodName)
    {
        GameObject[] gameObjects = (GameObject[])GameObject.FindObjectsOfType(typeof(GameObject));
        foreach (GameObject gameObject in gameObjects)
        {
            if (gameObject && gameObject.transform.parent == null)
            {
                gameObject.gameObject.BroadcastMessage(methodName, SendMessageOptions.DontRequireReceiver);
            }
        }
    }

    public static void SetLayerRecursively(this GameObject obj, int newLayer)
    {
        obj.layer = newLayer;

        foreach (Transform child in obj.transform)
        {
            SetLayerRecursively(child.gameObject, newLayer);
        }
    }
 }
