static bool IsValidScript(MonoScript script) { if (script == null) { return(false); } // The user can only define the order of scripts that contains valid classes (see case 579536) if (script.GetClass() == null) { return(false); } // Only allow MonoBehaviours and ScriptableObjects bool isMonoBehaviour = typeof(MonoBehaviour).IsAssignableFrom(script.GetClass()); bool isScriptableObject = typeof(ScriptableObject).IsAssignableFrom(script.GetClass()); if (!isMonoBehaviour && !isScriptableObject) { return(false); } // The user can only define the order of scripts in the assets folder. bool isRootFolder, isReadOnly; if (!AssetDatabase.GetAssetFolderInfo(AssetDatabase.GetAssetPath(script), out isRootFolder, out isReadOnly)) { return(false); } return(true); }
static bool IsAssetInReadOnlyFolder(string assetPath) { bool rootFolder, readOnly; bool validPath = AssetDatabase.GetAssetFolderInfo(assetPath, out rootFolder, out readOnly); return(validPath && readOnly); }
internal static bool IsOpenForEdit(string assetPath, out string message, StatusQueryOptions statusOptions) { message = ""; if (String.IsNullOrEmpty(assetPath)) { return(true); // assetPath can be empty in some cases where Unity is checking for stuff in Library folder } bool rootFolder, readOnly; bool validPath = AssetDatabase.GetAssetFolderInfo(assetPath, out rootFolder, out readOnly); if (validPath && readOnly) { return(false); } bool finalResult = AssetModificationHook.IsOpenForEdit(assetPath, out message, statusOptions); foreach (var method in GetIsOpenForEditMethods()) { object[] args = { assetPath, message }; if (!((bool)method.Invoke(null, args))) { message = args[1] as string; return(false); } } return(finalResult); }
public override bool CanStartDrag(TreeViewItem targetItem, List <int> draggedItemIDs, Vector2 mouseDownPosition) { // Prevent dragging of immutable root folder foreach (var draggedItemID in draggedItemIDs) { var path = AssetDatabase.GetAssetPath(draggedItemID); if (AssetDatabase.IsValidFolder(path) && !AssetDatabase.GetAssetFolderInfo(path, out _, out _)) { return(false); } } return(true); }
public override bool CanStartDrag(TreeViewItem targetItem, List <int> draggedItemIDs, Vector2 mouseDownPosition) { // Prevent dragging of immutable root folder foreach (var draggedItemID in draggedItemIDs) { var path = AssetDatabase.GetAssetPath(draggedItemID); bool rootFolder, readOnly; bool validPath = AssetDatabase.GetAssetFolderInfo(path, out rootFolder, out readOnly); if (validPath && rootFolder && readOnly) { return(false); } } return(true); }
static Editability GetPathEditability(string assetPath) { // read-only asset locations (e.g. shared packages) are considered not editable bool rootFolder, readOnly; bool validPath = AssetDatabase.GetAssetFolderInfo(assetPath, out rootFolder, out readOnly); if (validPath && readOnly) { return(Editability.Never); } // other paths that are not know to asset database, and not versioned, are considered always editable if (!Provider.PathIsVersioned(assetPath)) { return(Editability.Always); } return(Editability.Maybe); }
// TODO: Having an non-obsolete method that takes an obsolete enum types as parameter is no good. #pragma warning disable 0618 // Type or member is obsolete private static GameObject SavePrefab(GameObject inputObject, string path, ReplacePrefabOptions replaceOptions, PrefabCreationFlags creationFlags, out bool success) { if (inputObject == null) { throw new ArgumentNullException("inputObject is null"); } if (String.IsNullOrEmpty(path)) { throw new ArgumentNullException("path is null or empty"); } if (!Paths.IsValidAssetPath(path, ".prefab")) { throw new ArgumentException("Given path is not valid: '" + path + "'"); } string directory = Path.GetDirectoryName(path); bool isRootFolder = false; bool isImmutableFolder = false; bool isValidAssetFolder = AssetDatabase.GetAssetFolderInfo(directory, out isRootFolder, out isImmutableFolder); if (isValidAssetFolder && isImmutableFolder) { throw new ArgumentException("Saving Prefab to immutable folder is not allowed: '" + path + "'"); } if (directory.Length > 0 && !Directory.Exists(directory)) { throw new ArgumentException("Given path does not exist: '" + path + "'"); } string prefabGUID = AssetDatabase.AssetPathToGUID(path); if (!VerifyNestingFromScript(new GameObject[] { inputObject }, prefabGUID, PrefabUtility.GetPrefabInstanceHandle(inputObject))) { throw new ArgumentException("Cyclic nesting detected"); } return(SavePrefab_Internal(inputObject, path, replaceOptions, creationFlags, out success)); }
void SetupBreadCrumbBarIfNeeded() { if (m_BreadcrumbInitialized) { return; } var history = StageNavigationManager.instance.stageHistory; var crumbs = new List <BreadcrumbBar.Item>(); Texture sceneIcon = EditorGUIUtility.FindTexture("UnityEditor/SceneAsset Icon"); foreach (var stage in history) { bool isLastCrumb = stage == history.Last(); var label = stage.displayName; var icon = sceneIcon; var style = isLastCrumb ? BreadcrumbBar.DefaultStyles.labelBold : BreadcrumbBar.DefaultStyles.label; var tooltip = ""; if (stage.isPrefabStage) { icon = isLastCrumb ? PrefabStageUtility.GetCurrentPrefabStage().prefabFileIcon : stage.prefabIcon; if (!stage.prefabAssetExists) { style = isLastCrumb ? BreadcrumbBar.DefaultStyles.labelBoldMissing : BreadcrumbBar.DefaultStyles.labelMissing; tooltip = L10n.Tr("Prefab asset has been deleted"); } } crumbs.Add(new BreadcrumbBar.Item { content = new GUIContent(label, icon, tooltip), guistyle = style, userdata = stage }); } m_BreadcrumbBar.SetBreadCrumbs(crumbs); m_BreadcrumbBar.onBreadCrumbClicked -= BreadCrumbItemClicked; m_BreadcrumbBar.onBreadCrumbClicked += BreadCrumbItemClicked; m_BreadcrumbInitialized = true; bool isRootFolder; m_IsPrefabInValidAssetFolder = AssetDatabase.GetAssetFolderInfo(StageNavigationManager.instance.currentItem.prefabAssetPath, out isRootFolder, out m_IsPrefabInImmutableFolder); }
private void DoPrefabButtons(PrefabType prefabType, GameObject go) { // @TODO: If/when we support multi-editing of prefab/model instances, // handle it here. Only show prefab bar if all are same type? if (!m_HasInstance) { return; } using (new EditorGUI.DisabledScope(EditorApplication.isPlayingOrWillChangePlaymode)) { EditorGUILayout.BeginHorizontal(); // Prefab information GUIContent prefixLabel = targets.Length > 1 ? s_Styles.goTypeLabelMultiple : s_Styles.goTypeLabel[(int)prefabType]; if (prefixLabel != null) { EditorGUILayout.BeginHorizontal(GUILayout.Width(kIconSize + s_Styles.tagFieldWidth)); GUILayout.FlexibleSpace(); if (prefabType == PrefabType.DisconnectedModelPrefabInstance || prefabType == PrefabType.MissingPrefabInstance || prefabType == PrefabType.DisconnectedPrefabInstance) { GUI.contentColor = GUI.skin.GetStyle("CN StatusWarn").normal.textColor; GUILayout.Label(prefixLabel, EditorStyles.whiteLabel, GUILayout.ExpandWidth(false)); GUI.contentColor = Color.white; } else { GUILayout.Label(prefixLabel, GUILayout.ExpandWidth(false)); } EditorGUILayout.EndHorizontal(); } if (targets.Length > 1) { GUILayout.Label("Instance Management Disabled", s_Styles.instanceManagementInfo); } else { // Select prefab if (prefabType != PrefabType.MissingPrefabInstance) { if (GUILayout.Button("Select", "MiniButtonLeft")) { Selection.activeObject = PrefabUtility.GetCorrespondingObjectFromSource(target); EditorGUIUtility.PingObject(Selection.activeObject); } } using (new EditorGUI.DisabledScope(AnimationMode.InAnimationMode())) { if (prefabType != PrefabType.MissingPrefabInstance) { // Revert this gameobject and components to prefab if (GUILayout.Button("Revert", "MiniButtonMid")) { PrefabUtility.RevertPrefabInstanceWithUndo(go); // case931300 - The selected gameobject might get destroyed by RevertPrefabInstance if (go != null) { CalculatePrefabStatus(); } // This is necessary because Revert can potentially destroy game objects and components // In that case the Editor classes would be destroyed but still be invoked. (case 837113) GUIUtility.ExitGUI(); } // Apply to prefab if (prefabType == PrefabType.PrefabInstance || prefabType == PrefabType.DisconnectedPrefabInstance) { GameObject rootUploadGameObject = PrefabUtility.FindValidUploadPrefabInstanceRoot(go); GUI.enabled = rootUploadGameObject != null && !AnimationMode.InAnimationMode(); if (GUILayout.Button("Apply", "MiniButtonRight")) { UnityObject correspondingAssetObject = PrefabUtility.GetCorrespondingObjectFromSource(rootUploadGameObject); string prefabAssetPath = AssetDatabase.GetAssetPath(correspondingAssetObject); bool isRootFolder, isReadonly; bool validPath = AssetDatabase.GetAssetFolderInfo(prefabAssetPath, out isRootFolder, out isReadonly); if (validPath && isReadonly) { string prefabName = FileUtil.GetLastPathNameComponent(FileUtil.GetPathWithoutExtension(prefabAssetPath)); EditorUtility.DisplayDialog("Cannot apply changes", string.Format("Original prefab \"{0}\" is immutable.", prefabName), "Close"); } else { bool editablePrefab = Provider.PromptAndCheckoutIfNeeded( new string[] { prefabAssetPath }, "The version control requires you to check out the prefab before applying changes."); if (editablePrefab) { PrefabUtility.ReplacePrefabWithUndo(rootUploadGameObject); CalculatePrefabStatus(); // This is necessary because ReplacePrefab can potentially destroy game objects and components // In that case the Editor classes would be destroyed but still be invoked. (case 468434) GUIUtility.ExitGUI(); } } } } } } // Edit model prefab if (prefabType == PrefabType.DisconnectedModelPrefabInstance || prefabType == PrefabType.ModelPrefabInstance) { if (GUILayout.Button("Open", "MiniButtonRight")) { AssetDatabase.OpenAsset(PrefabUtility.GetCorrespondingObjectFromSource(target)); GUIUtility.ExitGUI(); } } } EditorGUILayout.EndHorizontal(); } }