public static List <Type> GetAllUnityComponents()
        {
            if (null == _availableUnityComponents)
            {
                var types = GuiReflector.GetAllLoadedTypesAsignableFrom(typeof(Component));  // UnityEngine.Component

#if DEBUG
                if (DebugMode)
                {
                    StringBuilder sb = new StringBuilder();
                    if (types.Count == 0)
                    {
                        sb.AppendLine("No styleable classes available.");
                    }
                    else
                    {
                        foreach (Type type in types)
                        {
                            sb.AppendLine(string.Format("    -> {0}", type));
                        }
                    }

                    Debug.Log(string.Format(@"====== Unity components ======
{0}", sb));
                }
#endif
                _availableUnityComponents = types;
                //Debug.Log("_availableUnityComponents: " + _availableUnityComponents.Count);
            }

            return(_availableUnityComponents);
        }
        /// <summary>
        /// Initializes the Singleton instance
        /// </summary>
        private void Initialize()
        {
            _adapters = new Dictionary <Type, StyleClientAdapterBase>();

            var adapters = GuiReflector.GetAllLoadedTypesDecoratedWith(typeof(StyleClientAdapterAttribute));

#if DEBUG
            if (DebugMode)
            {
                Debug.Log(string.Format(@"Found {0} adapters", adapters.Count));
            }
#endif
            foreach (var adapter in adapters)
            {
                if (!(typeof(StyleClientAdapterBase).IsAssignableFrom(adapter)))
                {
                    throw new Exception(string.Format("{0} is not StyleClientAdapterBase", adapter));
                }

                var attributes = CoreReflector.GetClassAttributes <StyleClientAdapterAttribute>(adapter);
                var client     = (StyleClientAdapterBase)Activator.CreateInstance(adapter);
                _adapters[attributes[0].Type] = client;
            }

#if DEBUG
            if (DebugMode)
            {
                Debug.Log(string.Format(@"Retrieved {0} style client adapters:
{1}", _adapters.Count, DictionaryUtil <Type, StyleClientAdapterBase> .Format(_adapters)));
            }
#endif
        }
        /*private static int ClassNameSort(Type x, Type y)
         * {
         *  return String.Compare(x.Name, y.Name, StringComparison.OrdinalIgnoreCase);
         * }*/

        #endregion

        #region Skin parts

        /// <summary>
        /// Describes skin parts
        /// </summary>
        /// <param name="componentType"></param>
        /// <returns></returns>
        public static string GetSkinParts(Type componentType)
        {
            if (!typeof(SkinnableComponent).IsAssignableFrom(componentType))
            {
                return(string.Format(@"Skin parts: Not a skinnable component." + NewLine + NewLine));
            }

            var dict = GuiReflector.GetSkinParts(componentType); // string->bool

            var list = new List <string>();

            foreach (string key in dict.Keys)
            {
                list.Add(key);
            }
            list.Sort();

            StringBuilder sb = new StringBuilder();

            foreach (var name in list)
            {
                MemberWrapper mw = new MemberWrapper(componentType, name);
                sb.AppendLine(string.Format("{0} [Type: {1}, Required: {2}]", name, mw.MemberType, dict[name]));
            }

            return(string.Format(@"Skin parts ({0}):
{1}
{2}", list.Count, Line, sb) + NewLine /* + NewLine*/);
        }
        /// <summary>
        /// Used by designer
        /// </summary>
        /// <param name="componentType"></param>
        public static List <Type> GetSkinClasses(Type componentType)
        {
            List <Type> list = new List <Type>();

            //Debug.Log("componentType: " + componentType);
            if (!componentType.IsSubclassOf(typeof(SkinnableComponent)))
            {
                //Debug.LogError("Component is not a subclass of SkinnableComponent: " + componentType);
                return(list);
            }

            List <Type> types = GuiReflector.GetAllLoadedTypes();

            foreach (Type type in types)
            {
                if (!type.IsClass)
                {
                    continue;
                }

                if (!type.IsSubclassOf(typeof(Skin)))
                {
                    continue;
                }

                var componentTypeSpecifiedInAttribute = SkinUtil.GetHostComponent(type);
                if (componentTypeSpecifiedInAttribute == componentType)
                {
                    list.Add(type);
                }
            }

            return(list);
        }
        public static Type GetTypeByClassName(string className)
        {
            var types = GuiReflector.GetAllLoadedTypes();

            return(types.Find(delegate(Type t)
            {
                return /*t.Namespace == nameSpace && */ t.Name == className;
            }));
        }
        //private static List<Type> _allTypes;
        //public static List<Type> GetAllLoadedTypes()
        //{
        //    /**
        //     * 1. Get all types for all loaded assemblies
        //     * This is done only when componet tab expanded, so is no performance issue
        //     * */

        //    if (null == _allTypes)
        //    {
        //        _allTypes = new List<Type>();
        //        Assembly[] loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
        //        foreach (Assembly assembly in loadedAssemblies)
        //        {
        //            _allTypes.AddRange(assembly.GetTypes());
        //        }
        //    }

        //    return _allTypes;
        //}

        public static bool IsUniqueScriptName(string className)
        {
            var types = GuiReflector.GetAllLoadedTypes();
            //Debug.Log(string.Format(@"nameToTest: {0}", className));
            //Debug.Log(string.Format(@"types: {0}", types.Count));
            bool found = types.Exists(delegate(Type t)
            {
                return /*t.Namespace == nameSpace && */ (t.Name == className);
            });

            return(!found);
        }
        internal void RefreshComponentList()
        {
            _showComponents          = EditorSettings.ReferenceShowComponents;
            _showSkinnableComponents = EditorSettings.ReferenceShowSkinnableComponents;
            _showSkins = EditorSettings.ReferenceShowSkins;

            _selectedIndex    = -1;
            _selectionChanged = false;

            _classes.Clear();

            var allClasses = GuiReflector.GetAllLoadedTypes();

            foreach (var type in allClasses)
            {
                if (typeof(Component).IsAssignableFrom(type))
                {
                    if (!string.IsNullOrEmpty(_searchText) && !PassesSearchFilter(type.FullName, _searchText))
                    {
                        /*!type.FullName.ToUpper().Contains(_searchText.ToUpper())*/
                        continue;
                    }
                    _classes.Add(type);
                }
            }

            _classes.Sort(TypeSort);
            //Debug.Log("_classes: " + _classes.Count);

            List <GUIContent> contentList = new List <GUIContent>();

            foreach (var @class in _classes)
            {
                var isSkinnableComponent = typeof(SkinnableComponent).IsAssignableFrom(@class);
                var isSkin            = typeof(Skin).IsAssignableFrom(@class);
                var isSimpleComponent = !isSkinnableComponent && !isSkin;
                var texture           = GuiComponentEvaluator.EvaluateComponentRowIcon(@class);

                if (_showComponents && isSimpleComponent ||
                    _showSkinnableComponents && isSkinnableComponent ||
                    _showSkins && isSkin)
                {
                    contentList.Add(new GUIContent(" " + @class.FullName, texture));
                }
            }

            _contents = contentList.ToArray();
            //Debug.Log("_contents: " + _contents.Length);
        }
        /// <summary>
        /// Initializes the Singleton instance
        /// </summary>
        private void Initialize()
        {
            _allModules.Clear();

            //var providerTypes = ReflectionUtil.GetAllLoadedTypesDecoratedWith(typeof (StyleModuleAttribute));
            var moduleTypes = GuiReflector.GetAllLoadedTypesAsignableFrom(typeof(StyleModuleBase));

            foreach (var moduleType in moduleTypes)
            {
                if (moduleType == typeof(StyleModuleBase))
                {
                    continue; // skip the abstract class
                }
                var module = (StyleModuleBase)Activator.CreateInstance(moduleType);
                module.ReflectAttribute();

                _allModules.Add(module);
            }
        }
        /// <summary>
        /// Describes skin states
        /// </summary>
        /// <param name="componentType"></param>
        /// <returns></returns>
        public static string GetSkinStates(Type componentType)
        {
            if (!typeof(SkinnableComponent).IsAssignableFrom(componentType))
            {
                return(string.Format(@"Skin states: Not a skinnable component." + NewLine + NewLine));
            }

            var list = GuiReflector.GetSkinStates(componentType); // string->bool

            StringBuilder sb = new StringBuilder();

            foreach (var name in list)
            {
                sb.AppendLine(name);
            }

            return(string.Format(@"Skin states ({0}):
{1}
{2}", list.Count, Line, sb) + NewLine /* + NewLine*/);
        }
        public static Type GetTypeByFullName(string name)
        {
            if (null == _allTypesDict)
            {
                _allTypesDict = new Dictionary <string, Type>();
                //Type myType = Type.GetType(name);
                var allTypes = GuiReflector.GetAllLoadedTypes();
                foreach (Type type in allTypes)
                {
                    _allTypesDict[type.FullName] = type;
                }
            }

            if (_allTypesDict.ContainsKey(name))
            {
                return(_allTypesDict[name]);
            }

            return(null);
        }
        /// <summary>
        /// Looks for all descriptor classes in running assemblies via reflection
        /// </summary>
        public static void Initialize()
        {
            //Debug.Log("Initialize");

            var toolbox = Toolbox.Instance;

            toolbox.Clear();

            /**
             * 1. Get all types for all loaded assemblies
             * This is done only when componet tab expanded, so is no performance issue
             * */
            List <Type> types = GuiReflector.GetAllLoadedTypes();

            foreach (Type type in types)
            {
                if (type.IsClass)
                {
                    if (type.IsSubclassOf(typeof(ComponentAdapter)))
                    {
                        string icon      = string.Empty;
                        string label     = string.Empty;
                        string groupName = string.Empty;
                        string tooltip   = string.Empty;

                        //ToolboxAttribute[] toolboxAttributes = (ToolboxAttribute[])type.GetCustomAttributes(typeof(ToolboxAttribute), true);
                        var toolboxAttributes = Core.Reflection.CoreReflector.GetClassAttributes <ToolboxAttribute>(type);

                        if (toolboxAttributes.Count > 0)
                        {
                            var attr = toolboxAttributes[0];
                            label     = attr.Label;
                            icon      = attr.Icon;
                            tooltip   = attr.Tooltip;
                            groupName = attr.Group;
                        }

                        Texture iconTexture = null;

                        if (!string.IsNullOrEmpty(icon))
                        {
                            iconTexture = (Texture)Resources.Load(icon);
                        }

                        if (string.IsNullOrEmpty(label))
                        {
                            label = type.Name.Replace("Adapter", string.Empty);
                        }

                        var desc = new ComponentTypeDesc(label, type, iconTexture, tooltip);

                        if (type.IsAbstract)
                        {
                            continue; // skip abstract classes (SkinnableComponentAdapter etc.)
                        }

                        /**
                         * 1. Stage
                         * */
                        if (type == typeof(StageAdapter))
                        {
                            Toolbox.Instance.StageContent = desc.GetContent();
                            continue;
                        }

                        /**
                         * 2. Named group
                         * */
                        if (!string.IsNullOrEmpty(groupName))
                        {
                            desc.Group = groupName;
                            toolbox.Add(desc);
                            continue;
                        }

                        /**
                         * 3. Group
                         * */
                        //if (type == typeof (GroupAdapter) || type.IsSubclassOf(typeof (GroupAdapter)))
                        if (typeof(GroupAdapter).IsAssignableFrom(type))
                        {
                            desc.Group = "Containers";
                            toolbox.Add(desc);
                            continue;
                        }

                        /**
                         * 4. Component
                         * */
                        desc.Group = "Components";
                        toolbox.Add(desc);
                    }
                }
            }

            toolbox.Process();
        }
        /// <summary>
        /// Re-scanns all the media queries on demand
        /// </summary>
        /// <exception cref="Exception"></exception>
        public void Rescan()
        {
            _methodInfos = new Dictionary <string, MethodInfo>();
            _queries     = new Dictionary <string, MediaQuery>();

            /*foreach (Type type in GlobalTypeDictionary.Instance.Values)
             * {
             *  if (typeof(MediaQueryBase).IsAssignableFrom(type) && typeof(MediaQueryBase) != type)
             *  {
             *      MediaQueryBase query = (MediaQueryBase)Activator.CreateInstance(type);
             *      _queries[query.Id] = query;
             *  }
             * }*/

            var queries = GuiReflector.GetMethodsInAllLoadedTypesDecoratedWith(typeof(MediaQueryAttribute));

#if DEBUG
            if (DebugMode)
            {
                Debug.Log(string.Format(@"Found {0} media queries", queries.Count));
            }
#endif

            foreach (var methodInfo in queries)
            {
                if (null == methodInfo.ReturnParameter || methodInfo.ReturnType != typeof(bool))
                {
                    throw new Exception(@"Method decorated with MediaQuery attribute should return a boolean value:
" + methodInfo);
                }

                var p          = methodInfo.GetParameters();
                var parameters = new List <Type>();
                foreach (var parameterInfo in p)
                {
                    parameters.Add(parameterInfo.ParameterType);
                }

                // get attribute
                var attributes = CoreReflector.GetMethodAttributes <MediaQueryAttribute>(methodInfo);
                if (attributes.Count == 0)
                {
                    throw new Exception(@"Cannot find MediaQuery attribute:
" + methodInfo);
                }

                var attribute = attributes[0];

                /**
                 * If there is already an existing query with a same ID, allow overriding
                 * only if this is an editor override. This way we'll get all the editor overrides
                 * override the Play mode media queries.
                 * */
                if (_methodInfos.ContainsKey(attribute.Id) && !attribute.EditorOverride)
                {
                    continue;
                }

                _methodInfos[attribute.Id] = methodInfo;

                MediaQuery query;
                if (parameters.Count > 0)
                {
                    var type = parameters[0];
                    query = (MediaQuery)NameValueBase.CreateProperty <MediaQuery>(attribute.Id, type);
                    //query.Value = type.
                }
                else
                {
                    query = new MediaQuery
                    {
                        Name       = attribute.Id,
                        Parameters = parameters.ToArray()
                    };
                }
                _queries[attribute.Id] = query;
            }

#if DEBUG
            if (DebugMode)
            {
                Debug.Log(string.Format(@"Number of queries after overrides: {0}", _queries.Keys.Count));
            }
#endif

#if DEBUG
            if (DebugMode)
            {
                Debug.Log(string.Format(@"Retrieved {0} media queries:
{1}", _queries.Count, DictionaryUtil <string, MediaQuery> .Format(_queries)));
            }
#endif
        }