// Copyright (c) Pixel Crushers. All rights reserved.
using UnityEngine;
using UnityEditor;
using System.IO;
namespace PixelCrushers
{
public static class AssetUtility
{
///
/// Creates a new ScriptableObject asset in the project's asset database.
///
/// The ScriptableObject type.
/// The type name to use when naming the asset file in the project's asset database.
/// If true, select the asset.
/// If true, prepend 'New' to the beginning of the asset name.
/// The asset.
public static T CreateAsset(string typeName, bool select = true, bool prependNew = true) where T : ScriptableObject
{
var asset = ScriptableObjectUtility.CreateScriptableObject() as T;
SaveAsset(asset, typeName, select, prependNew);
return asset;
}
///
/// Creates a new ScriptableObject asset in the project's asset database.
///
/// The ScriptableObject type.
/// The type name to use when naming the asset file in the project's asset database.
/// If true, select the asset.
/// If true, prepend 'New' to the beginning of the asset name.
/// The asset.
public static ScriptableObject CreateAsset(System.Type type, string typeName, bool select = true, bool prependNew = true)
{
var asset = ScriptableObjectUtility.CreateScriptableObject(type);
SaveAsset(asset, typeName, select, prependNew);
return asset;
}
///
/// Creates a new ScriptableObject asset in the project's asset database.
///
/// The ScriptableObject type.
/// The filename to save the asset as.
/// If true, select the asset.
/// The asset.
public static T CreateAssetWithFilename(string filename, bool select = true) where T : ScriptableObject
{
var asset = ScriptableObjectUtility.CreateScriptableObject() as T;
SaveAssetWithFilename(asset, filename, select);
return asset;
}
///
/// Creates a new ScriptableObject asset in the project's asset database.
///
/// The ScriptableObject type.
/// The filename to save the asset as.
/// If true, select the asset.
/// The asset.
public static ScriptableObject CreateAssetWithFilename(System.Type type, string filename, bool select = true)
{
var asset = ScriptableObjectUtility.CreateScriptableObject(type);
SaveAssetWithFilename(asset, filename, select);
return asset;
}
private static void SaveAsset(ScriptableObject asset, string typeName, bool select = true, bool prependNew = true)
{
string path = AssetDatabase.GetAssetPath(Selection.activeObject);
if (string.IsNullOrEmpty(path))
{
path = "Assets";
}
else if (!string.IsNullOrEmpty(Path.GetExtension(path)))
{
path = path.Replace(Path.GetFileName(AssetDatabase.GetAssetPath(Selection.activeObject)), "");
}
string assetPathAndName = AssetDatabase.GenerateUniqueAssetPath(path + (prependNew ? "/New " : "/") + typeName + ".asset");
SaveAssetWithFilename(asset, assetPathAndName, select);
}
private static void SaveAssetWithFilename(ScriptableObject asset, string filename, bool select = true)
{
AssetDatabase.CreateAsset(asset, filename);
//AssetDatabase.SaveAssets();
if (select)
{
EditorUtility.FocusProjectWindow();
Selection.activeObject = asset;
}
}
///
/// (Editor only) Registers a ScriptableObject as part of an asset in the project's asset database.
///
/// The ScriptableObject to add.
/// The asset to add the ScriptableObject to.
public static void AddToAsset(ScriptableObject scriptableObject, UnityEngine.Object asset)
{
scriptableObject.hideFlags = HideFlags.HideInHierarchy;
AssetDatabase.AddObjectToAsset(scriptableObject, asset);
//AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(scriptableObject));
//AssetDatabase.SaveAssets();
//AssetDatabase.Refresh();
}
///
/// (Editor only) Deletes a ScriptableObject from an asset.
///
/// The ScriptableObject to add.
/// The asset to delete the ScriptableObject from.
public static void DeleteFromAsset(ScriptableObject scriptableObject, UnityEngine.Object asset)
{
if (scriptableObject == null) return;
UnityEngine.Object.DestroyImmediate(scriptableObject, true);
//AssetDatabase.SaveAssets();
//AssetDatabase.Refresh();
}
}
}