Skin System

Hello people, today We will speak about a thing many use at these days. We will speak about Skin System.

A skin system is responsible for to change the character appearance, He changes the clothes, armor, weapons, physic appearance, and others things.

So Let’s go.

The first step that We to make is to create our button, and set his in our variable.

public class Skin_Options : MonoBehaviour
{


       GameObject _Button;

       void Start(){

	    _Button = this.gameObject;

       }

}

After this, We need to create the method that will configure the button, and more some things, as a INT value, for define our skin, We will use this for send, for our Skin_Manager, He go to manage the character skin.

Here is our code Skin_Options Completed.

public class Skin_Options : MonoBehaviour
{

	public static int _Skin_Value;

	public Skin_Manager _Skin_Manager_Script;

	GameObject _Button;

	void Start()
	{

		_Button = this.gameObject;
		_Skin_Manager_Script = GameObject.Find("Scene_Controller").GetComponent();

	}

	public void Change_Skin()
	{

		switch (_Button.name)
		{

			case "Skin_0":
				_Skin_Value = 0;
				break;

			case "Skin_1":
				_Skin_Value = 1;
				break;

			case "Skin_2":
				_Skin_Value = 2;
				break;

			case "Skin_3":
				_Skin_Value = 3;
				break;

			case "Skin_4":
				_Skin_Value = 4;
				break;

		}

		_Skin_Manager_Script.Set_Skin();

	}

}

Now, with this code completed, We go to create our Skin_Manager

Now We will to create more some things in our code, as the character parts that will to change, and also our content that will to replace the default skin.

In my case as I don’t have visual content, I will to change the color by my character, but the code will have both methods, You can select the better for you.

We have, the SPRITERENDERER variables, that are the parts of our character that will to change, and We have the LISTS as type SPRITE that are our skin’s content, and at this specific case, We have, the Color’s array, for to change, the color in our character, because as already said, the project don’t have visual content.

This is our code.

public class Skin_Manager : MonoBehaviour
{

	public SpriteRenderer _Head_Renderer;
	public SpriteRenderer _Body_Renderer;

	public SpriteRenderer _Left_Up_Arm_Renderer;
	public SpriteRenderer _Right_Up_Arm_Renderer;

	public SpriteRenderer _Left_Bottom_Arm_Renderer;
	public SpriteRenderer _Right_Bottom_Arm_Renderer;

	public SpriteRenderer _Left_Leg_Renderer;
	public SpriteRenderer _Right_Leg_Renderer;

	public List _Head_Sprites;
	public List _Body_Sprites;

	public List _Left_Up_Arm_Sprites;
	public List _Right_Up_Arm_Sprites;

	public List _Left_Bottom_Arm_Sprites;
	public List _Right_Bottom_Arm_Sprites;

	public List _Left_Leg_Sprites;
	public List _Right_Leg_Sprites;

	public Color[] _Skin_Color;

}

Now We will change our character, and save the choice, We also let’s go to see, if already did some change in our character previously, I will make this using playerprefs, but don’t worry, I will talk about this in other article, that will to stay in SAVING DATA section, for this, We need a method that will seted in our Buttons.

So here is our refreshed code:

public class Skin_Manager : MonoBehaviour
{

	public SpriteRenderer _Head_Renderer;
	public SpriteRenderer _Body_Renderer;

	public SpriteRenderer _Left_Up_Arm_Renderer;
	public SpriteRenderer _Right_Up_Arm_Renderer;

	public SpriteRenderer _Left_Bottom_Arm_Renderer;
	public SpriteRenderer _Right_Bottom_Arm_Renderer;

	public SpriteRenderer _Left_Leg_Renderer;
	public SpriteRenderer _Right_Leg_Renderer;

	public List _Head_Sprites;
	public List _Body_Sprites;

	public List _Left_Up_Arm_Sprites;
	public List _Right_Up_Arm_Sprites;

	public List _Left_Bottom_Arm_Sprites;
	public List _Right_Bottom_Arm_Sprites;

	public List _Left_Leg_Sprites;
	public List _Right_Leg_Sprites;

	public Color[] _Skin_Color;

    private void Start()
    {
		Skin_Options._Skin_Value = PlayerPrefs.GetInt("_Skin_Value");
		Set_Skin();
	}

    public void Set_Skin()
	{

		/*
		* In this case, I dont have art for use as skin, so I create a system that change the color.
		* If you have art, only need delete the next code block, that change color.
		*/

		#region Changing Character Parts

		/*
        _Head_Renderer.sprite = _Head_Sprites[_Skin_Value];
		_Body_Renderer.sprite = _Body_Sprites[_Skin_Value];

		_Left_Up_Arm_Renderer.sprite = _Left_Up_Arm_Sprites[_Skin_Value];
		_Right_Up_Arm_Renderer.sprite = _Right_Up_Arm_Sprites[_Skin_Value];

		_Left_Bottom_Arm_Renderer.sprite = _Left_Bottom_Arm_Sprites[_Skin_Value];
		_Right_Bottom_Arm_Renderer.sprite = _Right_Bottom_Arm_Sprites[_Skin_Value];

		_Left_Leg_Renderer.sprite = _Left_Leg_Sprites[_Skin_Value];
		_Right_Leg_Renderer.sprite = _Right_Leg_Sprites[_Skin_Value];
		*/

		#endregion

		#region Changing Color

		_Head_Renderer.color = _Skin_Color[Skin_Options._Skin_Value];
		_Body_Renderer.color = _Skin_Color[Skin_Options._Skin_Value];

		_Left_Up_Arm_Renderer.color = _Skin_Color[Skin_Options._Skin_Value];
		_Right_Up_Arm_Renderer.color = _Skin_Color[Skin_Options._Skin_Value];

		_Left_Bottom_Arm_Renderer.color = _Skin_Color[Skin_Options._Skin_Value];
		_Right_Bottom_Arm_Renderer.color = _Skin_Color[Skin_Options._Skin_Value];

		_Left_Leg_Renderer.color = _Skin_Color[Skin_Options._Skin_Value];
		_Right_Leg_Renderer.color = _Skin_Color[Skin_Options._Skin_Value];

		#endregion

		if (Skin_Options._Skin_Value != PlayerPrefs.GetInt("_Skin_Value"))
		{
			PlayerPrefs.SetInt("_Skin_Value", Skin_Options._Skin_Value);
		}

	}

}

So let’s go to implement this

The code Skin_Options must to be used in object that have a BUTTON, and He need to be seted in Method On Click, because that is written in our Start Method.

Our code Skin_Manager must to be used in object that SCENE_CONTROLLER.

Now I will to answer an important question

What do I do with the animations?

In this case, you don’t need to worry with your animations, because, you will to animate the Unity Game Objects, they will are in a hierarchy, as I maked in project. The construction will to be in that style, your animator component need to be in object father in your hierarchy, and is over there, that You will to animate, your character.

This project is in My Github – > https://github.com/MauroJrDeveloper/Skin-System
Follow-me in Instagram for stay knowing when I create new posts. @mauro_developer
You can also leave our feedback, and your suggestion, I will to read all, and I will to answer for you.

Thank you for to read.