当前位置:网站首页>In unity, how to read and write a scriptableobject object in editor and runtime
In unity, how to read and write a scriptableobject object in editor and runtime
2022-06-23 05:08:00 【dzj2021】
1、 When ScriptableObject
stay Unity in , mention MonoBehaviour, It's like a thunderbolt , Everyone can talk about it for a long time , But mentioned ScriptableObject, There should be many people who don't know .
Generally speaking ,MonoBehaviour It's a behavior script , Since it is a behavior script , So the way to take it is to hang it on GameObject On the object of the game , At every mount location , Generate a new instance .
Unlike behavior scripts ,ScriptableObject" Script " Focus on data storage . Someone wants to ask , I am here MonoBehaviour It defines a static Tables can also store data , And the global public , however , After shutting down , Yours static Is the data still there ?! Someone might say , I use txt perhaps json Save it to your local hard disk , I even store it in a database or server , You have to say that , You're really good , Mine at home !
that ScriptableObject What is it ?
- It is Unity A file or resource file used to store data in
- The data stored in it will not be lost , Even if you turn it off , Unless you uninstall your app ( use Unity Developed software )
- It can Shared by the whole project : Any scene in the whole project 、 anyplace 、 anybody , Can access these data , Including reading and writing
- It stores more data types than you think ,Int、String、Float…Material… Nothing to say , You can even define a font 【Font】 Variable of type , And put 【 Microsoft YaHei 】 Drag this font onto it , Save up , So that in some Text On , Apply this font .
2、 How to generate a ScriptableObject Resource file ?
- 1、 Define a ScriptableObject Script
- 2、 Generate a corresponding resource file
【FontsConfig.cs】 Code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// Font information configuration
/// establish asset Menu entry for :
/// Asset Catalog , Right click -> Create -> Persistent data creation / Create font configuration information
/// </summary>
[CreateAssetMenu(fileName = "FontsConfig", menuName = " Persistent data creation / Create font configuration information ")]
public class FontsConfig : ScriptableObject
{
/// <summary>
/// Default font
/// </summary>
public Font defaultFont;
/// <summary>
/// Fields for testing
/// </summary>
public int textIndex;
}
Generate corresponding resources :Asset Catalog , Right click -> Create -> Persistent data creation / Create font configuration information 
What's in there ?
Pictured : When you click on it FontsConfig.asset after , You can drag some fonts into this variable , Store in advance for future use .
3、run time How to use ScriptableObject Resource file
- First ScriptableObject Resource files should be stored in a persistent directory , such as Resources perhaps StreamingAssets Inside
- You can do it in one MonoBehaviour The behavior script defines a ScriptableObject The variable of , Save it in static Inside , For direct access to .
public ScriptableObject myFontsConfig2; // Definition asset resources
public static ScriptableObject s_myFontsConfig2; //static Easy to access anywhere
private void Awake()
{
s_myFontsConfig2 = myFontsConfig2; // Instance variables are bound to global variables
}
【FontsManager .cs】 Script code listing
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// Font management script
/// </summary>
public class FontsManager : MonoBehaviour
{
/// <summary>
/// Font configuration information
/// </summary>
[Header(" Font configuration information ")]
[SerializeField]
public ScriptableObject myFontsConfig2;
public static ScriptableObject s_myFontsConfig2;
private void Awake()
{
s_myFontsConfig2 = myFontsConfig2;
}
#if UNITY_EDITOR
[ContextMenu(" Set up the data ")]
#endif
void SetIndex()
{
var r = s_myFontsConfig2 as FontsConfig;
r.textIndex = r.textIndex + 1;
}
}
You can attach the script to the scene object , Then put the newly generated asset Drag the resource file to myFontsConfig2 Variables on the 
Four 、 How to be in Unity Editor Use the resource file in the state
- Read resource file object
- Read the font field from the object
Key code :
var instance = (FontsConfig)AssetDatabase.LoadAssetAtPath("Assets/Resources/Config/fontConfig.asset", typeof(FontsConfig));
var defaultFont = instance.defaultFont;
边栏推荐
猜你喜欢

云原生数据库如荼如火,未来可期

飞桨框架v2.3发布高可复用算子库PHI!重构开发范式,降本增效

使用teqcplot对teqc 质量分析结果进行可视化展示

微信小程序:老年祝福短视频

JSP entry notes

Amazing tips for using live chat to drive business sales

Metadata management Apache Atlas Compilation (embedded) deployment and various error records encountered

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

VGg Chinese herbal medicine identification

Arduino温湿度传感器DHT11(含代码)
随机推荐
JSP entry notes
Laravel 8.4 routing problem. At the end is the cross reference table on the left side of the editor, which can be understood by Xiaobai
【Mac】安全性与隐私中没有任何来源选项
ICER skill 01 regular matching
【C语言】关键字
MVC三層架構
Learn to draw Er graph in an article
Mini Homer - can you get a remote map data transmission link for hundreds of yuan?
laravel8实现图片验证码
Dolphin scheduler 2.0.5 spark task test summary (source code optimization)
Parameter passing of 18 generator function
ICer技能03Design Compile
Thinkphp6 template replacement
Brief ideas and simple cases of JVM tuning - why do you need JVM tuning?
工作5-8年的外包测试员,平均年薪有多少?
微信小程序:星际旅行飞船乘坐票制作生成
How to better organize the minimum web api code structure
Apache atlas quick start
Go learning record II (window)
Getting started with the shutter AppBar