The AddEntityToGameObject lets us load custom entity's classes from the Mods assembly. It uses a Postfix() to catch the return value of the original method.
If the original method returns null, that means it could not find our custom class. When that happens, we want to check in our custom Mods.dll to see if it's there.
If it's not null, meaning it found the custom class, then it'll simple return the original return type.
using DMT; using Harmony; using System; using System.Reflection; using UnityEngine; [HarmonyPatch(typeof(EntityFactory))] [HarmonyPatch("addEntityToGameObject")] public class SphereII_EntityFactoryPatch : IHarmony { public void Start() { Debug.Log(" Loading Patch: " + this.GetType().ToString()); var harmony = HarmonyInstance.Create(GetType().ToString()); harmony.PatchAll(Assembly.GetExecutingAssembly()); } // Using a Postfix method here. We want to catch the results of the original method using Entity __result, and also all the parameters that go into the orignal method, as // we'll need them all. static Entity Postfix(Entity __result, GameObject _gameObject, string _className) { if(__result == null) { Type type = Type.GetType(_className + ", Mods"); if(type != null) return (Entity)_gameObject.AddComponent(type); } return __result; } } |
Created with the Personal Edition of HelpNDoc: Free EPub and documentation generator