introduction
Recently, when playing 3DRPG games, you need some attributes of players or enemies, such as blood volume, defense and so on.
Method 1
The first method I think of is to write an enemy script, and then there are many attributes in it, such as blood volume and defense. However, if I write this, every time I create an enemy object, all my attributes need to be reset by myself. If there are many attributes, it will be very troublesome.
Method 2
Then I improved it. I made a C# class that does not inherit monobehavior. It is equivalent to a data class. Then I assigned values to my properties in this class. Then, when each object is created, I assigned values to the properties of the object in the wake method of the object. In this way, you don't have to reset it manually every time. But is there a better way.
Method 3
Then I found the concept of ScriptableObject. I read several articles first. I put the links of articles that I think are good and easy to understand below.
Portal 1
Portal 2
example
I'll take the game I'm playing as an example and record it so that I don't forget how to write it in the future...
Step 1:
First, we need a configuration file. If we need to create a new configuration file, we must first write a script to inherit from ScriptableObject, and then instantiate it as a configuration file. The first step is to create a new script named CharacterData_SO, SO stands for ScriptableObject. You can see at a glance that it inherits from ScriptableObject, and then write some properties of the object.
[CreateAssetMenu(fileName = "New Data", menuName = "Character Stats/Data")] public class CharacterData_SO : ScriptableObject { [Header("Stats Info")] public int maxHealth; public int currentHealth; public int baseDefence; public int currentDefence; }
Step 2
Then create a new folder called Game Data in the Assets directory, which stores all configuration files, and then create a folder called Character Data in this directory, which stores the configuration files of all characters.
After that, you can right-click under the Character Data folder and click create to find our Character Stats. Then click Data to create a configuration file called PlayerData, and then create a configuration file called SlimeData. Post it below. Also, I have configured the properties in the Inspectors window.
Step 3
At this time, we already have a configuration file, but how can we let the character read the data in me? We can't directly mount this configuration file or the ScriptableObject inherited previously (hereinafter collectively referred to as CharacterData_SO) to our character. Then we can write a script ourselves, and then there is characterdata in this script_ The instance of so script (that is, the configuration file), and then we can use this script to indirectly access the data in the configuration file.
So we create a script for CharacterStats, which inherits monobehavior this time.
public class CharacterStats : MonoBehaviour { public CharacterData_SO characterData; public int MaxHealth { get { return characterData == null ? 0 : characterData.maxHealth; } set { characterData.maxHealth = Mathf.Max(value, 0); } } public int CurrentHealth { get { return characterData == null ? 0 : characterData.currentHealth; } set { characterData.currentHealth = Mathf.Max(value, 0); } } public int BaseDefence { get { return characterData == null ? 0 : characterData.baseDefence; } set { characterData.baseDefence = Mathf.Max(value, 0); } } public int CurrentDefence { get { return characterData == null ? 0 : characterData.currentDefence; } set { characterData.currentDefence = Mathf.Max(value, 0); } } }
Step 4
We drag the script onto our character. Then, in the Inspector panel, drag the PlayerData configuration file onto the characterData attribute of CharacterStats. It means that we only need to declare a CharacterStats variable in our PlayerController, and then use this variable to get CharacterData_SO instance, and then we can indirectly read the data in the configuration file.
So far, the requirements have been completed.
Say at the end
ScriptableObject can be used to write configuration files, which can more easily modify some properties of our characters or some other enemies. And it can also improve the efficiency of the game. After all, there is only one configuration file for each character. It is not necessary to copy the same data for each role object.