How would I go about making a character customiser for my 2d game?

I had an idea of instantiating different prefabs for different customisations but this doesn’t seem efficient. How should I go about doing this?

Best Answer

  • iscottnoidea
    Answer ✓
    Mine is pretty sloppy and could be refined, but here's how it works, hope this helps:

    I have a gameobject with all cosmetics as children and active toggled to false. I have a cosmetic selection before a game that lets the player save them before the game using playerprefs to store the selected cosmetics names as strings. The cosmetics are based on categories (head, face, hands etc)

    Once the game starts and the character is instantiated, it will check for these using this an array for each category to reference the playerpref, then send an RPC to all other players to update it.

    the array
        public string[] cosmetics = new string[7] { "HAIR", "MASK", "CHEST", "BACK", "HANDS", "LEGS", "FEET" };
    

    The code on Instantiation of the player to set cosmetics active
                foreach (string s in cosmetics)
                {
                     if (PlayerPrefs.GetString(s, "None") != "None")
                     { 
                         cosmeticObject = this.transform.Find(PlayerPrefs.GetString(s)).gameObject;
                         cosmeticObject.SetActive(true);
                         photonView.RPC("AddCosmetic", RpcTarget.Others, PlayerPrefs.GetString(s, "None"));                   
                     }
                }
    

    The RPC to make others set it as active
    [PunRPC]
        void AddCosmetic(string name)
        {
            if (!photonView.IsMine)
            {
                GameObject currentObject = this.transform.Find(name).gameObject;
                if (currentObject != null)
                {
                    currentObject.gameObject.SetActive(true);
                }
            }
        }
    

    Happy to chat on Discord or Twitter if you want to see the cosmetic selection screen too. Just send me a message.

Answers

  • iscottnoidea
    Answer ✓
    Mine is pretty sloppy and could be refined, but here's how it works, hope this helps:

    I have a gameobject with all cosmetics as children and active toggled to false. I have a cosmetic selection before a game that lets the player save them before the game using playerprefs to store the selected cosmetics names as strings. The cosmetics are based on categories (head, face, hands etc)

    Once the game starts and the character is instantiated, it will check for these using this an array for each category to reference the playerpref, then send an RPC to all other players to update it.

    the array
        public string[] cosmetics = new string[7] { "HAIR", "MASK", "CHEST", "BACK", "HANDS", "LEGS", "FEET" };
    

    The code on Instantiation of the player to set cosmetics active
                foreach (string s in cosmetics)
                {
                     if (PlayerPrefs.GetString(s, "None") != "None")
                     { 
                         cosmeticObject = this.transform.Find(PlayerPrefs.GetString(s)).gameObject;
                         cosmeticObject.SetActive(true);
                         photonView.RPC("AddCosmetic", RpcTarget.Others, PlayerPrefs.GetString(s, "None"));                   
                     }
                }
    

    The RPC to make others set it as active
    [PunRPC]
        void AddCosmetic(string name)
        {
            if (!photonView.IsMine)
            {
                GameObject currentObject = this.transform.Find(name).gameObject;
                if (currentObject != null)
                {
                    currentObject.gameObject.SetActive(true);
                }
            }
        }
    

    Happy to chat on Discord or Twitter if you want to see the cosmetic selection screen too. Just send me a message.
  • @iscottnoidea thanks for the help! I will message you if I need any further help!