模组开发基础
阅读
2023-11-30更新
最新编辑:Lu_23333
阅读:
更新日期:2023-11-30
最新编辑:Lu_23333
After you've setup your basic or advanced mod it's time to actually start making a mod. This page will cover how to get started and all the basics of modding.
Getting Started
The first thing you do to create a mod is to create a class that implements IUserMod
.
This is the start point for any mod and by implementing IUserMod
the game will be able to load your mod.
Namespace
Your mod should always be within a namespace. The name of the namespace doesn't matter that much but it's recommended to just use the name of your mod. This will prevent conflicts with other mods and it will keep your projects more organized by scoping your code within your namespace.
Using directives
To properly use the ICities API and other assemblies you have to add the using directives. In most cases you'll be using ICities and UnityEngine so start of by adding these lines in your code.
using ICities; using UnityEngine;
IUserMod
By implementing IUserMod
you have to specify a Name
and Description
for your mod.
These are used within the Content Manager to display your mod.
The initial code for your mod should then look something like this and you should be able to load it in the content manager.
using ICities; using UnityEngine; namespace ModName { public class ModName: IUserMod { public string Name { get { return "Mod Name"; } } public string Description { get { return "My first awesome mod that doesn't do anything! *JEEY*"; } } } }
Modding Basics
Now that you have a working mod it's time to make it actually do something. This differs for each mod obviously but here are some things you should know to get started. There are millions of possibilities what your mod can do so after this you'll be on your own to figure out how to convert your ideas into an actual mod. There are still a lot of other guides you can read to learn how to do specific things.
Unity
The game is built in Unity which means you have access to all the functionality Unity offers in both basic and advanced mods. For example you could create game objects and monobehaviours, render UI elements, use physics and a bunch of other things. Check out the Unity Manual or the Unity API to learn how Unity works and to see what you can do.
Basic mods
For basic mods you'll mostly be implementing interfaces and extending the base classes.
Example
If you add this class to your mod you get unlimited oil and ore resources. Classes can be added in your main file or by creating a new file.
public class UnlimitedOilAndOreResource : ResourceExtensionBase { public override void OnAfterResourcesModified(int x, int z, NaturalResource type, int amount) { if ((type == NaturalResource.Oil || type == NaturalResource.Ore) && amount < 0) { resourceManager.SetResource(x, z, type, (byte)(resourceManager.GetResource(x, z, type) - amount), false); } } }
If we break this down you can see that we create a new class that extends ResourceExtensionBase
.
By doing this we can override an API method OnAfterResourcesModified
which gets executed when a natural resource is modified.
We then do some checks to make sure we're only overriding oil and ore.
The last thing it does is actually modify the resource by using the resourceManager
.
There are a lot of managers that can be used to modify things and get values so you'll mostly be using managers and calling their methods.
Managers
All interfaces/base classes have a OnCreated method which provides you with the related manager.
For example if you extend LoadingExtensionBase you can override the OnCreated
method and you'll get the ILoading
manager.
Each manager interface also has a reference to all other managers.
public class LoadingExample : LoadingExtensionBase { public override void OnCreated(ILoading loading) { loading.managers.milestones.UnlockMilestone("Basic Road Created"); } }
As you can see here we get a reference to the ILoading manager but then we access all managers and are able to access the milestones manager. We can then unlock the 'Basic Road Created' milestone when a level is loaded. For more information see the managers info on the modding page.
Modding API
The next step is to look at the Modding API and see what you can do. It describes all the classes and interfaces within the ICities assembly.
Advanced mods
Apart from the things you can do in basic mods you have a lot more possibilities. There are a lot of guides explaining how to do specific things so make sure to take a look at them. You'll mostly be Reverse Engineering the CS code to figure out what exactly you can do. By using Reflection you can go even further but this is for more advanced programmers only.
Singletons
There are a lot of singletons in the game for managers and other things. This will be the starting point for most things you do to modify the game. For example to simply pause the game you could do this.
Singleton<SimulationManager>.instance.SimulationPaused = true;
Most Manager instances can also be referenced using the static instance property, which wraps the Singleton, the above example becomes :
SimulationManager.instance.SimulationPaused = true;