首页 > 解决方案 > Create a Scriptable Object Instance through a Constructor

问题描述

I have a Character Scriptable Object and I have several methods in it. Like GetDate,GetAge (which I am not showing here). I have created a Constructor there, but I don't know how to create an instance of that ScriptableObject through the Constructor. That's what I want to do.

I have tried to just type the values in the inspector but that did not run the methods in the Constructor.

Character.cs

[CreateAssetMenu]
[Serializable]
public class Character : Scriptable Object
{
    // Attributes
    public string firstName;
    public string middleName;
    public string lastName;
    public string fullName;
    public bool isMale;

    // Constructor
    public Character(string _firstName, string _middleName, string _lastName, bool _isMale)
    {

        firstName = _firstName;
        middleName = _middleName;
        lastName = _lastName;

        // All The Functions 
        fullName = string.Format("{0} {1} {2}", firstName, middleName, 
        lastName);

        isMale = _isMale;

        // and all the other functions like date calculator.
    }
}

I want to create an instance of this Scriptable Object through this Construction. Something like

Character char("x", "y", "z", true);

or when I type the required fields in the Unity Inspector, then it will run all the methods like that full name string.format.

标签: c#unity3d

解决方案


You can't create a new ScriptableObject Instance using a constructor (much like a monobehaviour). so you need to use Unity API methods

I have added a working sample

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

[CreateAssetMenu]
[Serializable]
public class Character : ScriptableObject
{
// Attributes
public string firstName;
public string middleName;
public string lastName;
public string fullName;
public bool isMale;

// Constructor
public Character(string _firstName, string _middleName, string _lastName, bool _isMale)
{

    firstName = _firstName;
    middleName = _middleName;
    lastName = _lastName;

    // All The Functions 
    fullName = string.Format("{0} {1} {2}", firstName, middleName,
    lastName);

    isMale = _isMale;

    // and all the other functions like date calculator.
}

public void Initialize(string _firstName, string _middleName, string _lastName, bool _isMale)
{
    firstName = _firstName;
    middleName = _middleName;
    lastName = _lastName;

    // All The Functions 
    fullName = string.Format("{0} {1} {2}", firstName, middleName,
    lastName);

    isMale = _isMale;
}

}

and here is the class that creates the Character

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class Tester : MonoBehaviour
{
int characterCounter = 0;

// Update is called once per frame
void Update()
{
    if(Input.GetMouseButtonDown(0))
    {
        Character c = CreateMyAsset();
        c.Initialize("a", "b", "c", true);
    }
}

public Character CreateMyAsset()
{
    Character asset = ScriptableObject.CreateInstance<Character>();

    string assetName = "Assets/c" + characterCounter + ".asset";
    characterCounter++;

    AssetDatabase.CreateAsset(asset, assetName);
    AssetDatabase.SaveAssets();

    EditorUtility.FocusProjectWindow();

    Selection.activeObject = asset;
    return asset;
}


}

推荐阅读