使用Unity做下列的功能实现:鼠标点击下去,出现抹布,抹布随着鼠标的移动而移动。当抹布擦拭神灯后,会出现灯神。
//鼠标版
if (Input.GetMouseButtonDown(0))
{
dusterCloth = Instantiate(dusterClothPre) as GameObject;
dusterCloth.transform.position = ca.ScreenToWorldPoint(Input.mousePosition);
dusterCloth.transform.parent = clothParent;
dusterCloth.transform.localScale = Vector3.one;
dusterCloth.transform.localEulerAngles = Vector3.zero;
}
if (Input.GetMouseButton(0))
{
dusterCloth.transform.position = ca.ScreenToWorldPoint(Input.mousePosition);
RaycastHit hit;
Ray ray = ca.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.name == "1" || hit.collider.name == "2")
{
timeMax += Time.deltaTime;
if (timeMax > 1f)
{
timeMax = 0;
//触发按钮
Debug.Log("Send Message Trigger");
transform.parent.GetComponent<Sg_Aladdin>().SelectBtnMessage(hit.collider.gameObject);
}
}
}
}
if (Input.GetMouseButtonUp(0))
{
//删掉抹布
Destroy(dusterCloth);
timeMax = 0;
}
//触屏版
if (Input.touchCount > 0)
{
var touch = Input.GetTouch(0);
switch (touch.phase)
{
case TouchPhase.Began:
dusterCloth = Instantiate(dusterClothPre) as GameObject;
dusterCloth.transform.position = ca.ScreenToWorldPoint(Input.GetTouch(0).position);
dusterCloth.transform.parent = clothParent;
dusterCloth.transform.localScale = Vector3.one;
dusterCloth.transform.localEulerAngles = Vector3.zero;
break;
case TouchPhase.Moved:
dusterCloth.transform.position = ca.ScreenToWorldPoint(Input.GetTouch(0).position);
RaycastHit hit;
Ray ray = ca.ScreenPointToRay(Input.GetTouch(0).position);
if (Physics.Raycast(ray, out hit))
{
uilabel.text = hit.collider.name;
if (hit.collider.name == "1" || hit.collider.name == "2")
{
timeMax += Time.deltaTime;
if (timeMax > 1f)
{
timeMax = 0;
//触发按钮
}
}
}
break;
case TouchPhase.Ended:
//删掉抹布
Destroy(dusterCloth);
timeMax = 0;
break;
}
}
Notice : 界面摆放时,需要触发事件的UI置于最顶层。