/*
         *  if the name parameter is null, we jsut return the first of type(T)
         *  we find
         */
        static T GetSettingsInternal <T> (string name, bool firstTry) where T : GameSettingsObject
        {
            if (settings == null)
            {
                return(null);
            }

            // during editor, just search by for loop
            if (!Application.isPlaying)
            {
                for (int i = 0; i < settings.Length; i++)
                {
                    GameSettingsObject gs = settings[i];
                    T t = gs as T;
                    if (t != null)
                    {
                        if (name == null || t.name == name)
                        {
                            return(t);
                        }
                    }
                }
                if (firstTry)
                {
                    // try and refresh teh settings list if we cant find it
                    GameSettingsList.RefreshGameSettingsList();
                    return(GetSettingsInternal <T>(name, false));
                }
            }
            else
            {
                if (typesLookups == null)
                {
                    InitializeRuntimeSettingsLookups();
                }
                Dictionary <string, GameSettingsObject> namesLookup;
                if (typesLookups.TryGetValue(typeof(T), out namesLookup))
                {
                    if (name == null)
                    {
                        if (namesLookup.Count > 1)
                        {
                            Debug.LogWarning("Multiple Settings Objects of type " + typeof(T).FullName + " only returning first. Use 'GetSettings<T> (string name)' for more specific object");
                        }
                        return(namesLookup.First().Value as T);
                    }
                    else
                    {
                        GameSettingsObject gs;
                        if (namesLookup.TryGetValue(name, out gs))
                        {
                            return(gs as T);
                        }
                    }
                }
            }

            Debug.LogError("Couldnt find Settings Object of type " + typeof(T).FullName + " named '" + name + "'.");
            return(null);
        }
        static List <T> GetSettingsOfTypeInternal <T> (bool firstTry) where T : GameSettingsObject
        {
            List <T> r = new List <T>();

            if (settings == null)
            {
                return(r);
            }

            // during editor, just search by for loop
            if (!Application.isPlaying)
            {
                for (int i = 0; i < settings.Length; i++)
                {
                    GameSettingsObject gs = settings[i];
                    T t = gs as T;
                    if (t != null)
                    {
                        r.Add(t);
                    }
                }
                if (r.Count == 0)
                {
                    if (firstTry)
                    {
                        // try and refresh teh settings list if we cant find it
                        GameSettingsList.RefreshGameSettingsList();
                        return(GetSettingsOfTypeInternal <T>(false));
                    }
                }
            }
            else
            {
                if (typesLookups == null)
                {
                    InitializeRuntimeSettingsLookups();
                }
                Dictionary <string, GameSettingsObject> namesLookup;
                if (typesLookups.TryGetValue(typeof(T), out namesLookup))
                {
                    foreach (var k in namesLookup.Keys)
                    {
                        r.Add(namesLookup[k] as T);
                    }
                }
            }
            if (r.Count == 0)
            {
                Debug.LogError("Couldnt find any Settings Object of type " + typeof(T).FullName);
            }
            return(r);
        }
        static void InitializeRuntimeSettingsLookups()
        {
            typesLookups = new Dictionary <Type, Dictionary <string, GameSettingsObject> >();
            for (int i = 0; i < settings.Length; i++)
            {
                GameSettingsObject gs = settings[i];
                Type t = gs.GetType();

                Dictionary <string, GameSettingsObject> namesLookup;
                if (typesLookups.TryGetValue(t, out namesLookup))
                {
                    namesLookup.Add(gs.name, gs);
                }
                else
                {
                    typesLookups.Add(t, new Dictionary <string, GameSettingsObject>()
                    {
                        { gs.name, gs }
                    });
                }
            }
        }