Snippets

From Cities: Skylines Wiki
Jump to navigation Jump to search

Basic[edit]

Mod Setup Skeleton

Create the following file inside Source folder of your mod, this snippet will just print a string when a level is loaded:

using ICities;
using UnityEngine;

namespace CustomModNameSpace
{
	#region Mod Definition
	/// <summary>
	/// Provide description of the mod to the game, this is shown during mod loading screen.
	/// </summary>
	public class CustomMod : IUserMod
	{

		public string Name
		{
			get { return "My mod name"; }
		}

		public string Description
		{
			get { return "Here is where I define my mod"; }
		}
	}
	#endregion

	#region Mod Behavior
	/// <summary>
	/// Here we are creating a custom ILoadingExtension; 
	/// LoadingExtensionBase implemented ILoadingExtension and provides some default behavior so we are inheriting from that.
	/// </summary>
	public class CustomLoader: LoadingExtensionBase
	{
		/// <summary>
		/// This event is triggerred when a level is loaded
		/// </summary>
		public override void OnLevelLoaded(LoadMode mode)
		{
			// Instantiate a custom object
			GameObject go = new GameObject("Test Object");
			go.AddComponent<CustomComponent>();

			base.OnLevelLoaded(mode);
		}
	}
	#endregion

	#region Custom Game Object Components
	/// <summary>
	/// Here we creating a custom game object that directly utilize Unity Game Engine;
	/// See https://docs.unity3d.com/Manual/CreatingAndUsingScripts.html for more detail.
	/// </summary>
	public class CustomComponent: MonoBehaviour
	{
		/// <summary>
		/// This event is triggered when this object is created
		/// </summary>
		void Start()
		{
			DebugOutputPanel.AddMessage(ColossalFramework.Plugins.PluginManager.MessageType.Message, "Hello World 2!");
		}

		/// <summary>
		/// This event is triggered every frame, we can use this to add some animation etc.
		/// </summary>
		void Update()
		{

		}
	}
	#endregion
}

This is what the mod folder look like:

C:\Users\<username>\AppData\Local\Colossal Order\Cities_Skylines\Addons\Mods\CustomMod
│  App.config
│  CustomMod.csproj
│  CustomMod.sln
├─bin
│  ├─Debug
│  └─Release
├─obj
│  └─Debug
│      │  DesignTimeResolveAssemblyReferencesInput.cache
│      └─TempPE
├─Properties
│      AssemblyInfo.cs
└─Source
        ModSkeleton.cs

Some screenshots for the effect: Mod in Content Manager, Mod after a level is loaded

Camera[edit]

/// <summary>
/// Move the camera to a building. Also shows how to create an InstanceId from scratch.
/// </summary>
/// <param name="index">The index of the building in BuildingManager.instance.m_buildings.m_buffer</param>
static void MoveCameraToBuilding( ushort index )
{
    var instanceID = default(InstanceID);
    instanceID.Building = index; 
    MoveCameraToInstance( instanceID );
}
/// <summary>
/// Move the camera to something with an instance id
/// </summary>
/// <param name="instanceID"></param>
static void MoveCameraToInstance( InstanceID instanceID )
{
	ToolsModifierControl.cameraController.SetTarget( instanceID, ToolsModifierControl.cameraController.transform.position, true );
}

How-To (Work-In-Progress)[edit]

It might be helpful if we collect some snippets relevant to those areas to better demonstrate the capability of C# based API:

1. How to import/export street data; 2. How to dynamically create/destroy buildings.