NewN_UAVPlane/Assets/Temp/Scripts/Secondarymovement.cs

162 lines
4.3 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
public class Secondarymovement : MonoBehaviour
{
public static List<Secondarymovement> secondarymovements = new List<Secondarymovement>();
public string number;//每个设备的个数
public Slider slider;
private bool isDragging = false;
private bool isp = false;
private float time1 = 3;
private float timer = 0;
private Coroutine myCoroutine;
public UnityEvent standaloneevent;
public UnityEvent doubleclickecent;
public float timer1;//用来判断是否事单机还是双击
public float lasttime;//计入当前场景的时间
public int count;//用来判断点击了几下
public float interval = 0.2f;//用来判断双击间隔时间
private bool iscount = true;//用来判断的是单机还是双击
void Awake()
{
if (GameMain.skip ==false)
{
transform.gameObject.GetComponent<Secondarymovement>().enabled = false;
}
}
void Start()
{
secondarymovements.Add(this);
number = secondarymovements.Count.ToString();
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
Secondarymovement secondarymovement = hit.collider.GetComponent<Secondarymovement>();
if (secondarymovement&&secondarymovement.number==number)
{
Debug.Log(transform.name);
isp = true;
slider.gameObject.SetActive(true);
myCoroutine = StartCoroutine(FillProgressBar());
}
}
}
if (slider.value == 1 && myCoroutine != null)
{
StopCoroutine(myCoroutine);
myCoroutine = null;
slider.gameObject.SetActive(false);
timer = 0;
isDragging = true;
slider.value = 0;
}
if (Input.GetMouseButtonDown(1))
{
timer1 = 0.2f;
if (Time.realtimeSinceStartup - lasttime < interval)
{
count = 2;
}
else
{
count = 1;
}
}
if (Input.GetMouseButtonUp(1))
{
iscount = false;
lasttime = Time.realtimeSinceStartup;
}
if (!iscount)
{
timer1 -= Time.deltaTime;
if (timer1 < 0)
{
if (count == 1)
{
standaloneevent?.Invoke();
}
else if (count == 2)
{
Deletionmodel();
doubleclickecent?.Invoke();
}
iscount = true;
}
}
}
private void Deletionmodel()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 1000))
{
Secondarymovement secondarymovement = hit.collider.GetComponent<Secondarymovement>();
if (secondarymovement&&secondarymovement.number==number)
{
Destroy(gameObject);
}
}
}
IEnumerator FillProgressBar()
{
while (timer < time1 && isp)
{
timer += Time.deltaTime;
slider.value = Mathf.Lerp(slider.minValue, slider.maxValue, timer / time1);
yield return null;
}
}
void OnMouseDown()
{
}
void OnMouseDrag()
{
if (isDragging)
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo, 1000, 1 << 8))
{
isp = true;
transform.localPosition = hitInfo.point;
}
}
}
void OnMouseUp()
{
isp = false;
if (myCoroutine != null)
{
StopCoroutine(myCoroutine);
myCoroutine = null;
slider.gameObject.SetActive(false);
slider.value = 0;
timer = 0;
}
isDragging = false;
}
}