使用2D轮转图实现换装功能,要显先实现一个简单的2D轮转图:
public class ChangeWear : MonoBehaviour, IDragHandler, IEndDragHandler
{
public Image image;
public int num;
float r;
float angle;
float angle_all = 0;
List<GameObject> objs = new List<GameObject>();
List<Transform> trans = new List<Transform>();
private void Start()
{
r = num * (image.rectTransform.sizeDelta.x + 30) / 2 / Mathf.PI;
angle = 2 * Mathf.PI / num;
Move();
}
private void Move()
{
for (int i = 0; i < num; i++)
{
if (objs.Count < num)
{
objs.Add(Instantiate(image.gameObject, transform));
objs[i].transform.Find("Image").GetComponent<Image>().sprite = Resources.Load<Sprite>("Icon/Sword_" + i);
trans.Add(objs[i].transform);
}
float x = Mathf.Sin(angle * i + angle_all) * r;
float z = Mathf.Cos(angle * i + angle_all) * r;
float p = (z + r) / (r + r);
float scale = 1 - p + p / 2;
objs[i].transform.localScale = Vector3.one * scale;
objs[i].transform.localPosition = new Vector3(x, z / 5, 0);
}
trans.Sort((a, b) => (int)(a.localScale.z * 100 - b.localScale.z * 100));
for (int i = 0; i < trans.Count; i++)
{
trans[i].SetSiblingIndex(i);
}
}
public void OnDrag(PointerEventData eventData)
{
angle_all -= eventData.delta.x / r;
Move();
}
public void OnEndDrag(PointerEventData eventData)
{
int index = objs.IndexOf(trans[num - 1].gameObject);
float angle_move = objs[index].transform.localPosition.x / r;
DOTween.To(() => angle_all, t =>
{
angle_all = t;
Move();
}, angle_all + angle_move, 0.6f);
}
}
效果如下:
然后新建一个只渲染玩家模型的摄像机,把图像生成到一张RT图上,并将图片的层级调整到中
for (int i = 0; i < trans.Count; i++)
{
trans[i].SetSiblingIndex(i);
}
img.transform.SetSiblingIndex(7);//人物图形显示在中央
然后再根据轮转图中央图片的下标,给人物的手部生成对应的武器模型: