Unity ScriptableObject的创建

代码很简单,直接上代码了。

using UnityEngine;
using UnityEditor;
using System.IO;
using System;

public class ScriptableObjectCreater : EditorWindow
{
    string className;
    string scriptableObjectName;

    [MenuItem("Tools/MyTool/CreateScriptableObject")]
    static void AddWindow()
    {
        //创建窗口
        Rect wr = new Rect(0, 0, 300, 200);
        ScriptableObjectCreater win = (ScriptableObjectCreater)EditorWindow.GetWindowWithRect(typeof(ScriptableObjectCreater), wr, true, "创建配置");
        win.Show();
    }

    void OnGUI()
    {
        className = EditorGUILayout.TextField("类名:", className);
        scriptableObjectName = EditorGUILayout.TextField("文件名:", scriptableObjectName);
        if (GUILayout.Button("创建", GUILayout.Width(200)))
        {
            string path = "Assets/Resources/ScriptableObject";

            DirectoryInfo dirInfo = new DirectoryInfo(path);
            if (!dirInfo.Exists)
            {
                Debug.LogError(string.Format("can found path={0}", path));
                return;
            }

            // ScriptableObject对象要用ScriptableObject.CreateInstance创建
            var ddata = ScriptableObject.CreateInstance(className);

            // 创建一个asset文件
            string assetPath = string.Format("{0}/{1}.asset", path, scriptableObjectName);
            AssetDatabase.CreateAsset(ddata, assetPath);

            Debug.Log("Finish!");
        }
    }
}

ScriptableObject详细介绍:
http://blog.csdn.net/candycat1992/article/details/52181814


每天进步一点点。

你可能感兴趣的:(Unity3D)