Skip to content

Instantly share code, notes, and snippets.

@nakamura001
Forked from tsubaki/ScriptableObjectToAsset.cs
Last active December 27, 2015 12:42
Show Gist options
  • Save nakamura001/94d1a624174ce006c1b5 to your computer and use it in GitHub Desktop.
Save nakamura001/94d1a624174ce006c1b5 to your computer and use it in GitHub Desktop.
ScriptableObjectをAssetsファイルとして出力する汎用スクリプト
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
/// <summary>
// ScriptableObjectをプレハブとして出力する汎用スクリプト
/// </summary>
// <remarks>
// 指定したScriptableObjectをプレハブに変換する。
// 1.Editorフォルダ下にCreateScriptableObjectPrefub.csを配置
// 2.ScriptableObjectのファイルを選択して右クリック→Create ScriptableObjectを選択
// </remarks>
public class ScriptableObjectToAsset
{
readonly static string[] labels = {"Data", "ScriptableObject", string.Empty};
[MenuItem("Assets/Create ScriptableObject")]
static void Crate ()
{
foreach (Object selectedObject in Selection.objects) {
// get path
string path = getSavePath (selectedObject);
// create instance
ScriptableObject obj = ScriptableObject.CreateInstance (selectedObject.name);
AssetDatabase.CreateAsset (obj, path);
labels[2] = selectedObject.name;
// add label
ScriptableObject sobj = AssetDatabase.LoadAssetAtPath (path, typeof(ScriptableObject)) as ScriptableObject;
AssetDatabase.SetLabels (sobj, labels);
EditorUtility.SetDirty (sobj);
}
}
static string getSavePath (Object selectedObject)
{
string objectName = selectedObject.name;
string dirPath = Path.GetDirectoryName (AssetDatabase.GetAssetPath (selectedObject));
return AssetDatabase.GenerateUniqueAssetPath(string.Format ("{0}/{1}.asset", dirPath, objectName));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment