Unity技术分享Unity教程合集

ScriptObject创建工具

2016-02-26  本文已影响760人  影子丢了
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Reflection;

public class ScriptObjectCreator : EditorWindow
{
    public string scriptName;
    public string fieldName;

    public static void GetData()
    {
        string stringholderpath = "Assets/bonenames.asset";
        TestScriptObject a = AssetDatabase.LoadAssetAtPath(stringholderpath, typeof(TestScriptObject)) as TestScriptObject;
        Debug.LogError(a.test[0]);
    }
    [MenuItem("tools/CreateScriptObject")]
    public static void GetWindow()
    {
        EditorWindow.GetWindow<ScriptObjectCreator>("CreatorWindow");
    }
    void OnGUI()
    {
        EditorGUILayout.BeginVertical();
        scriptName = EditorGUILayout.TextField(scriptName, GUILayout.Width(250));
        fieldName = EditorGUILayout.TextField(fieldName, GUILayout.Width(250));
        if (GUILayout.Button(new GUIContent("确定")))
        {
            Assembly assembly = Assembly.GetExecutingAssembly();
            System.Type[] types = assembly.GetTypes();
            System.Type instanceType = null;
            foreach (var a in types)
            {
                if(a.ToString() == scriptName)
                {
                    instanceType = (System.Type)a;
                    break;
                }
            }

            ScriptableObject temp = ScriptableObject.CreateInstance(instanceType);
            string stringholderpath = "Assets/"+ fieldName + ".asset";
            AssetDatabase.CreateAsset(temp, stringholderpath);
        }
        EditorGUILayout.EndVertical();
    }
}
上一篇下一篇

猜你喜欢

热点阅读