44 lines
1.7 KiB
C#
44 lines
1.7 KiB
C#
using System;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
using Object = UnityEngine.Object;
|
|
|
|
namespace SubAssetsToolbox.Editor
|
|
{
|
|
/// <summary>
|
|
/// When the user creates a new asset via the right-click menu and the selected asset is a main asset,
|
|
/// this class adds it to the target asset as a sub-asset.
|
|
/// </summary>
|
|
public class AssetRelocator : AssetModificationProcessor
|
|
{
|
|
public static Object LastSelectionOnRightClick;
|
|
|
|
static void OnWillCreateAsset(string path)
|
|
{
|
|
// Used to check if the creation happened as a right-click on an asset
|
|
if (LastSelectionOnRightClick == null) return;
|
|
|
|
string newParentPath = AssetDatabase.GetAssetPath(LastSelectionOnRightClick);
|
|
|
|
if (TypeChecker.HasDefaultAction(path, newParentPath)) return; // There's already a default action
|
|
if (!TypeChecker.ValidateSubAssetType(path, false)) return; // Can't be a sub-asset
|
|
if (!TypeChecker.ValidateDestinationType(newParentPath)) return; // Can't contain a sub-asset
|
|
|
|
EditorApplication.delayCall += () => RelocateAsset(path);
|
|
}
|
|
|
|
private static void RelocateAsset(string path)
|
|
{
|
|
Type t = AssetDatabase.GetMainAssetTypeAtPath(path);
|
|
Object justCreatedAsset = AssetDatabase.LoadAssetAtPath(path, t);
|
|
string parentObjectPath = AssetDatabase.GetAssetPath(LastSelectionOnRightClick);
|
|
|
|
SubAssetsToolbox.AddSubAsset(parentObjectPath, justCreatedAsset, out _);
|
|
|
|
AssetDatabase.DeleteAsset(path);
|
|
|
|
AssetDatabase.SaveAssets();
|
|
AssetDatabase.Refresh();
|
|
}
|
|
}
|
|
} |