Version: 2022.3
言語: 日本語

ScriptableObject

class in UnityEngine

/

継承:Object

マニュアルに切り替える

説明

A class you can derive from if you want to create objects that live independently of GameObjects.

Use ScriptableObjects to centralise data in a way that can be conveniently accessed from scenes and assets within a project.

Instantiate ScriptableObject objects with CreateInstance.

You can save ScriptableObjects to asset files either from the Editor UI (see CreateAssetMenuAttribute), or by calling AssetDatabase.CreateAsset from a script. You can also generate ScriptableObjects as an output from a ScriptedImporter. See AssetImportContext.AddObjectToAsset.

If a ScriptableObject has not been saved to an asset, and it is referenced from an object in a scene, Unity serializes it directly into the scene file. For ScriptableObjects that have only a single persistent instance within a project, use the ScriptableSingleton<T0> base class.

Access previously saved objects using AssetDatabase, for example AssetDatabase.LoadAssetAtPath. When a ScriptableObject is referenced from a field on a MonoBehaviour, the ScriptableObject is automatically loaded, so a script can simply use the value of the field to reach it.

The C# fields of a ScriptableObject are serialized exactly like fields on a MonoBehaviour. See Script Serialization for details. representation for that sort of data.

Classes that include big arrays, or other potentially large data, should be declared with the PreferBinarySerialization attribute, because YAML is not an efficient representation for that sort of data.

After a ScriptableObject is destroyed, the C# object remains in memory until garbage is collected. A ScriptableObject in this state acts as if it is null. For example, it returns true for a "obj == null" check. However, this class doesn't support the null-conditional operator (?.) and the null-coalescing operator (??).

The following example demonstrates a typical use of a ScriptableObject: different types of vehicle parameters are represented in the fields of a VehicleTypeInfo class, derived from ScriptableObject. Each type of vehicle would have its own asset file, with the parameter values set appropriately for the type. Each instance of the vehicle in the game would have a reference to the asset corresponding to its type, rather than keeping its own redundant copy of each parameter. This design makes it convenient to tweak vehicle behaviour in a central location. It is also good for performance, especially in cases where the size of the shared data is substantial.

The first script of the example implements a class derived from ScriptableObject.

using UnityEngine;

[CreateAssetMenu] public class VehicleTypeInfo : ScriptableObject { // Class that represents a specific type of vehicle [Range(0.1f, 100f)] public float m_MaxSpeed = 0.1f;

[Range(0.1f, 10f)] public float m_MaxAcceration = 0.1f;

// This class could have many other vehicle parameters, such as Turning Radius, Range, Damage etc }

The second script implements a MonoBehaviour that uses the ScriptableObject.

using UnityEngine;
using UnityEditor;

public class VehicleInstance : MonoBehaviour { // Snippet of a MonoBehaviour that would control motion of a specific vehicle. // In PlayMode it accelerates up to the maximum speed permitted by its type

[Range(0f, 200f)] public float m_CurrentSpeed;

[Range(0f, 50f)] public float m_Acceleration;

// Reference to the ScriptableObject asset public VehicleTypeInfo m_VehicleType;

public void Initialize(VehicleTypeInfo vehicleType) { m_VehicleType = vehicleType; m_CurrentSpeed = 0f; m_Acceleration = Random.Range(0.05f, m_VehicleType.m_MaxAcceration); }

void Update() { m_CurrentSpeed += m_Acceleration * Time.deltaTime;

// Use parameter from the ScriptableObject to control the behaviour of the Vehicle if (m_VehicleType &amp;&amp; m_VehicleType.m_MaxSpeed < m_CurrentSpeed) m_CurrentSpeed = m_VehicleType.m_MaxSpeed;

gameObject.transform.position += gameObject.transform.forward * Time.deltaTime * m_CurrentSpeed; } }

public class ScriptableObjectVehicleExample { [MenuItem("Example/Setup ScriptableObject Vehicle Example")] static void MenuCallback() { // This example programmatically performs steps that would typically be performed from the Editor's user interface // to creates a simple demonstration. When going into Playmode the three objects will move according to the limits // set by their vehicle type.

// Step 1 - Create or reload the assets that store each VehicleTypeInfo object. VehicleTypeInfo wagon = AssetDatabase.LoadAssetAtPath<VehicleTypeInfo>("Assets/VehicleTypeWagon.asset"); if (wagon == null) { // Create and save ScriptableObject because it doesn't exist yet wagon = ScriptableObject.CreateInstance<VehicleTypeInfo>(); wagon.m_MaxSpeed = 5f; wagon.m_MaxAcceration = 0.5f; AssetDatabase.CreateAsset(wagon, "Assets/VehicleTypeWagon.asset"); }

VehicleTypeInfo cruiser = AssetDatabase.LoadAssetAtPath<VehicleTypeInfo>("Assets/VehicleTypeCruiser.asset"); if (cruiser == null) { cruiser = ScriptableObject.CreateInstance<VehicleTypeInfo>(); cruiser.m_MaxSpeed = 75f; cruiser.m_MaxAcceration = 2f; AssetDatabase.CreateAsset(cruiser, "Assets/VehicleTypeCruiser.asset"); }

// Step 2 - Create some example vehicles in the current scene { var vehicle = GameObject.CreatePrimitive(PrimitiveType.Sphere); vehicle.name = "Wagon1"; var vehicleBehaviour = vehicle.AddComponent<VehicleInstance>(); vehicleBehaviour.Initialize(wagon); }

{ var vehicle = GameObject.CreatePrimitive(PrimitiveType.Sphere); vehicle.name = "Wagon2"; var vehicleBehaviour = vehicle.AddComponent<VehicleInstance>(); vehicleBehaviour.Initialize(wagon); }

{ var vehicle = GameObject.CreatePrimitive(PrimitiveType.Cube); vehicle.name = "Cruiser1"; var vehicleBehaviour = vehicle.AddComponent<VehicleInstance>(); vehicleBehaviour.Initialize(cruiser); } } }

Static 関数

CreateInstanceScriptableObject のインスタンスを作成します。

メッセージ

Awake ScriptableObject スクリプトを開始するとき、この関数は呼び出されます。
OnDestroyScriptableObject が破棄されるとき、この関数は呼び出されます。
OnDisableScriptableObject クラスのオブジェクトがスコープを外れるとき、この関数は呼び出されます。
OnEnableオブジェクトがロードされたとき、この関数は呼び出されます。
OnValidateEditor-only function that Unity calls when the script is loaded or a value changes in the Inspector.
Resetデフォルト値にリセットします

継承メンバー

変数

hideFlagsShould the object be hidden, saved with the Scene or modifiable by the user?
nameオブジェクト名

Public 関数

GetInstanceIDGets the instance ID of the object.
ToStringReturns the name of the object.

Static 関数

DestroyRemoves a GameObject, component or asset.
DestroyImmediateDestroys the object obj immediately. You are strongly recommended to use Destroy instead.
DontDestroyOnLoadDo not destroy the target Object when loading a new Scene.
FindAnyObjectByTypeRetrieves any active loaded object of Type type.
FindFirstObjectByTypeRetrieves the first active loaded object of Type type.
FindObjectOfTypeタイプ type から最初に見つけたアクティブのオブジェクトを返します
FindObjectsByTypeRetrieves a list of all loaded objects of Type type.
FindObjectsOfTypeGets a list of all loaded objects of Type type.
Instantiateoriginal のオブジェクトをクローンします

Operator

boolオブジェクトが存在するかどうか
operator !=二つのオブジェクトが異なるオブジェクトを参照しているか比較します
operator ==2つのオブジェクト参照が同じオブジェクトを参照しているか比較します。
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961