Hi there,
I’m trying to add functionality in my game and allow the user customize the character.
I thought I’d just start of with colours and textures rather than different meshes just to keep it simple but, I’m still not sure how to approach it.
I’ve run into a bit of trouble so far, for example, if I’d like to change the character’s t-shirt texture, the t-shirt would have to be a separate mesh which would mean another skinned mesh and I’m guessing would have a big impact on speed.
Can anyone point me in the right direction here?
Thanks
Pete
We use both ways in our game to customize characters. The first way is to actually turn on and off skinned meshes or objects that are attached to the skeleton. so lets say you want regular pants and baggy pants. You would have 2 meshes PantsA and PantsB. If PantsA is active then PantsB is always not active.
The other thing we do that can be used along with or in place of the first method is to change the texture on the material. This change is always saved so thats a blessing or a curse. So we have an array of possible textures and I have a button that changes the texture on the material.
Here is our texture switcher class
public class TextureSwitcher : MonoBehaviour {
public Texture2D[] HeadTextures;
public Texture2D[] TorsoTextures;
public Texture2D[] PantTextures;
public Material[] playerPrefabs;
public void ChangeMaterialPrefab(int index,Material skinMaterial)
{
playerPrefabs[index].mainTexture = skinMaterial.mainTexture;
}
public void Next(Texture2D[] textureArray,Material skinMaterial)
{
Debug.Log("skinMaterial.name="+skinMaterial.name);
Debug.Log("skinMaterial.mainTexture.name="+skinMaterial.mainTexture.name);
TextureSwitcher.NextTexture(textureArray,skinMaterial);
}
public void Previous(Texture2D[] textureArray,Material skinMaterial)
{
Debug.Log("skinMaterial.name="+skinMaterial.name);
Debug.Log("skinMaterial.mainTexture.name="+skinMaterial.mainTexture.name);
TextureSwitcher.PreviousTexture(textureArray,skinMaterial);
}
public static void PreviousTexture(Texture2D[] textureArray,Material skinMaterial)
{
Texture2D currentTexture = (Texture2D)skinMaterial.mainTexture;
int index = IndexOfCurrentTexture(textureArray,currentTexture);
Debug.Log("currentTexture.index="+index);
Debug.Log("textureArray.GetLength(0)="+textureArray.GetLength(0));
if( (index > 0) )
{
//skinMaterial.SetTexture("_MainTex",textureArray[index+1]);
skinMaterial.mainTexture = textureArray[index-1];
}
else if( (index-1) < 0 )
{
//skinMaterial.SetTexture("_MainTex",textureArray[0]);
skinMaterial.mainTexture = textureArray[(textureArray.GetLength(0)-1)];
}
}
public static void NextTexture(Texture2D[] textureArray,Material skinMaterial)
{
Texture2D currentTexture = (Texture2D)skinMaterial.mainTexture;
int index = IndexOfCurrentTexture(textureArray,currentTexture);
Debug.Log("currentTexture.index="+index);
Debug.Log("textureArray.GetLength(0)="+textureArray.GetLength(0));
if( (index > -1) ((index+1) < textureArray.GetLength(0)) )
{
//skinMaterial.SetTexture("_MainTex",textureArray[index+1]);
skinMaterial.mainTexture = textureArray[index+1];
}
else if( (index+1) >= (textureArray.GetLength(0)-1) )
{
//skinMaterial.SetTexture("_MainTex",textureArray[0]);
skinMaterial.mainTexture = textureArray[0];
}
}
public static int IndexOfCurrentTexture(Texture2D[] textureArray,Texture2D currentTexture)
{
int index = -1;
for(int i=0;i<textureArray.GetLength(0);i++)
{
Texture2D tex = textureArray[i];
if(tex == currentTexture)
index = i;
}
return index;
}
}
Add break; in your for statements if you need more performance from the script. Thanks to corey.stpierre for the performance tip.
Sorry about the delay ( i had a scary deadline and had to leave unity for a bit)
Thanks, that sounds like a nice straightforward way to approach it but I thought that would be too heavy on the resources.
Even if the other items of clothes are hidden you still have 2 or 3 skinned meshes. I thought it was pushing it to have one on the iPhone?