コード例 #1
0
ファイル: LoadoutAPI.cs プロジェクト: IBurn36360/R2API
        /// <summary>
        /// Creates a new SkinDef from a SkinDefInfo.
        /// Note that this prevents null-refs by disabling SkinDef awake while the SkinDef is being created.
        /// The things that occur during awake are performed when first applied to a character instead.
        /// </summary>
        /// <param name="skin"></param>
        /// <returns></returns>
        public static SkinDef CreateNewSkinDef(SkinDefInfo skin)
        {
            if (!IsLoaded)
            {
                R2API.Logger.LogError("LoadoutAPI is not loaded. Please use [R2API.Utils.SubModuleDependency]");
                return(null);
            }
            On.RoR2.SkinDef.Awake += DoNothing;

            SkinDef newSkin = ScriptableObject.CreateInstance <SkinDef>();

            newSkin.baseSkins             = skin.baseSkins;
            newSkin.icon                  = skin.icon;
            newSkin.unlockableName        = skin.unlockableName;
            newSkin.rootObject            = skin.rootObject;
            newSkin.rendererInfos         = skin.rendererInfos;
            newSkin.gameObjectActivations = skin.gameObjectActivations;
            newSkin.meshReplacements      = skin.meshReplacements;
            newSkin.nameToken             = skin.nameToken;
            newSkin.name                  = skin.name;

            On.RoR2.SkinDef.Awake -= DoNothing;

            addedSkins.Add(newSkin);
            return(newSkin);
        }
コード例 #2
0
        /// <summary>
        /// Creates a new SkinDef from a SkinDefInfo.
        /// Note that this prevents null-refs by disabling SkinDef awake while the SkinDef is being created.
        /// The things that occur during awake are performed when first applied to a character instead.
        /// </summary>
        /// <param name="skin"></param>
        /// <returns></returns>
        public static SkinDef CreateNewSkinDef(SkinDefInfo skin)
        {
            On.RoR2.SkinDef.Awake += DoNothing;

            var newSkin = ScriptableObject.CreateInstance <SkinDef>();

            newSkin.baseSkins      = skin.BaseSkins;
            newSkin.icon           = skin.Icon;
            newSkin.unlockableDef  = skin.UnlockableDef;
            newSkin.unlockableName = skin.UnlockableName;

            newSkin.rootObject                  = skin.RootObject;
            newSkin.rendererInfos               = skin.RendererInfos;
            newSkin.gameObjectActivations       = skin.GameObjectActivations;
            newSkin.meshReplacements            = skin.MeshReplacements;
            newSkin.projectileGhostReplacements = skin.ProjectileGhostReplacements;
            newSkin.minionSkinReplacements      = skin.MinionSkinReplacements;
            newSkin.nameToken = skin.NameToken;
            newSkin.name      = skin.Name;

            On.RoR2.SkinDef.Awake -= DoNothing;

            AddedSkins.Add(newSkin);
            return(newSkin);
        }
コード例 #3
0
        /// <summary>
        /// Creates a new SkinDef from a SkinDefInfo.
        /// Note that this prevents null-refs by disabling SkinDef awake while the SkinDef is being created.
        /// The things that occur during awake are performed when first applied to a character instead.
        /// </summary>
        /// <param name="skin"></param>
        /// <returns></returns>
        public static SkinDef CreateNewSkinDef(SkinDefInfo skin)
        {
            On.RoR2.SkinDef.Awake += DoNothing;

            SkinDef newSkin = ScriptableObject.CreateInstance <SkinDef>();

            newSkin.baseSkins      = skin.baseSkins;
            newSkin.icon           = skin.icon;
            newSkin.unlockableName = skin.unlockableName;
            newSkin.rootObject     = skin.rootObject;
            newSkin.rendererInfos  = skin.rendererInfos;
            newSkin.nameToken      = skin.nameToken;
            newSkin.name           = skin.name;

            On.RoR2.SkinDef.Awake -= DoNothing;
            return(newSkin);
        }
コード例 #4
0
        public static SkinDef CreateSkinDef(string skinName, Sprite skinIcon, CharacterModel.RendererInfo[] rendererInfos, SkinnedMeshRenderer mainRenderer, GameObject root, UnlockableDef unlockableDef, Mesh skinMesh)
        {
            SkinDefInfo skinDefInfo = new SkinDefInfo
            {
                BaseSkins             = Array.Empty <SkinDef>(),
                GameObjectActivations = new SkinDef.GameObjectActivation[0],
                Icon             = skinIcon,
                MeshReplacements = new SkinDef.MeshReplacement[]
                {
                    new SkinDef.MeshReplacement
                    {
                        renderer = mainRenderer,
                        mesh     = skinMesh
                    }
                },
                MinionSkinReplacements = new SkinDef.MinionSkinReplacement[0],
                Name      = skinName,
                NameToken = skinName,
                ProjectileGhostReplacements = new SkinDef.ProjectileGhostReplacement[0],
                RendererInfos = rendererInfos,
                RootObject    = root,
                UnlockableDef = unlockableDef
            };

            On.RoR2.SkinDef.Awake += DoNothing;

            SkinDef skinDef = ScriptableObject.CreateInstance <RoR2.SkinDef>();

            skinDef.baseSkins                   = skinDefInfo.BaseSkins;
            skinDef.icon                        = skinDefInfo.Icon;
            skinDef.unlockableDef               = skinDefInfo.UnlockableDef;
            skinDef.rootObject                  = skinDefInfo.RootObject;
            skinDef.rendererInfos               = skinDefInfo.RendererInfos;
            skinDef.gameObjectActivations       = skinDefInfo.GameObjectActivations;
            skinDef.meshReplacements            = skinDefInfo.MeshReplacements;
            skinDef.projectileGhostReplacements = skinDefInfo.ProjectileGhostReplacements;
            skinDef.minionSkinReplacements      = skinDefInfo.MinionSkinReplacements;
            skinDef.nameToken                   = skinDefInfo.NameToken;
            skinDef.name                        = skinDefInfo.Name;

            On.RoR2.SkinDef.Awake -= DoNothing;

            return(skinDef);
        }
コード例 #5
0
ファイル: LoadoutAPI.cs プロジェクト: IBurn36360/R2API
        /// <summary>
        /// Adds a skin to the body prefab for a character.
        /// Will attempt to create a default skin if one is not present.
        /// Must be called during plugin Awake or OnEnable. If called afterwards the new skins must be added to bodycatalog manually.
        /// </summary>
        /// <param name="bodyPrefab">The body to add the skin to</param>
        /// <param name="skin">The SkinDef to add</param>
        /// <returns>True if successful</returns>
        public static Boolean AddSkinToCharacter(GameObject bodyPrefab, SkinDef skin)
        {
            if (bodyPrefab == null)
            {
                R2API.Logger.LogError("Tried to add skin to null body prefab.");
                return(false);
            }

            if (skin == null)
            {
                R2API.Logger.LogError("Tried to add invalid skin.");
                return(false);
            }
            addedSkins.Add(skin);

            var modelLocator = bodyPrefab.GetComponent <ModelLocator>();

            if (modelLocator == null)
            {
                R2API.Logger.LogError("Tried to add skin to invalid body prefab (No ModelLocator).");
                return(false);
            }

            var model = modelLocator.modelTransform;

            if (model == null)
            {
                R2API.Logger.LogError("Tried to add skin to body prefab with no modelTransform.");
                return(false);
            }

            if (skin.rootObject != model.gameObject)
            {
                R2API.Logger.LogError("Tried to add skin with improper root object set.");
                return(false);
            }

            var modelSkins = model.GetComponent <ModelSkinController>();

            if (modelSkins == null)
            {
                R2API.Logger.LogWarning(bodyPrefab.name + " does not have a modelSkinController.\nAdding a new one and attempting to populate the default skin.\nHighly recommended you set the controller up manually.");
                var charModel = model.GetComponent <CharacterModel>();
                if (charModel == null)
                {
                    R2API.Logger.LogError("Unable to locate CharacterModel, default skin creation aborted.");
                    return(false);
                }

                var skinnedRenderer = charModel.GetFieldValue <SkinnedMeshRenderer>("mainSkinnedMeshRenderer");
                if (skinnedRenderer == null)
                {
                    R2API.Logger.LogError("CharacterModel did not contain a main SkinnedMeshRenderer, default skin creation aborted.");
                    return(false);
                }

                var baseRenderInfos = charModel.baseRendererInfos;
                if (baseRenderInfos == null || baseRenderInfos.Length == 0)
                {
                    R2API.Logger.LogError("CharacterModel rendererInfos are invalid, default skin creation aborted.");
                    return(false);
                }

                modelSkins = model.gameObject.AddComponent <ModelSkinController>();

                var skinDefInfo = new SkinDefInfo {
                    baseSkins             = Array.Empty <SkinDef>(),
                    gameObjectActivations = Array.Empty <SkinDef.GameObjectActivation>(),
                    icon             = CreateDefaultSkinIcon(),
                    name             = "skin" + bodyPrefab.name + "Default",
                    nameToken        = bodyPrefab.name.ToUpper() + "_DEFAULT_SKIN_NAME",
                    rootObject       = model.gameObject,
                    unlockableName   = "",
                    meshReplacements = new SkinDef.MeshReplacement[1]
                    {
                        new SkinDef.MeshReplacement {
                            renderer = skinnedRenderer,
                            mesh     = skinnedRenderer.sharedMesh
                        }
                    },
                    rendererInfos = charModel.baseRendererInfos
                };

                var defaultSkinDef = CreateNewSkinDef(skinDefInfo);

                modelSkins.skins = new SkinDef[1] {
                    defaultSkinDef
                };
            }

            var skinsArray = modelSkins.skins;
            var index      = skinsArray.Length;

            Array.Resize <SkinDef>(ref skinsArray, index + 1);
            skinsArray[index] = skin;
            modelSkins.skins  = skinsArray;
            return(true);
        }
コード例 #6
0
ファイル: LoadoutAPI.cs プロジェクト: IBurn36360/R2API
        /// <summary>
        /// Adds a skin to the body prefab for a character.
        /// Will attempt to create a default skin if one is not present.
        /// Must be called during plugin Awake or OnEnable. If called afterwards the new skins must be added to bodycatalog manually.
        /// </summary>
        /// <param name="bodyPrefab">The body to add the skin to</param>
        /// <param name="skin">The SkinDefInfo for the skin to add</param>
        /// <returns>True if successful</returns>
        public static Boolean AddSkinToCharacter(GameObject bodyPrefab, SkinDefInfo skin)
        {
            var skinDef = CreateNewSkinDef(skin);

            return(AddSkinToCharacter(bodyPrefab, skinDef));
        }
コード例 #7
0
        /// <summary>
        /// Adds a skin to the body prefab for a character.
        /// Will attempt to create a default skin if one is not present.
        /// Must be called during plugin Awake or OnEnable. If called afterwards the new skins must be added to bodycatalog manually.
        /// </summary>
        /// <param name="bodyPrefab">The body to add the skin to</param>
        /// <param name="skin">The SkinDef to add</param>
        /// <returns>True if successful</returns>
        public static bool AddSkinToCharacter(GameObject bodyPrefab, SkinDef skin)
        {
            if (bodyPrefab == null)
            {
                LogCore.LogE("Tried to add skin to null body prefab.");
                return(false);
            }

            if (skin == null)
            {
                LogCore.LogE("Tried to add invalid skin.");
                return(false);
            }
            AddedSkins.Add(skin);

            var modelLocator = bodyPrefab.GetComponent <ModelLocator>();

            if (modelLocator == null)
            {
                LogCore.LogE("Tried to add skin to invalid body prefab (No ModelLocator).");
                return(false);
            }

            var model = modelLocator.modelTransform;

            if (model == null)
            {
                LogCore.LogE("Tried to add skin to body prefab with no modelTransform.");
                return(false);
            }

            if (skin.rootObject != model.gameObject)
            {
                LogCore.LogE("Tried to add skin with improper root object set.");
                return(false);
            }

            var modelSkins = model.GetComponent <ModelSkinController>();

            if (modelSkins == null)
            {
                LogCore.LogW(bodyPrefab.name + " does not have a modelSkinController.\nAdding a new one and attempting to populate the default skin.\nHighly recommended you set the controller up manually.");
                var charModel = model.GetComponent <CharacterModel>();
                if (charModel == null)
                {
                    LogCore.LogE("Unable to locate CharacterModel, default skin creation aborted.");
                    return(false);
                }

                var skinnedRenderer = charModel.GetFieldValue <SkinnedMeshRenderer>("mainSkinnedMeshRenderer");
                if (skinnedRenderer == null)
                {
                    LogCore.LogE("CharacterModel did not contain a main SkinnedMeshRenderer, default skin creation aborted.");
                    return(false);
                }

                var baseRenderInfos = charModel.baseRendererInfos;
                if (baseRenderInfos == null || baseRenderInfos.Length == 0)
                {
                    LogCore.LogE("CharacterModel rendererInfos are invalid, default skin creation aborted.");
                    return(false);
                }

                modelSkins = model.gameObject.AddComponent <ModelSkinController>();

                var skinDefInfo = new SkinDefInfo
                {
                    BaseSkins             = Array.Empty <SkinDef>(),
                    GameObjectActivations = Array.Empty <SkinDef.GameObjectActivation>(),
                    Icon             = CreateDefaultSkinIcon(),
                    Name             = "skin" + bodyPrefab.name + "Default",
                    NameToken        = bodyPrefab.name.ToUpper() + "_DEFAULT_SKIN_NAME",
                    RootObject       = model.gameObject,
                    UnlockableName   = "",
                    UnlockableDef    = null,
                    MeshReplacements = new[]
                    {
                        new SkinDef.MeshReplacement {
                            renderer = skinnedRenderer,
                            mesh     = skinnedRenderer.sharedMesh
                        }
                    },
                    RendererInfos = charModel.baseRendererInfos,
                    ProjectileGhostReplacements = Array.Empty <SkinDef.ProjectileGhostReplacement>(),
                    MinionSkinReplacements      = Array.Empty <SkinDef.MinionSkinReplacement>()
                };

                var defaultSkinDef = CreateNewSkinDef(skinDefInfo);

                modelSkins.skins = new[] {
                    defaultSkinDef
                };
            }

            //Array.Resize(ref modelSkins.skins, modelSkins.skins.Length + 1);
            //modelSkins.skins[modelSkins.skins.Length - 1] = skin;

            Array.Resize(ref modelSkins.skins, modelSkins.skins.Length + 1);
            modelSkins.skins[modelSkins.skins.Length - 1] = skin;
            return(true);
        }