internal static GameObjectInfo GetGameObjectInfo(Type gameObject) { // Try and fetch the list of components from cache. if (Cache.GameObjects.TryGetValue(gameObject, out GameObjectInfo foundInfo)) { return(foundInfo); } // Throw an error if the Type provided isn't a GameObject. if (gameObject != typeof(GameObject) && !gameObject.IsSubclassOf(typeof(GameObject))) { throw new Exception(nameof(gameObject) + " is not a GameObject."); } // If an entry isn't found, create one. GameObjectInfo info = new GameObjectInfo(); // Components attribute. ComponentsAttribute componentsAttribute = gameObject .GetCustomAttributes(typeof(ComponentsAttribute), true) .FirstOrDefault() as ComponentsAttribute; if (componentsAttribute != null) { List <Type> components = new List <Type>(); foreach (Type t in componentsAttribute.Components) { components.Add(t); } info.Components = components; } // Headless attribute. HeadlessModeAttribute headlessAttribute = gameObject .GetCustomAttributes(typeof(HeadlessModeAttribute), true) .FirstOrDefault() as HeadlessModeAttribute; if (headlessAttribute != null) { info.HeadlessSupportMode = headlessAttribute.SupportMode; } // Add to cache and return components. info.Update(); Cache.GameObjects.Add(gameObject, info); return(info); }
internal static ComponentInfo GetComponentInfo(Type component) { // Try and fetch from cache. if (Cache.Components.TryGetValue(component, out ComponentInfo result)) { return(result); } // Throw exception if the Type isn't a component. if (component != typeof(Component) && !component.IsSubclassOf(typeof(Component))) { throw new Exception(nameof(component) + " is not a Component."); } // If an entry isn't found, create one. result = new ComponentInfo(); // Execute in editor attribute. ExecuteInEditorAttribute attribute = component .GetCustomAttributes(typeof(ExecuteInEditorAttribute), true) .FirstOrDefault() as ExecuteInEditorAttribute; if (attribute != null) { result.RunInEditor = true; } // Headless attribute. HeadlessModeAttribute headlessAttribute = component .GetCustomAttributes(typeof(HeadlessModeAttribute), true) .FirstOrDefault() as HeadlessModeAttribute; if (headlessAttribute != null) { result.HeadlessSupportMode = headlessAttribute.SupportMode; } // Add to cache and return. result.Update(); Cache.Components.Add(component, result); return(result); }