03 Building

This example scene is located in Assets/Plugins/Weaver/Examples/03 Building. It contains a simple destructible building to be used by the later Missile Command example and demonstrates how you can create a Procedural Asset.

  • Building is a partial class which is split into multiple files. The file in this example contains the definition of the procedural asset while the code to destroy the buildings at runtime for the Missile Command example is located over in that folder because it isn't relevant to this example.
  • The ProceduralOfficeBuilding field uses a regular [AssetReference] attribute similar to the Frame Rate example, except:
    • It also has a [ProceduralAsset] attribute because we want to generate it using code. It can still be assigned manually, but it will be shown in the Procedural Assets Panel with a [G] button to generate it and overwrite whatever existing asset was assigned (instead of being shown in the regular Injectors Panel).
    • By default, it will find a method called "GenerateFieldName" to use as the generator method (in this case, that's GenerateProceduralOfficeBuilding), but you can name a method of your choice by setting the optional Generator property.
    • The EditorOnly property is set to true because we don't actually need this asset reference to be injected at runtime. We just want to generate the prefab in the Unity Editor, then use it in a scene like a regular prefab.
  • The GenerateProceduralOfficeBuilding method is used to generate the prefab.
    • All it needs to do is create, configure, and return the desired object, then Weaver will take care of saving it. In this case, it creates a new GameObject and adds various components but you can make practically any type of asset procedurally, including textures, meshes, animator controllers, and scriptable objects.
    • The building uses a Procedural Mesh created using the MeshBuilder class. That mesh could have been saved as a separate asset by making another field with its own [AssetReference] and [ProceduralAsset] attributes, but in this case the mesh is only going to be used by this one prefab, so it might as well be saved inside that prefab.
    • The same is true for the two Materials which are assigned to the MeshRenderer. Weaver automatically detects that they aren't saved anywhere else so it will save them inside the prefab asset itself.
    • Also note how the mesh uses two sub-meshes. The MeshBuilder constructor takes a vertexCapacity followed by two indexCapacities which causes it to allocate index lists for two sub-meshes - [0] for the base of the building and [1] for the windows. This allows the MeshRenderer to render each sub-mesh with its own Material to give them each a different color.
  • The name of the prefab follows one of the Asset Naming rules to automatically match up with the script name when you first import Weaver.
    • When first generated from scratch, the asset would saved in the Output Directory assigned in the Procedural Assets Panel and would be named according to its field name (Assets/Procedural Assets/Building.ProceduralOfficeBuilding.prefab). But after that it can be renamed or moved anywhere in the project and will overwrite the same asset every time it is regenerated.