CreateScriptableObjectAsset2

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Assets/Editor/YourUnityIntegration.cs)
(Added screenshot and improved description.)
Line 3: Line 3:
  
 
== Description ==
 
== Description ==
This page demonstrates an [[CreateScriptableObjectAsset|alternative method]] of creating an asset file for a <code>ScriptableObject</code> type class. As with the original topic the asset is uniquely named and placed in the currently selected project path but better mimics the way in which Unity's built-in assets are created since the asset is instantly in "rename" mode.
+
This page demonstrates an [[CreateScriptableObjectAsset|alternative method]] of adding a menu item which can be used to create an asset file for a <code>ScriptableObject</code> type class. As with the original topic the asset is uniquely named and placed in the currently selected project path but better mimics the way in which Unity's built-in assets are created since the asset is instantly in "rename" mode.
  
 
This topic is an extension of [[CreateScriptableObjectAsset]] by Brandon Edmark.
 
This topic is an extension of [[CreateScriptableObjectAsset]] by Brandon Edmark.
Line 9: Line 9:
 
== Warning ==
 
== Warning ==
 
This implementation makes use of the non-documented method <code>ProjectWindowUtil.CreateAsset</code> and was created by way of trial-and-error but seems to work. Use this at your own risk.
 
This implementation makes use of the non-documented method <code>ProjectWindowUtil.CreateAsset</code> and was created by way of trial-and-error but seems to work. Use this at your own risk.
 +
 +
== Screenshot ==
 +
[[Image:screenshot-of-create-yourscriptableobject-menu-item.png]]
  
 
== Usage ==
 
== Usage ==

Revision as of 13:04, 13 October 2014

Author: Lea Hayes

Contents

Description

This page demonstrates an alternative method of adding a menu item which can be used to create an asset file for a ScriptableObject type class. As with the original topic the asset is uniquely named and placed in the currently selected project path but better mimics the way in which Unity's built-in assets are created since the asset is instantly in "rename" mode.

This topic is an extension of CreateScriptableObjectAsset by Brandon Edmark.

Warning

This implementation makes use of the non-documented method ProjectWindowUtil.CreateAsset and was created by way of trial-and-error but seems to work. Use this at your own risk.

Screenshot

Screenshot-of-create-yourscriptableobject-menu-item.png

Usage

1. Copy the following editor script into the path "Assets\Editor\ScriptableObjectUtility2.cs".

2. You can then use this utility script when adding new items to the "Assets/Create" menu.

Assets/Editor/ScriptableObjectUtility2.cs

using UnityEngine;
using UnityEditor;
 
public static class ScriptableObjectUtility2 {
 
	/// <summary>
	/// Create new asset from <see cref="ScriptableObject"/> type with unique name at
	/// selected folder in project window. Asset creation can be cancelled by pressing
	/// escape key when asset is initially being named.
	/// </summary>
	/// <typeparam name="T">Type of scriptable object.</typeparam>
	public static void CreateAsset<T>() where T : ScriptableObject {
		var asset = ScriptableObject.CreateInstance<T>();
		ProjectWindowUtil.CreateAsset(asset, "New " + typeof(T).ToString() + ".asset");
	}
 
}

Assets/Editor/YourUnityIntegration.cs

using UnityEngine;
using UnityEditor;
 
static class YourUnityIntegration {
 
	[MenuItem("Assets/Create/YourScriptableObject")]
	public static void CreateYourScriptableObject() {
		ScriptableObjectUtility2.CreateAsset<YourScriptableObject>();
	}
 
}

Assets/YourScriptableObject.cs

using UnityEngine;
 
public class YourScriptableObject : ScriptableObject {
 
}

See also

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox