コード例 #1
0
        /// <inheritdoc cref="IDrawer.BuildRightClickMenu" />
        protected override void BuildContextMenu(ref Menu menu, bool extendedMenu)
        {
            if (BuildContextMenuItemsStartingFromBaseClass)
            {
                base.BuildContextMenu(ref menu, extendedMenu);
            }

                        #if UNITY_EDITOR
            string localPath = AssetDatabase.GetAssetPath(Target);
            if (localPath.Length > 0)
            {
                string fullPath = FileUtility.LocalToFullPath(localPath);
                if (fullPath.Length > 0)
                {
                    menu.AddSeparatorIfNotRedundant();
                    menu.Add("Disable", Disable);
                }
            }
                        #endif

            if (!BuildContextMenuItemsStartingFromBaseClass)
            {
                base.BuildContextMenu(ref menu, extendedMenu);
            }
        }
コード例 #2
0
        public Dictionary <string, string> FetchMemberTooltips([NotNull] MonoScript monoScript)
        {
            string scriptAssetLocalPath = AssetDatabase.GetAssetPath(monoScript);
            string scriptAssetPath      = FileUtility.LocalToFullPath(scriptAssetLocalPath);

            return(FetchMemberTooltips(scriptAssetPath, monoScript.GetClass()));
        }
コード例 #3
0
        protected virtual Type GetDrawerTypeForAsset([NotNull] Type assetType, [NotNull] string localPath)
        {
            Type drawerType;

            if (localPath.Length > 0)
            {
                                #if UNITY_EDITOR
                string fullPath = FileUtility.LocalToFullPath(localPath);
                if (Directory.Exists(fullPath))
                {
                    return(typeof(FolderDrawer));
                }
                                #endif

                string fileExtension = Path.GetExtension(localPath).ToLowerInvariant();
                if (fileExtension.Length > 0)
                {
                    if (drawersFor.assetsByExtension.TryGetValue(fileExtension, out drawerType))
                    {
                                                #if DEV_MODE && DEBUG_GET_FOR_ASSET
                        Debug.Log("GetDrawerTypeForAsset(" + StringUtils.ToString(assetType) + "): " + drawerType.Name + " via extension \"" + fileExtension + "\" with localPath=\"" + localPath + "\"");
                                                #endif

                        return(drawerType);
                    }
                }
            }

            if (drawersFor.assets.TryGetValue(assetType, out drawerType))
            {
                                #if DEV_MODE && DEBUG_GET_FOR_ASSET
                Debug.Log("GetDrawerTypeForAsset(" + StringUtils.ToString(assetType) + "): " + drawerType.Name + " via asset type with localPath=\"" + localPath + "\"");
                                #endif

                return(drawerType);
            }

                        #if UNITY_EDITOR
            // ScriptableObjects use AssetDrawers unless using Compatibility mode, in which case always use CustomEditorAssetDrawer
            if (assetType.IsScriptableObject() && !PluginCompatibilityUtility.UseCompatibilityModeForDisplayingTarget(assetType))
            {
                                #if DEV_MODE && DEBUG_GET_FOR_ASSET
                Debug.Log("GetDrawerTypeForAsset(" + StringUtils.ToString(assetType) + "): AssetDrawer as fallback with localPath=\"" + localPath + "\"");
                                #endif

                return(typeof(AssetDrawer));
            }

                        #if DEV_MODE && DEBUG_GET_FOR_ASSET
            Debug.Log("GetDrawerTypeForAsset(" + StringUtils.ToString(assetType) + "): CustomEditorAssetDrawer as fallback with localPath=\"" + localPath + "\"");
                        #endif

            return(typeof(CustomEditorAssetDrawer));
                        #else
            return(typeof(AssetDrawer));
                        #endif
        }
コード例 #4
0
        public void Install()
        {
            if (CompatibilityPackageIsInstalled)
            {
                Debug.LogWarning("Can't install " + GetDirectoryPath() + " because it is already installed.");
                return;
            }

                        #if DEV_MODE
            Debug.Log("PluginCompatibilityPackageInstaller.Install");
                        #endif

            var disabledFullPath  = FileUtility.LocalToFullPath(PackageDisablePath);
            var installedFullPath = FileUtility.LocalToFullPath(PackageInstallPath);
            Directory.Move(disabledFullPath, installedFullPath);
        }
コード例 #5
0
        private static void ApplyContextMenuPreferences()
        {
            // Currently cannot call Setup for InspectorPreferences because ApplyContextMenuPreferences is called outside of OnGUI.
            InspectorPreferences preferences;

            try
            {
                preferences = InspectorUtility.Preferences;
            }
                        #if DEV_MODE
            catch (NullReferenceException e)
            {
                Debug.LogError("PowerInspectorMenuItems failed to find Power Inspector Preferences asset.\n" + e);
                        #else
            catch (Exception)
            {
                        #endif
                return;
            }

            if (preferences == null)
            {
                                #if DEV_MODE
                Debug.LogWarning("PowerInspectorMenuItems failed to find Power Inspector Preferences asset.");
                                #endif
                return;
            }

            var scriptFile = FileUtility.FindScriptFile(typeof(PowerInspectorMenuItems));
            if (scriptFile == null)
            {
                Debug.LogError("PowerInspectorMenuItems failed to find script asset for itself.");
                return;
            }

            var scriptText = scriptFile.text;

            int preferencesStart = scriptText.IndexOf("#region ContextMenuPreferences", StringComparison.Ordinal) + 30;
            if (preferencesStart == -1)
            {
                throw new InvalidDataException("#region ContextMenuPreferences missing from PowerInspectorMenuItems.cs");
            }
            int preferencesEnd = scriptText.IndexOf("#endregion", preferencesStart, StringComparison.Ordinal);
            if (preferencesEnd == -1)
            {
                throw new InvalidDataException("#endregion missing from PowerInspectorMenuItems.cs");
            }
            string beforePreferences = scriptText.Substring(0, preferencesStart);
            string afterPreferences  = scriptText.Substring(preferencesEnd);
            string menuPreferences   = scriptText.Substring(preferencesStart, preferencesEnd - preferencesStart);

            bool scriptChanged = false;

            var enabledMenuItems = preferences.defaultInspector.enhanceUnityObjectContextMenu;
            SetContextMenuItemEnabled(ref menuPreferences, enabledMenuItems.HasFlag(ObjectContextMenuItems.ViewInPowerInspector), "#define PI_ENABLE_CONTEXT_INSPECT", ref scriptChanged);
            SetContextMenuItemEnabled(ref menuPreferences, enabledMenuItems.HasFlag(ObjectContextMenuItems.PeekInPowerInspector), "#define PI_ENABLE_CONTEXT_PEEK", ref scriptChanged);

            SetContextMenuItemEnabled(ref menuPreferences, !preferences.disabledMenuItems.HasFlag(MenuItems.Peek), "#define PI_ENABLE_MENU_PEEK", ref scriptChanged);
            SetContextMenuItemEnabled(ref menuPreferences, !preferences.disabledMenuItems.HasFlag(MenuItems.Reset), "#define PI_ENABLE_MENU_RESET", ref scriptChanged);

            if (scriptChanged)
            {
                string localPath = AssetDatabase.GetAssetPath(scriptFile);
                string fullPath  = FileUtility.LocalToFullPath(localPath);
                File.WriteAllText(fullPath, beforePreferences + menuPreferences + afterPreferences);
                EditorUtility.SetDirty(scriptFile);
                AssetDatabase.Refresh();
            }
        }
コード例 #6
0
        private static AssetDeleteResult OnWillDeleteAsset(string assetPath, RemoveAssetOptions options)
        {
                        #if DEV_MODE && DEBUG_ENABLED
            UnityEngine.Debug.Log("OnWillDeleteAsset: " + assetPath + " " + options);
                        #endif

            if (!assetPath.EndsWith(".cs", StringComparison.OrdinalIgnoreCase))
            {
                if (!assetPath.EndsWith(".dll", StringComparison.OrdinalIgnoreCase))
                {
                    return(AssetDeleteResult.DidNotDelete);
                }

                var fullAssetPath = FileUtility.LocalToFullPath(assetPath);
                var assembly      = System.Reflection.Assembly.LoadFile(fullAssetPath);
                if (assembly == null)
                {
                    return(AssetDeleteResult.DidNotDelete);
                }

                for (int n = packageInstallers.Length - 1; n >= 0; n--)
                {
                    var targetType = packageInstallers[n].Type;
                    if (targetType == null)
                    {
                        continue;
                    }

                    var packageInstaller = packageInstallers[n];

                    if (targetType.Assembly == assembly && packageInstaller.autoInstallEnabled && packageInstaller.CompatibilityPackageIsInstalled)
                    {
                        packageInstaller.Uninstall();
                        return(AssetDeleteResult.DidNotDelete);
                    }
                }

                return(AssetDeleteResult.DidNotDelete);
            }

            var script = AssetDatabase.LoadAssetAtPath <MonoScript>(assetPath);
            if (script == null)
            {
                return(AssetDeleteResult.DidNotDelete);
            }

            var type = script.GetClass();
            if (type == null)
            {
                return(AssetDeleteResult.DidNotDelete);
            }

            for (int n = packageInstallers.Length - 1; n >= 0; n--)
            {
                if (packageInstallers[n].Type == type && packageInstallers[n].CompatibilityPackageIsInstalled)
                {
                    packageInstallers[n].Uninstall();
                    return(AssetDeleteResult.DidNotDelete);
                }
            }

            return(AssetDeleteResult.DidNotDelete);
        }
コード例 #7
0
        public Dictionary <string, string> GetMemberTooltips([NotNull] Type classType)
        {
                        #if DEV_MODE && PI_ASSERTATIONS
            Debug.Assert(classType != null);
            Debug.Assert(classType != typeof(MonoScript));
                        #endif

            Dictionary <string, string> tooltips;
            if (memberTooltipsByClass.TryGetValue(classType, out tooltips))
            {
                return(tooltips);
            }

            var assembly = classType.Assembly;

            if (InspectorUtility.Preferences.enableTooltipsFromXmlComments)
            {
                // try fetching tooltips from xml documentation file
                if (XMLDocumentationCommentParser.TryGetMemberTooltips(classType, out tooltips))
                {
                    memberTooltipsByClass.Add(classType, tooltips);
                    return(tooltips);
                }
            }

            var rootAssembly = assembly.GetName().Name;
            int i            = rootAssembly.IndexOf('.');
            if (i != -1)
            {
                rootAssembly = rootAssembly.Substring(0, i);
            }

            switch (rootAssembly)
            {
            case "System":
            case "UnityEditor":
            case "UnityEngine":
                break;

            default:
                string className = classType.Name;

                //try fetching tooltips from MonoScript file
                var guids = AssetDatabase.FindAssets(className + " t:MonoScript");
                for (int n = guids.Length - 1; n >= 0; n--)
                {
                    string scriptAssetLocalPath = FileUtility.GUIDToAssetPath(guids[n]);

                    if (!string.Equals(System.IO.Path.GetFileNameWithoutExtension(scriptAssetLocalPath), className, StringComparison.OrdinalIgnoreCase))
                    {
                                                        #if DEV_MODE && DEBUG_GET_TOOLTIP_STEPS
                        Debug.Log("GetMemberTooltips(" + classType.Name + ") won't use script asset because name not an exact match: " + scriptAssetLocalPath);
                                                        #endif
                        continue;
                    }

                                                #if DEV_MODE && DEBUG_GET_TOOLTIP_STEPS
                    Debug.Log("GetMemberTooltips(" + classType.Name + ") found script asset @" + scriptAssetLocalPath);
                                                #endif

                    string scriptAssetPath = scriptAssetLocalPath; FileUtility.LocalToFullPath(scriptAssetLocalPath);
                    if (TryFetchMemberTooltips(scriptAssetPath, classType, out tooltips))
                    {
                        memberTooltipsByClass.Add(classType, tooltips);
                        return(tooltips);
                    }

                                                #if DEV_MODE && DEBUG_GET_TOOLTIP_STEPS
                    if (guids.Length > n - 1)
                    {
                        Debug.Log("TryFetchMemberTooltips(\"" + scriptAssetPath + "\", " + (classType == null ? "null" : classType.FullName) + ") failed. Testing next...");
                    }
                    else
                    {
                        Debug.LogWarning("TryFetchMemberTooltips(\"" + scriptAssetPath + "\", " + (classType == null ? "null" : classType.FullName) + ") failed - and it was the last one.");
                    }
                                                #endif
                }

                                        #if DEV_MODE && DEBUG_GET_TOOLTIP_STEPS
                Debug.Log("GetMemberTooltips(" + classType.Name + ") did not find script asset \"" + className + ".cs\"");
                                        #endif

                break;
            }

            memberTooltipsByClass.Add(classType, null);
            return(null);
        }