美文网首页
7.0ScriptableObject创建数据

7.0ScriptableObject创建数据

作者: 莫忘初心_倒霉熊 | 来源:发表于2021-02-18 14:55 被阅读0次

创建一个ScriptableObject脚本

方式一

ScriptableObject脚本需要继承自ScriptableObject。如下:

using UnityEngine;

[CreateAssetMenu(fileName ="LevelSettingsScriptableObject",menuName ="CreateLevelSettingsScriptableObject",order =0)]
public class LevelSettings : ScriptableObject
{
    public float TotalTime = 60;
    public AudioClip BGM;
    public Sprite Background;
}

CreateAssetMenu属性用于创建一个右键菜单,如图:


CreateAssetMenu属性
LevelSettingsScriptableObject

方式二

除此我们也可以自定义一个菜单创建ScriptableObject,代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class MenuItems 
{       
    [MenuItem("Tools/Level Creator/New Level Settings")]
    private static void NewLevelSettings()
    {
        string path = EditorUtility.SaveFilePanelInProject("New Level Settings", "LevelSettings", "asset",
            "Define the name for the LevelSettings asset.");
        if (!string.IsNullOrEmpty(path))
        {
            CreateAsset<LevelSettings>(path);
        }
    }    
    public static T CreateAsset<T>(string path) where T:ScriptableObject
    {
        T dataClass = ScriptableObject.CreateInstance<T>();
        AssetDatabase.CreateAsset(dataClass,path);
        AssetDatabase.Refresh();
        AssetDatabase.SaveAssets();
        return dataClass;
    }
}

点击如图的菜单即可。


自定义菜单

ScriptableObject使用

using UnityEngine;

public class Test : MonoBehaviour
{
    public LevelSettings levelSettings;
    void Start()
    {
        levelSettings = ScriptableObject.CreateInstance<LevelSettings>();
        Debug.Log(levelSettings.TotalTime);     //60
        levelSettings.TotalTime = 10;
        Debug.Log(levelSettings.TotalTime);     //10
        Debug.Log(ScriptableObject.CreateInstance<LevelSettings>().TotalTime);      //60
    }
}

另一种使用方式,首先创建一个ScriptableObject资源,然后在赋值给脚本,如图



TotalTime默认值为60,如图:


默认值
当通过脚本修改TotalTime为10时,即使停止运行游戏,LevelSettings的TotalTime也会修改为10,这一点需要尤为注意。
脚本如下:
using UnityEngine;

public class Test : MonoBehaviour
{
    public LevelSettings levelSettings;
    void Start()
    {
        levelSettings.TotalTime = 10;
    }
}
修改之后,停止游戏

相关文章

  • 7.0ScriptableObject创建数据

    创建一个ScriptableObject脚本 方式一 ScriptableObject脚本需要继承自Scripta...

  • Sqlite3__代码操作

    创建数据库 创建表 插入数据 查询数据

  • SQL

    创建库 创建表 插入数据 更新数据 查询数据 删除数据

  • SQLite 常用操作

    创建数据库 创建表 插入数据 查询数据 更新数据 删除数据

  • 85-实战-单表访问方法

    一、数据准备 1.1、创建数据库 1.2、创建数据 设置MySQL可以创建 函数 创建随机字符串 函数 创建随机数...

  • MAC下mysql中文乱码问题

    1、基本命令 创建数据库 创建数据库并设置编码格式 创建数据表 创建数据表并设置表编码格式 插入数据 查询数据 更...

  • sql 基础使用和语法教程

    1、创建数据库 1)创建数据库 istester 2)查看数据库创建是否成功 2、在isTester数据库下,创建...

  • 2. SQL Server 2008创建数据库和表

    一.利用鼠标操作的方式创建数据库和表 创建数据库--创建表--设置数据类型--设置主码和其他数据约束 创建数据库步...

  • MySQL生成大量测试数据方法

    Mysql创建测试大量测试数据 修改mysql配置 创建测试数据库 创建数据表 创建随机字符串函数 创建存储过程 ...

  • MySQL基础操作

    MySQL基础操作 创建数据库 创建数据库,该命令的作用: 如果数据库不存在则创建,存在则不创建。 创建RUNOO...

网友评论

      本文标题:7.0ScriptableObject创建数据

      本文链接:https://www.haomeiwen.com/subject/rfogxltx.html