mestiez / ppg-snippets

a number of useful code snippets that should help users write People Playground mods
https://www.studiominus.nl/ppg-modding/
53 stars 113 forks source link

how to add a category #17

Open An4r3w opened 2 years ago

An4r3w commented 2 years ago

how can i add a category for all my custom humans guns and objects?

R0Y1T0 commented 2 years ago
using System;
using System.Linq;
using System.Collections.Generic;
using UnityEngine;

public class CategoryBuilder
{
    /// <summary>
    /// Use this method to create your own category.
    /// </summary>
    /// <param name="name">New category name</param>
    /// <param name="description">Description of the new category</param>
    /// <param name="icon">New category icon</param>
    public static void Create(string name, string description, Sprite icon)
    {
        CatalogBehaviour manager = UnityEngine.Object.FindObjectOfType<CatalogBehaviour>();
        if (manager.Catalog.Categories.FirstOrDefault((Category c) => c.name == name) == null)
        {
            Category category = ScriptableObject.CreateInstance<Category>();
            category.name = name;
            category.Description = description;
            category.Icon = icon;
            Category[] NewCategories = new Category[manager.Catalog.Categories.Length + 1];
            Category[] categories = manager.Catalog.Categories;
            for (int i = 0; i < categories.Length; i++)
            {
                NewCategories[i] = categories[i];
            }
            NewCategories[NewCategories.Length - 1] = category;
            manager.Catalog.Categories = NewCategories;
        }
    }
}

copy and paste all of the first code into a clean .cs file and call it CategoryBuilder

CategoryBuilder.Create("NameOfCategory", "DescriptionOfCategory", ModAPI.LoadSprite("ImageOfCategory.png")); create another .cs file and call it script.cs, then you use the basic entry point code and paste this line inside Main.

Make sure your mod.json file have this line below Scripts "CategoryBuilder.cs",

If you want to add custom content to the category just find the category u created CategoryOverride = ModAPI.FindCategory("CustomCategorynamegoeshere"),

An4r3w commented 2 years ago

Hello, first of all thank you so much. I have another question tho, what size must be the category icon? Can i add color to it?