![]() ![]() Return to Unity and go to the Hierarchy window. OnMouseDown is a built-in MonoBehaviour function that will be called when the user clicks the mouse.Įxamples of how to retrieve the data from your ScriptableObject asset. ![]() The data container for this sword’s data. Here’s what you’ve added with the above code: Open up Sword.cs and add the following code: Open up SwordData.cs and add the following under the fields that you added earlier: Now it’s time to get the data from these ScriptableObjects.įirst, you need to add some public getter methods so that other scripts can access the private fields inside your ScriptableObject. Be sure to use the appropriate sprites located under the Sword Icons folder for the Icon Sprite field:Ĭongrats! You’ve created a ScriptableObject and set up various assets using that ScriptableObject. Try to give them all a unique description, gold cost, and attack damage. Here, you’ll see an asset to store information about this particular sword. Now, rename each asset accordingly to match its respective prefab:Ĭlick on the first Sword Data asset in the Sword Data folder, and take a look at the Inspector window: Select the asset and duplicate it six times ( Ctrl/Cmd + D) to create seven total Sword Data assets, one for each of the swords. The new Sword Data asset should still have the default fileName specified earlier. Inside the newly created Sword Data folder, create your first Sword Data asset. Stay organized by creating a folder under your Scripts folder called Scriptable Objects, and then another folder inside of the Scriptable Objects folder called Sword Data. ![]() It should be located in the second grouping underneath the Folder asset:Īlternatively, you should be able to right click within the Project window and see your new Sword Data asset there as well: If all went well, you should be able to go to Assets ► Create and see your new Sword Data asset on the menu. So 51 will put your new asset in the second grouping of the Asset Menu. Unity separates assets into sub-groups by factors of 50. order: Where the asset will be located within the Asset Menu.menuName: The name of the asset as it appears in the Asset Menu.fileName: The default name when the asset is created.But before you can create these implementations, you will need to add your ScriptableObject to the Asset Menu.Īdd your ScriptableObject to the Asset Menu by giving the SwordData class the following attribute: This will let you set the values in the editor without giving access to the variable from other scripts.Įach sword will need it’s own unique implementation of the SwordData ScriptableObject. In Unity, the SerializeField attribute allows you to have private script variables that are exposed in the Inspector. attackDamage: An int to hold the attack damage of the sword.goldCost: An int to hold the gold cost of the sword.icon: A sprite to hold the icon of the sword.description: A string to hold the description of the sword.swordName: A string to hold the name of the sword.Instead, it will be treated like any other common asset that can be created, similar to creating a prefab, scene or material.įill this script with some serialized fields that will contain all the data, corresponding with the information displayed on the Sword Merchant UI. This action tells Unity that you still want to use Unity features and methods, like a typical MonoBehaviour, but you’ll no longer need to put this script onto a GameObject. Public class SwordData : ScriptableObject Inside this class, start by deriving from ScriptableObject instead of MonoBehaviour: This class will be used as a container for all the sword data that will display in your Sword Merchant shop. Under the Scripts folder, create a new script called SwordData. It’s time to make your first ScriptableObject! Sword Prefabs: Contains a prefab of every sword in the Sword Merchant scene.įirst, make sure you are in the Sword Merchant scene.Sword Icons: Contains thumbnails of each individual sword. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |