CreateScriptableObjectAsset2

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Assets/Editor/ScriptableObjectUtility2.cs: Avoid including entire namespace of type.)
 
Line 33: Line 33:
 
public static void CreateAsset<T>() where T : ScriptableObject {
 
public static void CreateAsset<T>() where T : ScriptableObject {
 
var asset = ScriptableObject.CreateInstance<T>();
 
var asset = ScriptableObject.CreateInstance<T>();
ProjectWindowUtil.CreateAsset(asset, "New " + typeof(T).ToString() + ".asset");
+
ProjectWindowUtil.CreateAsset(asset, "New " + typeof(T).Name + ".asset");
 
}
 
}
 
 

Latest revision as of 14:25, 16 October 2014

Author: Lea Hayes

Contents

[edit] 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.

[edit] Screenshot

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

[edit] 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.

[edit] 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.

[edit] 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).Name + ".asset");
	}
 
}

[edit] Assets/Editor/YourUnityIntegration.cs

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

[edit] Assets/YourScriptableObject.cs

using UnityEngine;
 
public class YourScriptableObject : ScriptableObject {
 
}

[edit] See also

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox