using UnityEngine; using UnityEditor; using System; namespace Unity.Cinemachine.Editor { /// /// This is a collection of utilities surrounding ScriptableObjects /// class ScriptableObjectUtility { /// 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; } class CreateAssetAction : UnityEditor.ProjectWindowCallback.EndNameEditAction { public Type TypeToCreate; public override void Action(int instanceId, string pathName, string resourceFile) { var asset = CreateAt(TypeToCreate, pathName); if (asset != null) ProjectWindowUtil.ShowCreatedAsset(asset); } } /// /// Creates a new asset of the specified type in the Unity project. /// /// This method initializes the asset creation process in the Unity project window. The /// user is prompted to name the asset before it is finalized. The asset is created as a `.asset` /// file. /// The type of the asset to create. Must derive from . /// The default name for the new asset. If null or empty, a name is /// automatically generated based on the type of the asset. public static void Create(string defaultName = null) where T : ScriptableObject { if (string.IsNullOrEmpty(defaultName)) defaultName = "New " + ObjectNames.NicifyVariableName(typeof(T).Name); var action = ScriptableObject.CreateInstance(); action.TypeToCreate = typeof(T); var icon = EditorGUIUtility.IconContent("ScriptableObject Icon").image as Texture2D; ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, action, $"{defaultName}.asset", icon , null); } } }