ScriptableObject.CreateInstance() usage in Unity

In Unity, `ScriptableObject.CreateInstance()` is a method used to create ScriptableObject instances. ScriptableObject is a special Unity class that can be used to create custom serializable data objects, which can be used in projects to store configuration, data, status and other information. ScriptableObject does not need to be mounted to a game object, so it can be more conveniently used to save and manage data.
 

ScriptableObject.CreateInstance<Type>();

Among them, `Type` is the type of ScriptableObject you want to create. You should replace it with your own defined ScriptableObject type.

Here is an example, assuming you have a custom ScriptableObject type called `MyDataObject` that is used to save some data:

using UnityEngine;



public class MyDataObject : ScriptableObject

{

    public int intValue;

    public string stringValue;

}



public class ExampleScript : MonoBehaviour

{

    void Start()

    {

        // 创建一个新的MyDataObject实例

        MyDataObject newDataObject = ScriptableObject.CreateInstance<MyDataObject>();



        // 设置数据

        newDataObject.intValue = 42;

        newDataObject.stringValue = "Hello, ScriptableObject!";



        // 这个新的MyDataObject实例现在可以在项目中被使用和保存

        // 你可以将它作为Asset保存到磁盘,也可以在其他Script中引用和使用它

    }

}

After creating a ScriptableObject instance, you can choose to save it as an Asset so you can use it in the editor and share data between different scenes and objects. To save a ScriptableObject instance as an Asset, simply right-click anywhere in the Hierarchy panel or Project panel, then select "Create" and select your custom ScriptableObject type.

Summary: The `ScriptableObject.CreateInstance()` method allows you to create custom ScriptableObject instances at runtime, and then save, use and manage these data objects in your project.

`MyDataObject newDataObject = ScriptableObject.CreateInstance<MyDataObject>(); and MyDataObject newDataObject = ScriptableObject.CreateInstance(typeof(MyDataObject)); are equivalent. They are both used to create ScriptableObject instances of the MyDataObject type. The effect of both is exactly the same. They will create a new instance of the `MyDataObject` type and assign it to the `newDataObject` variable.

Using `ScriptableObject.CreateInstance<MyDataObject>()` is a more concise and convenient way of writing. It will automatically obtain the `System.Type` object of `MyDataObject` type during compilation and pass it to `ScriptableObject.CreateInstance()` . This way, you don't need to manually call the `typeof()` method to get the type object.

`ScriptableObject.CreateInstance(typeof(MyDataObject))` explicitly uses the `typeof()` method to obtain the type object and passes it to `ScriptableObject.CreateInstance()`. Although this way of writing is also valid, in actual development, the more concise way of writing `ScriptableObject.CreateInstance<MyDataObject>()` is usually preferred.

Summary: Both writing methods can correctly create `ScriptableObject` instances of `MyDataObject` type, but `ScriptableObject.CreateInstance<MyDataObject>()` is a more recommended way of writing, because it is more concise, easy to read, and compiles Type information is automatically processed.

typeof() explanation

The `typeof()` method is added to the `ScriptableObject.CreateInstance()` method to tell the compiler what type of `ScriptableObject` instance to create. The `typeof()` method is a keyword in C#, used to obtain a System.Type object of a certain type. Here, `typeof()` is used to pass type information to `ScriptableObject.CreateInstance()` so that it can correctly create a `ScriptableObject` instance of the specified type.

In Unity, the parameter of the `ScriptableObject.CreateInstance()` method is a `System.Type` object, which represents the `ScriptableObject` type you want to create. Using the `typeof()` keyword, you can obtain the `System.Type` object of this type and then pass it as a parameter to the `ScriptableObject.CreateInstance()` method.

Example:

Suppose you have a custom ScriptableObject type called MyDataObject:


using UnityEngine;



public class MyDataObject : ScriptableObject

{

    // 一些数据成员

}

If you want to create a `ScriptableObject` instance of type `MyDataObject` in code, you can do this:

MyDataObject newDataObject = (MyDataObject)ScriptableObject.CreateInstance<MyDataObject>();

Here, `typeof(MyDataObject)` will return a `System.Type` object of type `MyDataObject` and pass it to `ScriptableObject.CreateInstance()` so that it knows that it is creating an instance of type `MyDataObject`.

Summary: Use the `typeof()` method to clearly tell the `ScriptableObject.CreateInstance()` method what type of `ScriptableObject` you want to create. In this way, the compiler can correctly create the corresponding instance.

In this statement, `(MyDataObject)` is a cast, used to convert the return value of `ScriptableObject.CreateInstance(typeof(MyDataObject))` to the `MyDataObject` type.

The return type of the `ScriptableObject.CreateInstance(typeof(MyDataObject))` method is `ScriptableObject`, and you want to convert it to a custom `MyDataObject` type. To achieve this, you can use casts.

In fact, this way of writing is equivalent to using generics directly `ScriptableObject.CreateInstance<MyDataObject>()`. Both create a `ScriptableObject` instance of type `MyDataObject`. Therefore, if your goal is to create an instance of the MyDataObject type, it is more concise and readable to use generics without having to perform explicit type conversions.

Here again the use of generics is shown:

MyDataObject newDataObject = ScriptableObject.CreateInstance<MyDataObject>();

This way of writing is enough to convert a `ScriptableObject` instance to the `MyDataObject` type without explicit cast. Therefore, it is recommended to use generics to create subclass instances of `ScriptableObject`.

Guess you like

Origin blog.csdn.net/qq_74158527/article/details/131878271