ScriptableObject
UnityEngine 中的类
/
继承自:Object
/
实现于:UnityEngine.CoreModule
建议更改
成功!
感谢您帮助我们提升 Unity 文档的质量。虽然我们无法接受所有提交内容,但我们确实阅读了用户提出的每项更改建议,并在适用的情况下进行更新。
关闭
提交失败
由于某些原因,您的更改建议无法提交。请<a>稍后再试</a>。感谢您抽出时间帮助我们提升 Unity 文档的质量。
关闭
切换到手册
描述
如果您想创建独立于游戏对象的物体,可以从该类派生。
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
}
第二个脚本实现了一个使用 ScriptableObject 的 MonoBehaviour。
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 && 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);
}
}
}