Skip to content

Instantly share code, notes, and snippets.

@anchan828
Created November 30, 2013 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anchan828/4a4b7d746cde6164aa02 to your computer and use it in GitHub Desktop.
Save anchan828/4a4b7d746cde6164aa02 to your computer and use it in GitHub Desktop.
#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class TrashAsset : MonoBehaviour
{
public string originalAssetPath;
public string trashAssetPath;
private int count;
void OnValidate ()
{
if (count++ == 0) {
// Redo
UndoExtention.MoveAssetToTrash (this);
}
}
}
#endif
using System;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;
public class Example
{
[MenuItem ("Assets/Move to Trash")]
static void Delete ()
{
Object[] os = Selection.objects;
if (os.Length == 0)
return;
UndoExtention.MoveToTrash (os);
}
}
[InitializeOnLoad]
public class UndoExtention
{
public static TrashAsset[] trashAssets;
static UndoExtention ()
{
trashAssets = Resources.FindObjectsOfTypeAll<TrashAsset> ();
Undo.undoRedoPerformed += () => {
foreach (var trashAsset in trashAssets) {
if (!trashAsset) {
//Undo
File.Move (trashAsset.trashAssetPath, trashAsset.originalAssetPath);
File.Move (AssetDatabase.GetTextMetaDataPathFromAssetPath (trashAsset.trashAssetPath), AssetDatabase.GetTextMetaDataPathFromAssetPath (trashAsset.originalAssetPath));
AssetDatabase.ImportAsset (trashAsset.originalAssetPath);
ArrayUtility.Remove (ref trashAssets, trashAsset);
}
}
};
}
public static void MoveToTrash (Object obj, string name = "Move to Trash")
{
MoveToTrash (new Object[]{ obj }, name);
}
public static void MoveToTrash (Object[] objs, string name = "Move to Trash")
{
GameObject g = EditorUtility.CreateGameObjectWithHideFlags ("Trash", HideFlags.HideAndDontSave);
GameObject.DontDestroyOnLoad (g);
foreach (var o in objs) {
Internal_MoveAssetToTrash (o, g, null);
}
Undo.RegisterCreatedObjectUndo (g, name);
}
public static void MoveAssetToTrash (TrashAsset trashAsset)
{
Internal_MoveAssetToTrash (null, null, trashAsset);
}
public static void Internal_MoveAssetToTrash (Object o, GameObject g, TrashAsset trashAsset)
{
string assetPath = "";
bool isObj = o;
if (o) {
assetPath = AssetDatabase.GetAssetPath (o);
} else if (trashAsset != null) {
assetPath = trashAsset.originalAssetPath;
}
string newName = string.Format ("{0}{1}", Path.GetFileNameWithoutExtension (assetPath), DateTime.Now.ToString ("HH-m-s"));
string error = AssetDatabase.RenameAsset (assetPath, newName);
if (string.IsNullOrEmpty (error)) {
string newAssetPath = Path.Combine (Path.GetDirectoryName (assetPath), newName + Path.GetExtension (assetPath));
AssetDatabase.MoveAssetToTrash (newAssetPath);
if (isObj) {
trashAsset = g.AddComponent<TrashAsset> ();
trashAsset.originalAssetPath = assetPath;
}
#if UNITY_EDITOR_OSX
trashAsset.trashAssetPath = string.Format ("/Users/{0}/.Trash/{1}{2}", Environment.UserName, newName, Path.GetExtension (assetPath));
#elif !UNITY_EDITOR_OSX
throw new NotImplementedException ();
trashAsset.trashAssetPath = string.Format("{0}\\{1}{2}",Directory.GetDirectories("C:\\$Recycle.Bin")[0],newName, Path.GetExtension (assetPath));
#endif
if (!ArrayUtility.Contains<TrashAsset> (trashAssets, trashAsset))
ArrayUtility.Add (ref trashAssets, trashAsset);
} else {
Debug.LogError (error);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment