Ejemplo n.º 1
0
        /*
         *  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
                    RefreshSettingsList.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);
        }
Ejemplo n.º 2
0
        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
                        RefreshSettingsList.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);
        }