CreateScriptableObjectAsset

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Easily create an asset file of a ScriptableObject-derived class instance in Unity's intuitive style.)
Line 1: Line 1:
 
== Description ==
 
== Description ==
This is a method to create a new instance of a ScriptableObject-derived class as a unique asset in the current path. It mimics the way Unity's built-in assets are created.
+
This is a method to easily create a new instance of a ScriptableObject-derived class as a unique asset in the current path. It mimics the way Unity's built-in assets are created.
  
 
== Usage ==
 
== Usage ==
 
Copy ScriptableObjectUtility.cs into your project. Then create a class like _____, replacing YourClass with the name of your ScriptableObject-inheriting class, and place it in an Editor folder.
 
Copy ScriptableObjectUtility.cs into your project. Then create a class like _____, replacing YourClass with the name of your ScriptableObject-inheriting class, and place it in an Editor folder.
  
=== CSharp - ScriptableObjectUtility.cs ===
+
=== ScriptableObjectUtility.cs ===
 
<csharp>
 
<csharp>
 
using UnityEngine;
 
using UnityEngine;
Line 41: Line 41:
 
</csharp>
 
</csharp>
  
=== CSharp - YourClassAsset.cs ===
+
=== YourClassAsset.cs ===
 
<csharp>
 
<csharp>
 
using UnityEngine;
 
using UnityEngine;

Revision as of 18:46, 29 July 2011

Contents

Description

This is a method to easily create a new instance of a ScriptableObject-derived class as a unique asset in the current path. It mimics the way Unity's built-in assets are created.

Usage

Copy ScriptableObjectUtility.cs into your project. Then create a class like _____, replacing YourClass with the name of your ScriptableObject-inheriting class, and place it in an Editor folder.

ScriptableObjectUtility.cs

<csharp> 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 (); EditorUtility.FocusProjectWindow (); Selection.activeObject = asset; } } </csharp>

YourClassAsset.cs

<csharp> using UnityEngine; using UnityEditor;

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

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox