当前位置:网站首页>Unity中,如何在【编辑器】和【运行时】状态下读写一个ScriptableObject对象
Unity中,如何在【编辑器】和【运行时】状态下读写一个ScriptableObject对象
2022-06-23 03:53:00 【dzj2021】
1、什么时ScriptableObject
在Unity中,提到MonoBehaviour,简直如雷贯耳,大家都能扒拉吧拉说上半天,但是提到ScriptableObject,应该有许多人不知道。
通俗的讲,MonoBehaviour是行为脚本,既然是行为脚本,那么它的服用方法自然就是挂在GameObject游戏对象上,在每一个挂载的地方,生成一个新的实例。
与行为脚本不同,ScriptableObject"脚本"则专注于数据的存储。有人要问,我在MonoBehaviour里面定义一个static的表也可以存数据,而且全局公用,但是,关机后,你的static数据还在不在呢?!也许有人说,我用txt或者json存到本地硬盘嘛,甚至我存在数据库或者服务器里,你要这么讲,确实你厉害,家里有矿啊!
那ScriptableObject到底是啥呢?
- 它是Unity中用来存储数据的文件或者资源文件
- 它存的数据不会丢失,即便你关机,除非你卸载你的应用(用Unity开发的软件)
- 它可以 被整个工程共享使用:整个工程里面的任何场景、任何地方、任何人,都可以访问这些数据,包括读和写
- 它存的数据类型超过你的想象,Int、String、Float…Material…都不在话下,甚至你可以定义一个字体【Font】类型的变量,并把【微软雅黑】这种字体拖到它上面,存起来,以便在某些Text上,应用这种字体。
2、如何生成一个ScriptableObject资源文件?
- 1、定义一个ScriptableObject脚本
- 2、生成一个对应的资源文件
【FontsConfig.cs】代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 字体信息配置
/// 创建asset的菜单入口:
/// Asset目录,右键 -> Create -> 持久数据创建/创建字体配置信息
/// </summary>
[CreateAssetMenu(fileName = "FontsConfig", menuName = "持久数据创建/创建字体配置信息")]
public class FontsConfig : ScriptableObject
{
/// <summary>
/// 默认的字体
/// </summary>
public Font defaultFont;
/// <summary>
/// 用于测试的字段
/// </summary>
public int textIndex;
}
生成对应的资源:Asset目录,右键 -> Create -> 持久数据创建/创建字体配置信息
里面到底有啥?
如图:当你点开FontsConfig.asset后,你能够把一些字体拖到这个变量里面,预存起来以备后用。
3、run time如何使用ScriptableObject资源文件
- 首先ScriptableObject资源文件要存在一个持久的目录,比如Resources或者StreamingAssets里面
- 你可以在一个MonoBehaviour行为脚本里面定义一个ScriptableObject的变量,把它存到static里面,以便直接访问。
public ScriptableObject myFontsConfig2; //定义asset资源
public static ScriptableObject s_myFontsConfig2; //static方便任何地方访问
private void Awake()
{
s_myFontsConfig2 = myFontsConfig2; //实例变量绑定给全局变量
}
【FontsManager .cs】脚本代码清单
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 字体管理脚本
/// </summary>
public class FontsManager : MonoBehaviour
{
/// <summary>
/// 字体配置信息
/// </summary>
[Header("字体配置信息")]
[SerializeField]
public ScriptableObject myFontsConfig2;
public static ScriptableObject s_myFontsConfig2;
private void Awake()
{
s_myFontsConfig2 = myFontsConfig2;
}
#if UNITY_EDITOR
[ContextMenu("设置数据")]
#endif
void SetIndex()
{
var r = s_myFontsConfig2 as FontsConfig;
r.textIndex = r.textIndex + 1;
}
}
你可以把该脚本挂载到场景物体上,然后把刚生成的asset资源文件拖到myFontsConfig2变量上
四、如何在Unity Editor状态下使用该资源文件
- 读取资源文件对象
- 从该对象里面读取字体字段
关键代码:
var instance = (FontsConfig)AssetDatabase.LoadAssetAtPath("Assets/Resources/Config/fontConfig.asset", typeof(FontsConfig));
var defaultFont = instance.defaultFont;
边栏推荐
- 轮播图的实现
- 传统意义上的互联网式的平台或将不复存在,一个融合的产业特质和互联网特质的全新产业
- 美团好文:从预编译的角度理解Swift与Objective-C及混编机制
- Small problems in the spoole framework for TCP communication in PHP
- Meituan's good article: understand swift, Objective-C and the mixing mechanism from the perspective of precompiling
- Cloud native database is in full swing, and the future can be expected
- 【图论】—— 二分图
- [OFDM communication] simulation of OFDM multi-user resource allocation based on MATLAB [including Matlab source code 1902]
- ICER skills 03design compile
- Shadertoy基础教学02、画笑脸
猜你喜欢

Dolphin scheduler 2.0.5 task test (spark task) reported an error: container exited with a non zero exit code 1

【Proteus仿真】Arduino UNO+PCF8574+LCD1602+MPX4250电子秤

Function declaration and call of 17 generator

【毕业季_进击的技术er】送别过去两年迷茫的自己。重整旗鼓,大三我来啦

元数据管理Apache Atlas编译(内嵌式)部署及遇到各种错误记录

【图论】—— 二分图

Install and run mongodb under win10

Flask Foundation: environment setup + configuration + mapping between URL and attempt + redirection + database connection

Mini Homer——几百块钱也能搞到一台远距离图数传链路?

AlertManager告警的单独使用及prometheus配置告警规则使用
随机推荐
Shadertoy basic teaching 01. Circle drawing (explanation of smoothstep() function)
The solution to prompt "this list creation could be rewritten as a list literal" when adding elements to the list using the append() method in pychart
Icer Skill 02makefile script Running VCS Simulation
teqc进行GNSS数据质量分析时生成的s文件介绍
Openwrt directory structure
实战| 记一次借Viper来多重内网渗透
【OFDM通信】基于matlab OFDM多用户资源分配仿真【含Matlab源码 1902期】
Altium designer 09 screen printing displays a green warning near the pad. How to prevent it from alarming?
dolphinscheduler海豚调度升级代码改造-UpgradeDolphinScheduler
Install and run mongodb under win10
Precautions for running high-frequency and high-speed signal lines near PCB board - basic principles for high-frequency and high-speed signal design
Transformers中的动态学习率
An understanding of free() (an error in C Primer Plus)
Summary of switched reluctance motor suspension drive ir2128
The solution to prompt "this dictionary creation could be rewritten as a dictionary literal" when updating the dictionary key value in pychart
How to better organize the minimum web api code structure
Course design C for freshmen -- clothing management system
接收传来得文件并下载(简单用法)a标签
dolphinscheduler 2.0.5 spark 任务测试总结(源码优化)
Using editor How to handle MD uploading pictures?