1. 效果展示
2. 配件区脚本
RightPanel脚本:当鼠标拖入到配件区时,创建配件预制体,隐藏拖拽的元件。
using UnityEngine;
using UnityEngine.EventSystems;
public class RightPanel : MonoBehaviour, IPointerEnterHandler
{
public GameObject contentPrefbs;
public Transform parentTransform;
public void OnPointerEnter(PointerEventData eventData)
{
var gameobj = SwitchOperation.Instance.GetCurrentSwitchObj();
if (gameobj != null)
{
gameobj = SwitchOperation.Instance.GetCurrentSwitchObj().gameObject;
var dd = gameobj.GetComponent<SwitchDrag>();
if (dd.isdrag && gameobj.activeSelf && dd.status == 1)
{
//修改透明度
TransparentSet.Instance.RecoveryTransparent();
gameobj.SetActive(false);
SwitchOperation.Instance.hideInfo(false);
//获取配件区命名UI的信息
string objName = SwitchOperation.Instance.GetCurrentSwitchObj().gameObject.name + "_UI";
//判断是否存在,不存在则创建配件区RightItem
var tempObj = parentTransform.Find(objName) != null ? parentTransform.Find(objName).gameObject : Instantiate(contentPrefbs, parentTransform);
//并命名作为重复判断的依据
tempObj.name = objName;
}
}
}
}
脚本挂载
3. 配件区UI拖拽
配件区UI的拖拽,主要是模拟选择配件,安装配件。
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class RightItem : MonoBehaviour, IBeginDragHandler, IDragHandler
{
// Start is called before the first frame update
private Button button;
private string objName;
private Image img;
private GameObject moveObj;
Vector3 offPos;//存储按下鼠标时的图片-鼠标位置差
Vector3 arragedPos; //保存经过整理后的向量,用于图片移动
public void OnBeginDrag(PointerEventData eventData)
{
if (RectTransformUtility.ScreenPointToWorldPointInRectangle(transform.GetComponent<RectTransform>(), Input.mousePosition
, eventData.enterEventCamera, out arragedPos))
{
Debug.Log("移动位置1" + arragedPos);
offPos = transform.position - arragedPos;
}
}
private void Destroy()
{
Destroy(this.gameObject);
}
public void OnDrag(PointerEventData eventData)
{
transform.position = offPos + Input.mousePosition;
var gameObj = GameObject.Find("Switch");
moveObj = UHelper.FindTheChild(gameObj.gameObject, objName).gameObject;
if (!moveObj.activeSelf)
{
moveObj.SetActive(true);
moveObj.GetComponent<SwitchDrag>().isdrag = true;
moveObj.GetComponent<SwitchDrag>().status = 0;
Invoke("Destroy", 0.1f);
}
}
// Start is called before the first frame update
void Start()
{
button = GetComponent<Button>();
objName = this.name.Replace("_UI", "");
button.AddOnClick(onClick);
}
void onClick()
{
Debug.Log("compentPart:" + objName);
}
// Update is called once per frame
void Update()
{
}
}
脚本挂载
4. 调整脚本
- 调整SwitchDrag,增加元件状态信息,删除不需要的变量,修改元件拖拽方式为跟踪鼠标轨迹移动
using UnityEngine;
public class SwitchDrag : MonoBehaviour
{
public bool isdrag = false;//用来判断物体是否被拖拽
Vector3 targetPos;
Vector3 lastPos;
Vector3 originalPos;
//private Outline outline;
private SwitchPart switchPart;
//状态0为默认 1为卸下,作为配件区UI创建的依据
public int status = 0;
void Start()
{
switchPart = GetComponent<SwitchPart>();
originalPos = this.transform.position;
}
//当鼠标按下物体
void OnMouseDown()
{
if (!isdrag)
{
lastPos = Camera.main.ScreenToViewportPoint(Input.mousePosition) - Camera.main.WorldToViewportPoint(transform.position);
}
isdrag = true;
status = 1;
}
private void OnMouseOver()
{
if (Input.GetMouseButtonDown(1))
{
isdrag = false;
this.transform.position = originalPos;
}
}
void Update()
{
moveObj();
}
void moveObj()
{
//moveObj1 = UHelper.FindTheChild(gameObj.gameObject, objName).gameObject;
if (this.gameObject.activeSelf && isdrag)
{
Vector3 screenPos = Camera.main.WorldToScreenPoint(this.transform.position);
//获取鼠标位置
Vector3 mousePos = Input.mousePosition;
//因为鼠标只有X,Y轴,所以要赋予给鼠标Z轴
mousePos.z = screenPos.z;
//把鼠标的屏幕坐标转换成世界坐标
Vector3 worldPos = Camera.main.ScreenToWorldPoint(mousePos);
//控制物体移动
//this.transform.position = worldPos;
this.transform.position = Vector3.Lerp(this.transform.position, worldPos, Time.deltaTime * 15);
}
}
//当鼠标拖拽物体
void OnMouseDrag()
{
targetPos = Camera.main.ViewportToWorldPoint(Camera.main.ScreenToViewportPoint(Input.mousePosition) - lastPos);
transform.position = new Vector3(targetPos.x, targetPos.y, targetPos.z);
isdrag = true;
SwitchOperation.Instance.currentIndex = SwitchOperation.Instance.GetSwitchParts().FindIndex(p => p.ObjName == switchPart.mSwitchParts.ObjName);
}
}