在Unity编辑器中创建脚本对象

时间:2018-05-28 10:44:52

标签: c# unity3d

显然我很擅长在我的大学里聆听,因为我无法解决这个问题,即使是谷歌... 如何在编辑器中创建可编写脚本的对象?我打开了项目,看起来像这样:

enter image description here

单击“创建”按钮,就像要创建文件夹或C#脚本一样。

enter image description here

从弹出菜单中选择ScriptableObject。

enter image description here

获取此面板并在为其选择脚本后完成对象。

问题是:我没有ScriptableObject按钮。我有一个脚本是一个可编写脚本的对象(以确保我甚至复制了大学项目中的那个)。我重新启动Unity,我检查是否有任何软件包安装(没有),我google了相当多。但我似乎无法让这个工作......

我有什么需要先安装或添加的吗? 提前谢谢!

2 个答案:

答案 0 :(得分:3)

您需要另一个脚本来添加按钮,该按钮将从该脚本化对象创建实例。类似的东西

using UnityEngine;
using System.Collections;
using UnityEditor;

public class MakeScriptableObject {
    [MenuItem("Assets/Create/My Scriptable Object")]
    public static void CreateMyAsset()
    {
        MyScriptableObjectClass asset = ScriptableObject.CreateInstance<MyScriptableObjectClass>();

        AssetDatabase.CreateAsset(asset, "Assets/NewScripableObject.asset");
        AssetDatabase.SaveAssets();

        EditorUtility.FocusProjectWindow();

        Selection.activeObject = asset;
    }
}

您可以在统一网站上查看Introduction to Scriptable Objects tutorial

答案 1 :(得分:2)

我无法发表评论,所以只需将其作为答案: 不要忘记使用 UnityEditor.AssetDatabase.GenerateUniqueAssetPath 因为简单的 AssetDatabase.CreateAsset 可以删除您的数据:

using UnityEngine;
using System.Collections;
using UnityEditor;

public class MakeScriptableObject
{
    [MenuItem("Assets/Create/My Scriptable Object")]
    public static void CreateMyAsset()
    {
        MyScriptableObjectClass asset = ScriptableObject.CreateInstance<MyScriptableObjectClass>();

        string name = UnityEditor.AssetDatabase.GenerateUniqueAssetPath("Assets/NewScripableObject.asset");
        AssetDatabase.CreateAsset(asset, name);
        AssetDatabase.SaveAssets();

        EditorUtility.FocusProjectWindow();

        Selection.activeObject = asset;
    }
}