survivalport.blogg.se

Scriptable object architecture
Scriptable object architecture






Whenever you move the focus back to Unity, it will reimport the spreadsheet and that will trigger the assets to be recreated, using the new values within. With the script already in your project, try making some changes to the data in the spreadsheet.

scriptable object architecture

You may want to make a different data manager for each spreadsheet if your project gets too large, but keeping it all in one place simplifies the demo. In the “OnPostprocessAllAssets” method, I loop through the changed files and if any of them are in my parser dictionary, I parse that spreadsheet with the associated Action. Here I used a “static constructor” (sort of like a normal constructor, but for class initialization) to link the names of spreadsheets to an Action (a delegate which performs a task). String fileName = string.Format(".asset", filePath, enemyData.name) ĪssetDatabase.CreateAsset(enemyData, fileName) Static void OnPostprocessAllAssets(string importedAssets, string deletedAssets, string movedAssets, string movedFromAssetPaths) Parsers.Add("Enemies.csv", ParseEnemies) Public class SettingsAutoConverter : AssetPostprocessor The file path used to place the created assets puts them in a “Resources” folder which is necessary in order to load them using the “Resources.Load” method Unity provides. The path was hardcoded but can be changed to suit your needs or to match your own project organization. Also, it expects the “Enemies.csv” to be placed in a “Settings” folder. Note that the following script needs to be placed in an “Editor” folder to work properly.

Scriptable object architecture update#

Even better, it is possible to “listen” to any time that the spread sheet changes and have the assets automatically update themselves to match! Rather than manually copy the data from a spreadsheet to a scriptable object asset in the project, we will use Editor scripts to create them automatically for us. HitCount = Convert.ToInt32( elements ) Īgility = Convert.ToInt32( elements ) Public class EnemyData : ScriptableObject Here is a class that can hold an entry of our bestiary spreadsheet: The solution is found with the ScriptableObject and editor scripts. On a mobile device it could be too much to handle. That would allow quick lookup times, but caching the entire bestiary would result in an even larger memory footprint. You could try converting each entry into a data object and store it in a dictionary keyed by its name. Finding just the asset you need would require string parsing which is not efficient. You can get references to the TextAsset and parse it into data at runtime, but then you will need to hold the entire file in memory. The data here is very easy to balance, but it is not directly usable. There are more things we would actually need to track such as whether or not it is undead, what kinds of status ailments it can inflict when attacking, what it is immune to, etc.

scriptable object architecture

It shows the name of an enemy, the amount of hit points it has, the number of times it attacks, the damage it inflicts, its agility, the amount of experience you gain from killing it and the amount of gold you gain from killing it. I created a demo spreadsheet called “Enemies.csv” using some data from Final Fantasy 1 as an example. This way you can order your enemies however you like (for balance purposes), and at a glance compare how stats change over the course of the list. It would be even worse if you ever decided to change architecture such as splitting a component into multiple smaller and reusable components, or even doing something as simple as renaming a variable – all of the data which had been serialized into the prefab will be lost.Ī better approach would allow you to see your data in a spreadsheet. Although it would technically work, it would be a nightmare to try to balance the data – there isn’t a good way to look at values across multiple enemies and make sure there is a good ramp in difficulty. A naive approach could be to directly create a prefab for each enemy, already configured to use the correct sprite and or model, and with components attached and stats typed into each one. Let’s suppose we were implementing the bestiary. The bestiary alone could easily have hundreds of entries. The items in their shops (name, sprite, model, cost, etc) and the bestiary (name, hp, strength, experience award, etc) for example. If you were creating a game like Final Fantasy you would need to manage and balance A LOT of data.






Scriptable object architecture