162 lines
		
	
	
		
			5.5 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			162 lines
		
	
	
		
			5.5 KiB
		
	
	
	
		
			C#
		
	
	
	
| using AdamSync;
 | |
| using HslCommunication.Profinet.OpenProtocol;
 | |
| using NetMQ;
 | |
| using System.Collections;
 | |
| using System.Collections.Generic;
 | |
| using System.Xml.Serialization;
 | |
| using UnityEngine;
 | |
| using UnityEngine.UI;
 | |
| 
 | |
| public class Devicemovement : MonoBehaviour
 | |
| {
 | |
|     public string deviceid;//当前设备ID
 | |
|     public string devicename;//获取当前设备名字
 | |
|     public Slider Progressbar;//进度条
 | |
|     public EquipmentCommon equipment;
 | |
|     public static List<Devicemovement> devicemovements = new List<Devicemovement>();
 | |
|     public string devicnumber;
 | |
|     private bool Opencoroutine = false;//是否开启协程
 | |
|     private bool ispMove = false;//是否能移动
 | |
|     private Coroutine coroutine;//接受协程
 | |
|     private float tiemr = 3;//进度条加载的时间
 | |
|     private float inception = 0;//进度条归零
 | |
|     public  Vector3  pos;
 | |
|     void Start()
 | |
|     {
 | |
|         devicemovements.Add(this);
 | |
|         devicnumber = devicemovements.Count.ToString();
 | |
| 
 | |
|     }
 | |
| 
 | |
| 
 | |
|     void Update()
 | |
|     {
 | |
|         if (Input.GetMouseButtonDown(2))
 | |
|         {
 | |
|             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 | |
|             RaycastHit hit;
 | |
|             if (Physics.Raycast(ray, out hit, Mathf.Infinity))
 | |
|             {
 | |
|                 Devicemovement devicemovement = hit.collider.gameObject.GetComponent<Devicemovement>();
 | |
|                 equipment = hit.collider.gameObject.GetComponent<EquipmentCommon>();
 | |
|                 pos = new Vector3(transform.position.x, transform.position.y, transform.position.z);
 | |
|                 if (devicemovement && devicemovement.devicnumber == devicnumber && equipment.isPlayer)
 | |
|                 {
 | |
|                     devicename = equipment.equipmentType;
 | |
|                     deviceid = equipment.deviceID;
 | |
|                     Debug.Log(deviceid);
 | |
|                     Opencoroutine = true;
 | |
|                     Progressbar.gameObject.SetActive(true);
 | |
|                     coroutine = StartCoroutine(Schedule());
 | |
|                 }
 | |
|             }
 | |
|         }
 | |
|         if (Progressbar.value == 1 && coroutine != null)
 | |
|         {
 | |
|             StopCoroutine(Schedule());
 | |
|             coroutine = null;
 | |
|             Progressbar.gameObject.SetActive(false);
 | |
|             inception = 0;
 | |
|             ispMove = true;
 | |
|             Progressbar.value = 0;
 | |
|         }
 | |
|         if (!Input.GetMouseButtonUp(2) && ispMove)
 | |
|         {
 | |
|             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 | |
|             RaycastHit hit;
 | |
|             if (Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 8))
 | |
|             {
 | |
|                 Opencoroutine = false;
 | |
|                 transform.localPosition = hit.point;
 | |
|             }
 | |
|         }
 | |
|         if (Input.GetMouseButtonUp(2))
 | |
|         {
 | |
|             Opencoroutine = false;
 | |
|             ispMove = false;
 | |
|             if (coroutine != null)
 | |
|             {
 | |
|                 StopCoroutine(Schedule());
 | |
|                 coroutine = null;
 | |
|                 Progressbar.gameObject.SetActive(false);
 | |
|                 Progressbar.value = 0;
 | |
|                 inception = 0;
 | |
|             }
 | |
|             if (transform.position!=pos)
 | |
|             {
 | |
|                 string msg = Gettele();
 | |
|                 MyNetMQClient.instance.Send(msg);
 | |
|                 Debug.Log(msg); 
 | |
|                 equipment.SetDatabaseInfo("r1", $"{transform.position.x},{transform.position.y},{transform.position.z},{transform.eulerAngles.x},{transform.eulerAngles.y},{transform.eulerAngles.z}");
 | |
|             }
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     IEnumerator Schedule()
 | |
|     {
 | |
|         while (inception < tiemr && Opencoroutine)
 | |
|         {
 | |
|             inception += Time.deltaTime;
 | |
|             Progressbar.value = Mathf.Lerp(Progressbar.minValue, Progressbar.maxValue, inception / tiemr);
 | |
|             yield return null;
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     void OnMouseDown()
 | |
|     {
 | |
|         //Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 | |
|         //RaycastHit hit;
 | |
|         //if (Physics.Raycast(ray, out hit, Mathf.Infinity))
 | |
|         //{
 | |
|         //    Devicemovement devicemovement = hit.collider.gameObject.GetComponent<Devicemovement>();
 | |
|         //    equipment = hit.collider.gameObject.GetComponent<EquipmentCommon>();
 | |
|         //    if (devicemovement && devicemovement.devicnumber == devicnumber&&equipment.isPlayer)
 | |
|         //    {
 | |
|         //        deviceid = equipment.deviceID;
 | |
|         //        Debug.Log(deviceid);
 | |
|         //        Opencoroutine = true;
 | |
|         //        Progressbar.gameObject.SetActive(true);
 | |
|         //        coroutine = StartCoroutine(Schedule());
 | |
|         //    }
 | |
|         //}
 | |
|     }
 | |
|     void OnMouseDrag()
 | |
|     {
 | |
|         //if (ispMove)
 | |
|         //{
 | |
|         //    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 | |
|         //    RaycastHit hit;
 | |
|         //    if (Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 8))
 | |
|         //    {
 | |
|         //        Opencoroutine = false;
 | |
|         //        transform.localPosition = hit.point;
 | |
|         //    }
 | |
|         //}
 | |
|     }
 | |
| 
 | |
|     void OnMouseUp()
 | |
|     {
 | |
|         //Opencoroutine = false;
 | |
|         //ispMove = false;
 | |
|         //if (coroutine != null)
 | |
|         //{
 | |
|         //    StopCoroutine(Schedule());
 | |
|         //    coroutine = null;
 | |
|         //    Progressbar.gameObject.SetActive(false);
 | |
|         //    Progressbar.value = 0;
 | |
|         //    inception = 0;
 | |
|         //}
 | |
|         //if (ispMove == false)
 | |
|         //{
 | |
| 
 | |
|         //}
 | |
|     }
 | |
|     protected string Gettele()
 | |
|     {
 | |
|         Debug.Log(transform.position.x);
 | |
|         Debug.Log(transform.position.y);
 | |
|         Debug.Log(transform.position.z);
 | |
|         return string.Format("{0},{1},{2},{3},{4}", "Teleportation", deviceid, transform.position.x, transform.position.y, transform.position.z);
 | |
|     }
 | |
| }
 |