Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

Commit

Permalink
Added GetOrCreateAsset extensions (#511)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenHodgson authored Apr 11, 2020
1 parent 8993ea9 commit f0ea305
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion Inspectors/Extensions/ScriptableObjectExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) XRTK. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;
Expand Down Expand Up @@ -45,6 +45,8 @@ public static ScriptableObject CreateAsset(this ScriptableObject scriptableObjec
{
var name = string.IsNullOrEmpty(fileName) ? $"{scriptableObject.GetType().Name}" : fileName;

name = name.Replace(" ", string.Empty);

if (string.IsNullOrWhiteSpace(path))
{
path = MixedRealityPreferences.ProfileGenerationPath;
Expand All @@ -61,6 +63,8 @@ public static ScriptableObject CreateAsset(this ScriptableObject scriptableObjec
Directory.CreateDirectory(Path.GetFullPath(path));
}

path = path.Replace(".asset", string.Empty);

string assetPathAndName = AssetDatabase.GenerateUniqueAssetPath($"{path}/{name}.asset");

AssetDatabase.CreateAsset(scriptableObject, assetPathAndName);
Expand Down Expand Up @@ -88,6 +92,41 @@ public static ScriptableObject CreateAsset(this ScriptableObject scriptableObjec
return scriptableObject;
}

/// <summary>
/// Attempts to find the asset associated to the instance of the <see cref="ScriptableObject"/>, if none is found a new asset is created.
/// </summary>
/// <param name="scriptableObject"><see cref="ScriptableObject"/> you want to create an asset file for.</param>
/// <param name="ping">The new asset should be selected and opened in the inspector.</param>
public static ScriptableObject GetOrCreateAsset(this ScriptableObject scriptableObject, bool ping = true)
{
return GetOrCreateAsset(scriptableObject, null, ping);
}

/// <summary>
/// Attempts to find the asset associated to the instance of the <see cref="ScriptableObject"/>, if none is found a new asset is created.
/// </summary>
/// <param name="scriptableObject"><see cref="ScriptableObject"/> you want to create an asset file for.</param>
/// <param name="path">Optional path for the new asset.</param>
/// <param name="ping">The new asset should be selected and opened in the inspector.</param>
public static ScriptableObject GetOrCreateAsset(this ScriptableObject scriptableObject, string path, bool ping = true)
{
return GetOrCreateAsset(scriptableObject, path, null, ping);
}

/// <summary>
/// Attempts to find the asset associated to the instance of the <see cref="ScriptableObject"/>, if none is found a new asset is created.
/// </summary>
/// <param name="scriptableObject"><see cref="ScriptableObject"/> you want get or create an asset file for.</param>
/// <param name="path">Optional path for the new asset.</param>
/// <param name="fileName">Optional filename for the new asset.</param>
/// <param name="ping">The new asset should be selected and opened in the inspector.</param>
public static ScriptableObject GetOrCreateAsset(this ScriptableObject scriptableObject, string path, string fileName, bool ping)
{
return !AssetDatabase.TryGetGUIDAndLocalFileIdentifier(scriptableObject, out var guid, out long _)
? scriptableObject.CreateAsset(path, fileName, ping)
: AssetDatabase.LoadAssetAtPath<ScriptableObject>(AssetDatabase.GUIDToAssetPath(guid));
}

/// <summary>
/// Gets all the scriptable object instances in the project.
/// </summary>
Expand Down

0 comments on commit f0ea305

Please sign in to comment.