146 lines
3.9 KiB
C#
146 lines
3.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
|
|
namespace QFramework
|
|
{
|
|
public class ObjClickLongCondition : ICondition
|
|
{
|
|
|
|
private static SimpleObjectPool<ObjClickLongCondition> mSimpleObjectPool =
|
|
new SimpleObjectPool<ObjClickLongCondition>(() => new ObjClickLongCondition(), null, 10);
|
|
|
|
private ObjClickLongCondition() { }
|
|
public GameObject obj = null;
|
|
string path;
|
|
string deviceName;
|
|
float time;
|
|
float curTime;
|
|
bool isDown = false;
|
|
public static ObjClickLongCondition Allocate(string path, Dictionary<string, string> datas)
|
|
{
|
|
var conditionAction = mSimpleObjectPool.Allocate();
|
|
conditionAction.ActionID = ActionKit.ID_GENERATOR++;
|
|
conditionAction.Deinited = false;
|
|
conditionAction.Reset();
|
|
conditionAction.path = path;
|
|
conditionAction.deviceName = datas.ContainsKey("deviceName") ? datas["deviceName"] : null;
|
|
if (datas.ContainsKey("time"))
|
|
{
|
|
float.TryParse(datas["time"], out conditionAction.time);
|
|
}
|
|
return conditionAction;
|
|
}
|
|
public bool Check()
|
|
{
|
|
if (obj == null)
|
|
{
|
|
if (string.IsNullOrEmpty(deviceName))
|
|
{
|
|
obj = Utility.FindObj(path);
|
|
}
|
|
else
|
|
{
|
|
obj = DeviceController.Instance.GetDeviceObj(deviceName);
|
|
if (obj == null)
|
|
{
|
|
Debug.LogError($"没有找到 path:{path} deviceName:{deviceName}");
|
|
}
|
|
}
|
|
}
|
|
|
|
// 检测鼠标左键按下
|
|
if (Input.GetMouseButtonDown(0))
|
|
{
|
|
OnMouseDown();
|
|
}
|
|
|
|
// 检测鼠标左键松开
|
|
if (Input.GetMouseButtonUp(0))
|
|
{
|
|
OnMouseUp();
|
|
}
|
|
|
|
if (isDown && EventSystem.current.IsPointerOverGameObject() == false)
|
|
{
|
|
Vector3 mousePos = Input.mousePosition;
|
|
Ray ray = Camera.main.ScreenPointToRay(mousePos);
|
|
RaycastHit hit;
|
|
if (Physics.Raycast(ray, out hit))
|
|
{
|
|
if (obj == hit.collider.gameObject)
|
|
{
|
|
curTime -= Time.deltaTime;
|
|
if (curTime <= 0)
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public void OnMouseDown()
|
|
{
|
|
curTime = this.time;
|
|
isDown = true;
|
|
}
|
|
|
|
public void OnMouseUp()
|
|
{
|
|
isDown = false;
|
|
}
|
|
|
|
|
|
public bool Paused { get; set; }
|
|
public bool Deinited { get; set; }
|
|
public ulong ActionID { get; set; }
|
|
public ActionStatus Status { get; set; }
|
|
public void OnStart()
|
|
{
|
|
}
|
|
|
|
public void OnExecute(float dt)
|
|
{
|
|
if (Check())
|
|
{
|
|
this.Finish();
|
|
}
|
|
}
|
|
|
|
public void OnFinish()
|
|
{
|
|
}
|
|
|
|
public void Deinit()
|
|
{
|
|
if (!Deinited)
|
|
{
|
|
Deinited = true;
|
|
obj = null;
|
|
path = null;
|
|
mSimpleObjectPool.Recycle(this);
|
|
}
|
|
}
|
|
|
|
public void Reset()
|
|
{
|
|
curTime = 0;
|
|
Paused = false;
|
|
Status = ActionStatus.NotStart;
|
|
}
|
|
}
|
|
|
|
//public static class ObjClickLongConditionExtension
|
|
//{
|
|
// public static ISequence ObjClickLongCondition(this ISequence self, string uipath)
|
|
// {
|
|
// return self.Append(QFramework.ObjClickLongCondition.Allocate(uipath));
|
|
// }
|
|
//}
|
|
} |