Unity UI拖拽模型选择功能拖拽模型选择功能
指定一块区域,玩家鼠标or手指拖拽这个区域,模型会进行偏移,并用于进行人物、道具的选择
给模型定义一些属性
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UIModelUtil : MonoBehaviour
{
public Animator animator;
public int id;
public int index;
}
模型控制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UIModelControl : MonoBehaviour
{
public Transform modelsParent;
public Transform centerPos;
public float interval;
public bool loop;
List<UIModelUtil> models;
bool isPressing;
public UIDrag dragComp;
Vector3 mousePos;
private void Awake()
{
if(models == null)
{
int i = 0;
models = new List<UIModelUtil>();
foreach(UIModelUtil util in modelsParent.GetComponentsInChildren<UIModelUtil>())
{
models.Add(util);
//util.index = i;
Vector3 pos = Vector3.zero;
pos.x = i * interval;
util.transform.localPosition = pos;
i++;
}
}
}
private void Start()
{
JumpToSelect();
}
private void Update()
{
//接受拖拽事件
if (isPressing)
{
float x = GetInputDeltaX();
int dir = 0;
if (x > 0) dir = 1;