Skip to content

Instantly share code, notes, and snippets.

@CorbyO
Created December 8, 2022 04:37
Show Gist options
  • Save CorbyO/ffa724d7830e090c4c12c6505e9a51f8 to your computer and use it in GitHub Desktop.
Save CorbyO/ffa724d7830e090c4c12c6505e9a51f8 to your computer and use it in GitHub Desktop.
Unity Hidden asset manager
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
public class HiddenManager : EditorWindow
{
private static SortedSet<string> _hiddenPaths;
private static ListView _listView;
[MenuItem("Tools/Hidden Manager")]
public static void ShowWindows()
{
// This method is called when the user selects the menu item in the Editor
EditorWindow wnd = GetWindow<HiddenManager>();
wnd.titleContent = new GUIContent("Hidden Manager");
}
public void CreateGUI()
{
// Create a root element
VisualElement root = rootVisualElement;
// load hidden path list from file
var hiddenFile = Application.dataPath + "/../ProjectSettings/HiddenManager.txt";
if (!File.Exists(hiddenFile)) File.Create(hiddenFile).Close();
_hiddenPaths = new SortedSet<string>(File.ReadAllLines(hiddenFile).ToList());
// show hidden path list
_listView = new ListView(_hiddenPaths.ToList(), 20,
() => new Label(),
(item, index) => (item as Label).text = _hiddenPaths.ElementAt(index));
_listView.onSelectionChange += (e) => { };
root.Add(_listView);
// remove button
var removeButton = new Button(RemoveHiddenList);
removeButton.text = "Remove";
root.Add(removeButton);
// hide button
Button hideButton = new Button(MakeHide);
hideButton.text = "Hide";
root.Add(hideButton);
// show button
Button showButton = new Button(MakeShow);
showButton.text = "Show";
root.Add(showButton);
}
/// <summary>
/// Add hidden path for selected object in project window<br/>
/// Not hidden when now
/// </summary>
[MenuItem("Assets/Add Hidden Path")]
private static void AddHiddenList()
{
var hiddenFile = Application.dataPath + "/../ProjectSettings/HiddenManager.txt";
var paths = new List<string>();
foreach(var obj in Selection.objects)
{
var path = AssetDatabase.GetAssetPath(obj);
if(!_hiddenPaths.Contains(path))
{
_hiddenPaths.Add(path);
_listView.hierarchy.Add(new Label(path));
}
}
// write
if (!File.Exists(hiddenFile)) File.Create(hiddenFile).Close();
File.WriteAllLines(hiddenFile, _hiddenPaths);
}
/// <summary>
/// Remove hidden path for selected object in HiddenManager window
/// </summary>
private void RemoveHiddenList()
{
var hiddenFile = Application.dataPath + "/../ProjectSettings/HiddenManager.txt";
if (_listView.selectedIndex == -1) return;
_hiddenPaths.Remove(_listView.selectedItem as string);
_listView.itemsSource = _hiddenPaths.ToList();
_listView.RefreshItems();
// write
if (File.Exists(hiddenFile))
{
File.Delete(hiddenFile);
}
File.Create(hiddenFile).Close();
File.WriteAllLines(hiddenFile, _hiddenPaths);
}
/// <summary>
/// Elements hide in Hidden List
/// </summary>
private void MakeHide()
{
// all Hide
foreach (var path in _hiddenPaths)
{
// dir
var oldPath = new DirectoryInfo(Application.dataPath + "/../" + path);
var newPath = new DirectoryInfo(Application.dataPath + "/../" + path + "~");
if (oldPath.Exists)
{
oldPath.MoveTo(newPath.FullName);
}
// meta file
var oldMetaPath = new FileInfo(Application.dataPath + "/../" + path + ".meta");
var newMetaPath = new FileInfo(Application.dataPath + "/../" + path + "~.meta");
if(oldMetaPath.Exists)
{
oldMetaPath.MoveTo(newMetaPath.FullName);
}
}
AssetDatabase.Refresh();
}
/// <summary>
/// Elements show in Hidden List
/// </summary>
private void MakeShow()
{
// all Show
foreach (var path in _hiddenPaths)
{
// dir
var oldPath = new DirectoryInfo(Application.dataPath + "/../" + path + "~");
var newPath = new DirectoryInfo(Application.dataPath + "/../" + path);
if (oldPath.Exists)
{
oldPath.MoveTo(newPath.FullName);
}
// meta file
var oldMetaPath = new FileInfo(Application.dataPath + "/../" + path + "~.meta");
var newMetaPath = new FileInfo(Application.dataPath + "/../" + path + ".meta");
if (oldMetaPath.Exists)
{
oldMetaPath.MoveTo(newMetaPath.FullName);
}
}
AssetDatabase.Refresh();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment