如图所示,点击HEAD箭头可以实现头部换装,点击HAND箭头可以实现手部换装,点击地下的颜色按钮可以换颜色,点击PLAY按钮可以实现进入下一场景,
实现代码如下所示:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class menucontrollers : MonoBehaviour {
public Color purple;//因为系统没有紫色,所以自定义一种紫色
public SkinnedMeshRenderer headRenderer;
public Mesh[] headMeshArray;
private int headMeshIndex =0; //定义head的数组,可以换不同的头部形状
public SkinnedMeshRenderer handRenderer;
public Mesh[] handMeshArray;//手部数组
private int handMeshIndex = 0;
public SkinnedMeshRenderer[] bodyArray;//定义body数组,把身体的各部分加进数组,实现整体变色
private Color[] colorArray;
private int colorIndex = -1;
void Start()
{
colorArray = new Color[] { Color.blue, Color.cyan, Color.green, purple, Color.red };
DontDestroyOnLoad(this.gameObject);
}
public void OnHeadMeshNext()
{
headMeshIndex++;
headMeshIndex %= headMeshArray.Length;
headRenderer.sharedMesh = headMeshArray[headMeshIndex];
}
public void OnHandMeshNext()
{
handMeshIndex++;
handMeshIndex %= handMeshArray.Length;
handRenderer.sharedMesh = handMeshArray[handMeshIndex];
}
public void OnChangeColorBlue()
{
colorIndex=0;
OnChangeColor(Color.blue);
}
public void OnChangeColorCyan()
{
colorIndex=1;
OnChangeColor(Color.cyan);
}
public void OnChangeColorGreen()
{
colorIndex=2;
OnChangeColor(Color.green);
}
public void OnChangeColorPurple()
{
colorIndex=3;
OnChangeColor(purple);
}
public void OnChangeColorRed()
{
colorIndex=4;
OnChangeColor(Color.red);
}
void OnChangeColor(Color c)
{
foreach (SkinnedMeshRenderer renderer in bodyArray)
{
renderer.material.color=c;
}
}
void Save() //保存当前套装,进入下一场景
{
PlayerPrefs.SetInt("HeadMeshIndex",headMeshIndex);
PlayerPrefs.SetInt("HandMeshIndex",handMeshIndex);
PlayerPrefs.SetInt("ColorIndex",colorIndex);
}
public void OnPlay()
{
Save();
Application.LoadLevel(1);//进入下一场景
}
}
如上为脚本代码自定义的public,选择合适的代入。