Unity RTS Engine

  1. Home
  2. Unity RTS Engine
  3. Scripting
  4. Useful Scripting Practices

Useful Scripting Practices

Creating Units

You can create a unit using the public static method CreateUnit from the UnitManager class:

Parameters:

  1. unitPrefab: The main unit component (Unit) of the unit prefab to spawn
  2. spawnPosition: Vector3 that specifies where will the unit will be spawned
  3. factionID: Integer that specifies which faction the unit belongs to
  4. createdBy: The main building component (Building) of the building that will be marked as the creator of the unit. When set to null, no building creator will be set for the unit.
  5. freeUnit: Optional field (default value: false). When set to true, the unit will be a free unit (will not belong to any faction).

Creating Buildings

You can create a placed building using the public static method CreatePlacedInstance from the BuildingManager class:

Parameters:

  1. buildingPrefab: The main building component (Building) of the building prefab to spawn.
  2. placementPosition: Vector3 that specifies where the building will be placed.
  3. buildingCenter: The Border component of the building that will be set as the center of the building to create. When set to null, no building center will be set for the new building.
  4. factionID: Integer that specifies which faction the building belongs to.
  5. placedByDefault: Optional field (default value = false). When set to true, the building will be fully constructed when created. When set to false, builders will have to construct this building before it becomes fully functional.

Creating Resources:

You can create a resource using the public static method CreateResource from the ResourceManager class:

Parameters:

  1. resourcePrefab: The main resource component (Resource) of the resource prefab to spawn.
  2. spawnPosition: Vector3 that specifies where the resource will be created at.

Spawning Effect Objects:

You can spawn an effect object using public static method SpawnEffectObj from the EffectObjPool class:

Parameters:

  1. prefab: The effect object prefab (that has the EffectObj component attached to it) that you want to spawn.
  2. spawnPosition: Vector3 that specifies where the effect object will be spawned at.
  3. spawnRotation: Quaternion that specifies the rotation of the effect object when it spawns.
  4. parent (optional): If you’d like the effect object to spawn a child object, then assigned the Transform of the parent here, else leave this set to null.
  5. enableLifeTime (optional): Set to true if you’d like to enable life time as soon as the effect object spawns, else if you’d like to control disabling the effect object from your own components (using the Disable() method from the EffectObj class) then set to false.
  6. autoLifeTime (optional): Only valid if enableLifeTime is true. Set to true if you’d like to use the default life time assigned in the EffectObj component for the instance of this effect object. Set false otherwise.
  7. lifeTime (optional): Only valid if enableLifeTime is true & autoLifeTime is false. Set to the custom duration (in seconds) of the effect object life time.

How can we help?