using UnityEngine;
using UnityEditor;
using System.IO;
using System;
namespace Cinemachine.Editor
{
///
/// This is a collection of utilities surrounding ScriptableObjects
///
public class ScriptableObjectUtility : ScriptableObject
{
///
/// The default relative path to the root directory where Cinemachine is installed
///
public static string kPackageRoot = "Packages/com.unity.cinemachine";
/// Get the Cinemachine package install path.
public static string CinemachineInstallPath
{
get
{
string path = Path.GetFullPath(kPackageRoot);
path = path.Replace('\\', '/'); // because of GetFullPath()
return path;
}
}
/// Get the relative Cinemachine package install path.
public static string CinemachineRealativeInstallPath
{
get
{
return kPackageRoot;
}
}
/// Create a scriptable object asset
/// The type of asset to create
/// The full path and filename of the asset to create
/// The newly-created asset
public static T CreateAt(string assetPath) where T : ScriptableObject
{
return CreateAt(typeof(T), assetPath) as T;
}
/// Create a scriptable object asset
/// The type of asset to create
/// The full path and filename of the asset to create
/// The newly-created asset
public static ScriptableObject CreateAt(Type assetType, string assetPath)
{
ScriptableObject asset = ScriptableObject.CreateInstance(assetType);
if (asset == null)
{
Debug.LogError("failed to create instance of " + assetType.Name + " at " + assetPath);
return null;
}
AssetDatabase.CreateAsset(asset, assetPath);
return asset;
}
/// Create a ScriptableObject asset
/// The type of asset to create
/// If true, prepend the selected asset folder name to the asset name
/// If true, remove instances of the "Asset", "Attributes", "Container" strings from the name
public static void Create(bool prependFolderName = false, bool trimName = true) where T : ScriptableObject
{
string className = typeof(T).Name;
string assetName = className;
string folder = GetSelectedAssetFolder();
if (trimName)
{
string[] standardNames = new string[] { "Asset", "Attributes", "Container" };
foreach (string standardName in standardNames)
{
assetName = assetName.Replace(standardName, "");
}
}
if (prependFolderName)
{
string folderName = Path.GetFileName(folder);
assetName = (string.IsNullOrEmpty(assetName) ? folderName : string.Format("{0}_{1}", folderName, assetName));
}
Create(className, assetName, folder);
}
private static ScriptableObject Create(string className, string assetName, string folder)
{
ScriptableObject asset = ScriptableObject.CreateInstance(className);
if (asset == null)
{
Debug.LogError("failed to create instance of " + className);
return null;
}
asset.name = assetName ?? className;
string assetPath = GetUnusedAssetPath(folder, asset.name);
AssetDatabase.CreateAsset(asset, assetPath);
return asset;
}
private static string GetSelectedAssetFolder()
{
if ((Selection.activeObject != null) && AssetDatabase.Contains(Selection.activeObject))
{
string assetPath = AssetDatabase.GetAssetPath(Selection.activeObject);
string assetPathAbsolute = string.Format("{0}/{1}", Path.GetDirectoryName(Application.dataPath), assetPath);
if (Directory.Exists(assetPathAbsolute))
{
return assetPath;
}
else
{
return Path.GetDirectoryName(assetPath);
}
}
return "Assets";
}
private static string GetUnusedAssetPath(string folder, string assetName)
{
for (int n = 0; n < 9999; n++)
{
string assetPath = string.Format("{0}/{1}{2}.asset", folder, assetName, (n == 0 ? "" : n.ToString()));
string existingGUID = AssetDatabase.AssetPathToGUID(assetPath);
if (string.IsNullOrEmpty(existingGUID))
{
return assetPath;
}
}
return null;
}
}
}