protected virtual void OnEnable()
        {
            _typeName    = target.GetType().Name;
            _displayType = (string)target.GetType().GetField("DISPLAYTYPE", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null);
            _valueType   = (string)target.GetType().GetField("VALUETYPE", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null);
            _dataType    = serializedObject.FindProperty("_dataType");
            _description = serializedObject.FindProperty("_description");

            _updateDecoratorsMulti = target.GetType().GetMethodPrivate("UpdateDecorators", BindingFlags.NonPublic | BindingFlags.Instance);
            _decorators            = serializedObject.FindProperty("_decorators");

            if (!_const)
            {
                _getRelay              = CacheGetRelay();
                _getListenerCount      = _getRelay.FieldType.GetProperty("listenerCount", BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy);
                _getListeners          = _getRelay.FieldType.GetField("_listeners", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);
                _isOnRestoreOverridden = IsOnRestoreOverridden(target.GetType());
            }

            #region Cache available decorator types
            _decoratorTypes.Clear();

            System.Type tDecBase = null;
            _smartType = SmartEditorUtils.GetSmartObjectType(target as SmartBase, out _tData);
            switch (_smartType)
            {
            case SmartEditorUtils.SmartObjectType.EVENT:
            case SmartEditorUtils.SmartObjectType.EVENT_MULTI:
                // Event decorators
                tDecBase = typeof(SmartEventDecoratorBase);
                break;

            case SmartEditorUtils.SmartObjectType.VAR:
            case SmartEditorUtils.SmartObjectType.MULTI:
                tDecBase = typeof(SmartDataDecoratorBase <>).MakeGenericType(_tData);
                break;

            case SmartEditorUtils.SmartObjectType.SET:
                tDecBase = typeof(SmartSetDecoratorBase <>).MakeGenericType(_tData);
                break;
            }

            if (tDecBase != null)
            {
                foreach (var a in System.AppDomain.CurrentDomain.GetAssemblies())
                {
                    foreach (var t in a.GetTypes())
                    {
                        if (!t.IsAbstract && t.IsSubclassOf(tDecBase))
                        {
                            _decoratorTypes.Add(t);
                        }
                    }
                }
            }
            #endregion

            _decBtnStyle = new GUILayoutOption[] {
                GUILayout.MaxWidth(18), GUILayout.MaxHeight(18)
            };
            _iconChevronUp   = Resources.Load <Texture2D>("GUI/chevron_up");
            _iconChevronDown = Resources.Load <Texture2D>("GUI/chevron_down");
            _iconWarn        = EditorGUIUtility.Load("icons/_Help.png") as Texture2D;
            _iconGo          = EditorGUIUtility.IconContent("GameObject Icon").image as Texture2D;
            _iconAsset       = EditorGUIUtility.IconContent("Prefab Icon").image as Texture2D;
            _iconSmart       = SmartEditorUtils.LoadSmartIcon(_smartType);

            PopulateRefs();

            EditorApplication.update += Update;
        }
Beispiel #2
0
        void PostProcessMeta()
        {
            #region Repair GUIDS
            bool refresh = false;
            foreach (var a in scriptAbsPathToGuid)
            {
                // Get latest guid of script from assetdatabase and compare to cached to see if changed
                string newGuid = AssetDatabase.AssetPathToGUID(SmartEditorUtils.ToRelativePath(a.Key));
                if (newGuid != a.Value)
                {
                    Debug.LogWarningFormat("Repairing changed GUID for script {0}", a.Key);
                    string metaPath = a.Key + ".meta";

                    #region Repair meta file
                    if (File.Exists(metaPath))
                    {
                        // Find line with latest guid and replace with cached guid
                        var lines = File.ReadAllLines(a.Key);
                        for (int i = 0; i < lines.Length; ++i)
                        {
                            string l = lines[i];
                            if (l.Contains(newGuid))
                            {
                                Debug.LogFormat("\n\tRestoring {0} to {1}", newGuid, a.Value);
                                lines[i] = l.Replace(newGuid, a.Value);

                                // Save and flag for asset database refresh
                                File.WriteAllLines(a.Key, lines);
                                refresh = true;
                                break;
                            }
                        }
                    }
                    #endregion
                }
            }
            if (refresh)
            {
                AssetDatabase.Refresh();
            }
            #endregion

            #region Set icons
            refresh = false;
            foreach (var a in scriptAbsPathToGuid)
            {
                string metaPath = a.Key + ".meta";
                if (File.Exists(metaPath))
                {
                    var lines = File.ReadAllLines(metaPath);

                    // Read meta file, look for line with "icon:"
                    for (int i = 0; i < lines.Length; ++i)
                    {
                        string l = lines[i];

                        // Line must have "icon: " and default icon stuff
                        if (l.Contains(ICON_MATCH))
                        {
                            // Get type of script, then get associated icon from utilities
                            string      pathToScript = AssetDatabase.GUIDToAssetPath(a.Value);
                            MonoScript  script       = AssetDatabase.LoadAssetAtPath <MonoScript>(pathToScript);
                            System.Type tData;
                            var         sot = SmartEditorUtils.GetSmartObjectType(script.GetClass(), out tData);

                            //TODO Implement icons for component types
                            if (sot == SmartEditorUtils.SmartObjectType.NONE)
                            {
                                break;
                            }
                            Texture2D icon = SmartEditorUtils.LoadSmartIcon(sot, false, true);

                            // Get guid of icon and jam it in there
                            string pathToIcon = AssetDatabase.GetAssetPath(icon);
                            string iconGuid   = AssetDatabase.AssetPathToGUID(pathToIcon);

                            bool modified = false;
                            if (l.Contains(ICON_DEFAULT))
                            {
                                // Replace entire default icon
                                l        = l.Replace(ICON_DEFAULT, string.Format("{{fileID: 2800000, guid: {0}, type: 3}}", iconGuid));
                                modified = true;
                            }
                            else if (l.Contains(ICON_CUSTOM_HEAD))
                            {
                                // Isolate guid to check if changed
                                string check = l.Trim().Replace(ICON_MATCH, "").Replace(ICON_CUSTOM_HEAD, "").Replace(ICON_CUSTOM_TAIL, "");
                                if (check != iconGuid)
                                {
                                    var sb = new System.Text.StringBuilder();

                                    // Whitespace
                                    for (int j = 0; j < l.Length; ++j)
                                    {
                                        if (l[j] != ' ')
                                        {
                                            break;
                                        }
                                        sb.Append(' ');
                                    }

                                    // Build with new guid
                                    sb.Append(ICON_MATCH);
                                    sb.Append(ICON_CUSTOM_HEAD);
                                    sb.Append(iconGuid);
                                    sb.Append(ICON_CUSTOM_TAIL);
                                    l = sb.ToString();

                                    modified = true;
                                }
                            }

                            if (modified)
                            {
                                Debug.Log("Setting icon for script " + a.Key);
                                // Save and flag for asset database refresh
                                lines[i] = l;
                                File.WriteAllLines(metaPath, lines);
                                refresh = true;
                            }
                            break;
                        }
                    }
                }
                else
                {
                    Debug.LogError("Meta file not found: " + metaPath);
                }
            }
            #endregion

            if (refresh)
            {
                AssetDatabase.Refresh();
            }
        }