Survivalist: Invisible Strain

Survivalist: Invisible Strain

Not enough ratings
Importing new models using Unity Asset Bundles
By Bob
How to add new types of buildings etc to your mod
   
Award
Favorite
Favorited
Unfavorite
How to import a blue or possibly red cube into the game
The first thing you need to do - if you haven't already - is follow this guide to create your own Story mod.

Next, download Unity[unity3d.com]. I'm using Unity 6000.0.35f1. (I'm not sure if it matters if you use a different version)

In Unity, start a new project (selecting "3D (Built-In Render Pipeline)" when it asks you which template to use. Make sure not to select "Universal 3D" or "High Definition 3D"). I've created a quick Example Project[survivalistgame.blogspot.com] as a guide, if that helps.

Create the prop you want to import and make it a prefab. This isn't a Unity tutorial so I won't explain in depth how to do that, but presumably you'll be creating a model in Blender[www.blender.org] or similar. Put that and all your textures in the Assets folder of your Unity project and then create a prefab from it. As a test, you can try just creating a cube. In the scene Hierarchy pane click + and select 3D Object/Cube. Drag it into the Assets folder to create a prefab.

Make sure your model has collision detection! Survivalist doesn't have full 3d collision detection for stuff like walking around the world but it is used for raycasts to check if AI can see people through your prop, or if you can shoot through it. It's also used for ragdolls and objects you throw. To give your model collision detection, add components: Box Colliders, Sphere Colliders, or Mesh Colliders. If you're using a mesh collider, make sure it's not a massively complicated mesh - go easy on the vertex count.

Next you need to add the prefab, and any models, textures or materials it uses, to an asset bundle, following the Unity Asset Bundle Workflow[docs.unity3d.com]:
  • Select the Asset you want to assign to a bundle from your Project View.
  • Examine the object in the Inspector
  • At the bottom of the Inspector, there is a section to assign AssetBundles and Variants. Use the left-hand drop down to assign the AssetBundle, and the right-hand drop down to assign the variant.
  • Click None on the left-hand drop to reveal the currently registered AssetBundle names.
  • Click New to create a new AssetBundle
  • Type in the desired AssetBundle name.

Create a folder called Editor in the Assets folders, and place a script with the following contents in the folder:
using UnityEditor; using System.IO; public class CreateAssetBundles { [MenuItem("Assets/Build AssetBundles")] static void BuildAllAssetBundles() { string assetBundleDirectory = "Assets/AssetBundles"; if(!Directory.Exists(assetBundleDirectory)) { Directory.CreateDirectory(assetBundleDirectory); } BuildPipeline.BuildAssetBundles(assetBundleDirectory, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows); } }
This script creates a menu item at the bottom of the Assets menu called Build AssetBundles that executes the code in the function associated with that tag. When you click Build AssetBundles a progress bar appears with a build dialog. This takes all the Assets you labeled with an AssetBundle name and places them in "Assets/AssetBundles".


Find the "Assets\AssetBundles" folder in Windows Explorer and copy the asset bundle and the manifest file - in my case "cube" and "cube.manifest". In your Survivalist: Invisible Strain story mod folder, create a subdirectory called AssetBundles and paste them in there.

Run Survivalist: Invisible Strain and go to Editor / Your Story / Edit Props. Click Create New and name it Cube. In the Model[0] dropdown you should be able to find your prefab - in my case assets/cube.prefab.


Set up the prop with some appropriate settings and you should now be able to place it in the game using the F8 debug menu and going to Prop Spawner.

Some notes about the prop editor:
  • The Category field defines where it will appear in the F8 Prop Spawner menu
  • The Extents Min and Extents Max fields define how big it is in terms of tiles. e.g. if Extents Min is -1, -1 and Extents Max is 1, 1, the prop will be 3x3 tiles.
  • The Type Name defines its behaviour, e.g. select Building if you want people to be able to enter it.
  • In my example I created two materials, a blue and a red one, and imported them both along with the cube prefab. I selected them in the Material Variations fields. This means the cube will randomly choose the blue or red variation when it is spawned. Similarly if you selected more than one Model in the Models dropdowns it would pick one at random.
Materials and Snow
Every prop in the world of Invisible Strain needs to support snow, which is done using a custom shader. This means you can't just use the standard Unity materials, you'll need to use one with the "Custom/StandardSnow" shader I wrote which adds snow to everything - if it's facing upwards - based on some global values such as _SnowAmount. (The moss that you see on most props in the world also comes from the same shader).

This means you'll need to add my snow shader files to your project. In fact there's 7 of them:

StandardSnow.shader
StandardCutoutSnow.shader
StandardFloraSnow.shader
StandardFloraSnow.cginc
StandardFloraSnowSurface.cginc
FloraCommon.cginc
CustomTerrainCommon.cginc

Download my Example Project[survivalistgame.blogspot.com] to get them.

Then, when you create a new material, select the Custom/StandardSnow shader at the top. Or if you have existing materials, convert them all to use that. (Unless of course you really don't want your stuff to be affected by snow, which might be the case with stuff that's under a shelter or something).
Weapon Models
What if you want to add a new model for a gun, melee weapon, or other piece of equipment? First, in the game's Editor menu go to Edit Equipment, and Create New. Enter a name. Then set up all the fields - you might want to look in BaseStory for comparison against other similar types of equipment. Make sure to tick the Can Be Equipped field.

The Type Name defines what kind of special behaviour the equipment will have, and what fields will be available. Let's use the Magnum mod as an example - the Type Name is set to Pistol.


So, just like you did with the buildings, set up a model of a gun or whatever your equipment is in Unity and add it to an asset bundle, and put that in the AssetBundles folder of your mod. Then run the game and, in the Edit Equipment screen, find the Equipped Model Name field and you should be able to set that to your asset's name in the dropdown.


This means your model will now appear attached the character when they equip that piece of equipment. But it might be a crazy scale - either enormous or tiny - or it might be offset from their hand by a huge distance. You need to set it up to have the correct position, rotation and scale. To do this, back out of the editor and start a New Game, selecting your mod.

Once the world has loaded, press F8 to open the debug menu, go to Equipment Spawner, and find your equipment at the bottom of the list. Spawn one and equip yourself with it. Hopefully you will now be able to see it, but it may be hidden due to having a small scale or something. In the debug menu go to EquippedModelEditor and start playing around with the Scale until you can see it.

You'll probably want to set Bone to RightHand so it's attached to that hand. Equipped Anim controls what set of animations they use when holding it, for the Magnum we set that to Pistol. Once you've set those you can play with the Position, Rotation and Scale until it fits as closely as possible into their hand.


Some of the other values are a little more esoteric-looking - when the player aims a gun, some IK happens on top of the base animation to set their hand positions. If you're making a gun you'll probably want to copy these values from other, existing guns. And if you're not, you can probably ignore them.

You can also put sounds in your asset bundles and override the default gun firing and reloading sounds - at the bottom of the Edit Equipment screen there are some drop-downs for this.

Clothing Models
You can also make your own clothing models - see the Trilby Hat mod for an example, and download this sample project[survivalistgame.blogspot.com] which includes the character model in Blender, which you'll need to attach your clothing to.

NOTE: I've discovered recently that there's a bug where clothing models imported with the newest versions of Blender don't work - it looks like possibly the root bone is being assigned wrong. The Blender version that works for me is v2.78a. Unity uses the latest version of Blender that you have installed, so I had to actually uninstall newer Blender versions to get clothes importing working again. Unfortunately that's the workaround, until I fix it.

In Trilby.blend you'll find a Male and Female character model, and an armature and a Trilby hat model for each. They are all on different layers, use the layers control to show them all, and the Outliner to see all the components of the scene.


If you select the Trilby models you'll see they have Armature modifiers, attaching them to UMA_Female_Rig and UMA_Male_Rig. Select the Weight Paint tool and you can see all the vertices are attached to the Head bone.


So to add a clothing model, you'll need to make a copy of this .blend file, import your models (a male one and a female one), add Armature modifiers attaching them to the skeletons, and use weight paint to attach them to the appropriate bones. Then save your blender file and put it in the Assets folder of your Unity project.

In Unity, select your Blender file. Look at its properties in the Inspector (Go to Window -> General -> Inspector if you can't see it). Untick the "Convert Units" property.



Next, in Unity, select the blender file and drag it into the scene. Right click it and select Prefab -> Unpack Completely. You'll want to make a prefab for the male model and a prefab for the female. Delete all the body part models (they are just for your reference in Blender). And delete the model and rig of the other gender. Keep the rig and model of the gender your are making (i.e. Trilby_Female and UMA_Female_Rig).


Make a prefab out of this by dragging it onto the Assets folder. Then do the same for the other gender. Set your prefabs to go into your asset bundle, as described earlier in this guide, build the asset bundle and copy it to your mod's AssetBundles folder.

Run the game, edit your mod, go to Edit Equipment, and Create New. You'll want to set the Clothing Type field (e.g. for the Trilby this is set to Hat). Then set the Male Mesh Name and Female Mesh Name fields - the models in your asset bundle should be available in these drop-downs. And you're done!

Vehicles
Creating a vehicle is similar to creating any other kind of prop. The extra steps are:
  • You need to set the Type Name to "EnterableVehicle" in the Prop Editor.
  • As with a building, you'll need to set Number of Inhabitants to 1 or more in the Prop Editor, so someone can get in and drive it
  • Set Liquid Type to Gasoline or Vegetable Oil and Liquid Capacity to something sensible
  • Set the Capture Resource Type and Repair Resource Type to CarParts and set the amount needed to something sensible
  • There are a number of extra fields for vehicles such as Mass or Vehicle HorsePower, they will be set to sensible defaults but you may want to tweak them
  • You need to set up the wheels, for which see below:

To set up the wheels, the game is looking for 4 Unity objects called "Wheel_F_R", "Wheel_F_L", "Wheel_R_R", and "Wheel_R_L". These should be child objects of your vehicle prefab in Unity.

The wheels need to rotate around their centers! When you create the vehicle in Blender the wheels should be separate objects from the chassis, and for their origin should be their center. Whereas probably the origin of the chassis will be the world origin in Blender, i.e. the point 0,0,0.

When the vehicle is imported, run the game and spawn one in the world using F8/Prop Spawner. Then highlight it and open F8/Prop Editor. Scroll down and click Calculate Wheel Pos. This should set up your wheel Pos, Offset and Radius correctly and you should see red debug graphics showing where the bottoms of each wheel are. If they look correct, scroll to the bottom and click Save Prototype.

As with all buildings you should add collider components, but just make them cover the chassis, don't add anything on the wheels. For example the van in this picture has two box colliders on its chassis (the light green boxes):


In theory this will result in a working vehicle!
"Under Construction" and "Take Over" Visuals
In v220 I introduced the "Under Construction" visuals, where you see a building taking shape piece by piece as you are building it, and also when you Take Over a building I show the windows getting boarded up. You can apply these effects to your modded buildings as well.

Copy the following files from Example Project[survivalistgame.blogspot.com] into yours:

Assets/Editor/MeshPostProcessing.cs
Assets/Scripts/MeshPostProcessSettings.cs
Assets/Scripts/MeshPostProcessSettings.cs.meta

You must include the .meta file for MeshPostProcessSettings.cs, because this script defines a Unity Component that you can add to your prefabs, and it must have the guid from this .meta file so that the game will recognize it. (dc011a5edef1bd64bad2a56597c9c913)

You will need to set your model to writable so that the script can include some extra info with it. Select the model file (.blend, .fbx, or whatever) in Unity and tick the Read/Write checkbox on the Model tab (in the Inspector window, about halfway down underneath Meshes and Mesh Compression). Then click the Apply button at the bottom of that tab.

On the root object of building prefabs, click Add Component and add a MeshPostProcessSettings component. Set the ExtraInfo field to ConstructionProgressInfo. Right-click your prefab and click Reimport. Check Unity's Console window and make sure there are no warnings after it finishes.

What this does is find groups of connected vertices in your model, and assign each of them a number based on how high up they are in the model. The construction effect will then make the lower ones visible first as the building progresses, so you'll see the building taking shape from the ground up. This works best if the model consists of lots of small groups of connected vertices (e.g. planks of wood) that are isolated from each other. It won't work if all the vertices are connected together in one big shape.

Also, make sure you have the latest version of StandardSnow.shader from the example project as this is needed to hide the vertices that haven't been "built" yet.

As for the boarded up windows, just Open your prefab in Unity and right click on the root object, click 3D Object, Plane. Name your plane "BoardedWindow1". Set the scale to about 0.1 or something in that region, depending on how large yours window are, and place the plane on top of one of your windows that you want to show boarded up. Make the plane face outwards, so you see the white side, not the transparent side, when looking at the model.

If you want to add more windows, just create more planes and name them "BoardedWindow2", etc. And that's it. When it loads the models the game will make these planes invisible and use their positions to show the boarded window effect, if the building is taken over.
36 Comments
⚡Electro Javaboy⚡ 30 Jan @ 1:33am 
"I'm using Unity 6000.0.14f1. (I'm not sure if it matters if you use a different version)"

In my case, when making prefabs using that version, the standard material doesn't get loaded?
But for old version like (2023.1.7f1) the materials loads just fine, or at least i don't see any problem with it.
Jackerino 13 Nov, 2023 @ 9:11pm 
er... what's stopping you from making a new Unity Account?

Also, if your actions violated ToS, then that results in your Unity Privileges being revoked. If you truly did nothing wrong, they provide a handy Appeal Service, documented here:

https://support.unity.com/hc/en-us/articles/14315075759636-How-do-I-appeal-if-my-organization-is-blocked-for-a-violation-of-Unity-s-Terms-of-Service-
Uncle Piggy 13 Nov, 2023 @ 6:40pm 
Bob,I got problemwith unity since my account has been blocked over 4 yrs for some reason ,and I currently can't do anything about it ,is there any alternative way to do this ?
ribomat 7 Jul, 2023 @ 4:09am 
Hi, I wanted to congratulate you on the game you created. I'm using this guide to create some new objects and I was wondering if it's possible to edit existing objects. For example, I modified the watchtower to accommodate two people, I managed to position them well inside, but I would like to resize the watchtower to make a little more space. Thank you in advance.
2370329962 30 Sep, 2022 @ 6:17pm 
Hi Bob.

What about replacing the textures of the origin mods ?

Is it possible ?
Lemski07 19 Sep, 2021 @ 5:32pm 
mine showed up after reloading after editing in game f8 and using the older version of both blender and unity that they recommended.
Kerrigor 17 Sep, 2021 @ 5:08am 
@TGxRedPlayer OHHh ok.... yes it was the save and reload thing XD. tnx
TGxRedPlayer 17 Sep, 2021 @ 4:38am 
@Kerrigor did you set the model in the equipped model mesh? once you do that you have to start any story with the mod you made, open f8 and go to the equipped model editor, set the animation (if you're making a pistol chose pistol) click save and exit the game completely then reopen, if everything is alright with the model then you should see it once you enter the story where you were testing your mod again (or at least that's how I do i t since my models are also invisible until I chose an animation and save/reopen lol)
If that doesn't work then idk what the problem could be, I use Unity 2020.1.2f1 and Blender 2.78, 2.82 and 2.93 depending on the model so I doubt Blender has something to do with the problem
Hope I could help haha
Kerrigor 17 Sep, 2021 @ 4:17am 
@Lemski07 did it work for you? i tried unity 2020.1.10.F1 and is still does not show up in my game.Did you try blender? I will try downgrading blender next.
Lemski07 23 Aug, 2021 @ 2:48am 
@SlyckBoi I'll try that. I'm using the latest one.