CreateScriptableObjectAsset

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Usage)
m (Added reference to alternative implementation.)
 
(6 intermediate revisions by 3 users not shown)
Line 3: Line 3:
  
 
== Description ==
 
== Description ==
This is a method to easily create a new instance of a ScriptableObject-derived class as a unique asset in the currently selected project path. It mimics the way Unity's built-in assets are created.
+
This is a method to easily create a new asset file instance of a ScriptableObject-derived class. The asset is uniquely named and placed in the currently selected project path; this mimics the way Unity's built-in assets are created.
  
ScriptableObject asset files are useful for storing data that doesn't fit naturally within the MonoBehaviour/GameObject Prefab system. Since they use Unity's built-in serialization, ScriptableObject assets are much easier to work with in Unity than XML, CSV, or other traditional ways of storing such data.
+
ScriptableObject asset files are useful for storing data that doesn't fit naturally within the MonoBehaviour/GameObject Prefab system. Since they use Unity's built-in serialization, they are guaranteed to map perfectly to an existing class; therefore, ScriptableObject assets are much easier to work with in Unity than XML, CSV, or other traditional ways of storing such data.
  
 
== Usage ==
 
== Usage ==
Line 11: Line 11:
  
 
=== ScriptableObjectUtility.cs ===
 
=== ScriptableObjectUtility.cs ===
<csharp>
+
<syntaxhighlight lang="csharp">
 
using UnityEngine;
 
using UnityEngine;
 
using UnityEditor;
 
using UnityEditor;
Line 40: Line 40:
 
 
 
AssetDatabase.SaveAssets ();
 
AssetDatabase.SaveAssets ();
 +
        AssetDatabase.Refresh();
 
EditorUtility.FocusProjectWindow ();
 
EditorUtility.FocusProjectWindow ();
 
Selection.activeObject = asset;
 
Selection.activeObject = asset;
 
}
 
}
 
}
 
}
</csharp>
+
</syntaxhighlight>
  
 
=== YourClassAsset.cs ===
 
=== YourClassAsset.cs ===
<csharp>
+
<syntaxhighlight lang="csharp">
 
using UnityEngine;
 
using UnityEngine;
 
using UnityEditor;
 
using UnityEditor;
Line 59: Line 60:
 
}
 
}
 
}
 
}
</csharp>
+
</syntaxhighlight>
 +
 
 +
== See also==
 +
*[[CreateScriptableObjectAsset2]]

Latest revision as of 12:55, 13 October 2014

Author: Brandon Edmark

Contents

[edit] Description

This is a method to easily create a new asset file instance of a ScriptableObject-derived class. The asset is uniquely named and placed in the currently selected project path; this mimics the way Unity's built-in assets are created.

ScriptableObject asset files are useful for storing data that doesn't fit naturally within the MonoBehaviour/GameObject Prefab system. Since they use Unity's built-in serialization, they are guaranteed to map perfectly to an existing class; therefore, ScriptableObject assets are much easier to work with in Unity than XML, CSV, or other traditional ways of storing such data.

[edit] Usage

Copy ScriptableObjectUtility.cs into your project. Then create a class like YourClassAsset.cs, replacing YourClass with the name of your ScriptableObject-inheriting class, and place it in an Editor folder. You will now be able to create a uniquely named YourClass asset file from the Asset menu.

[edit] ScriptableObjectUtility.cs

using UnityEngine;
using UnityEditor;
using System.IO;
 
public static class ScriptableObjectUtility
{
	/// <summary>
	//	This makes it easy to create, name and place unique new ScriptableObject asset files.
	/// </summary>
	public static void CreateAsset<T> () where T : ScriptableObject
	{
		T asset = ScriptableObject.CreateInstance<T> ();
 
		string path = AssetDatabase.GetAssetPath (Selection.activeObject);
		if (path == "") 
		{
			path = "Assets";
		} 
		else if (Path.GetExtension (path) != "") 
		{
			path = path.Replace (Path.GetFileName (AssetDatabase.GetAssetPath (Selection.activeObject)), "");
		}
 
		string assetPathAndName = AssetDatabase.GenerateUniqueAssetPath (path + "/New " + typeof(T).ToString() + ".asset");
 
		AssetDatabase.CreateAsset (asset, assetPathAndName);
 
		AssetDatabase.SaveAssets ();
        	AssetDatabase.Refresh();
		EditorUtility.FocusProjectWindow ();
		Selection.activeObject = asset;
	}
}

[edit] YourClassAsset.cs

using UnityEngine;
using UnityEditor;
 
public class YourClassAsset
{
	[MenuItem("Assets/Create/YourClass")]
	public static void CreateAsset ()
	{
		ScriptableObjectUtility.CreateAsset<YourClass> ();
	}
}

[edit] See also

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox