233 lines
8.2 KiB
C#
233 lines
8.2 KiB
C#
using System;
|
||
using UnityEngine;
|
||
using UnityEngine.EventSystems;
|
||
using UnityEngine.UI;
|
||
using ZXK.BYSS;
|
||
using ZXK.UTility;
|
||
|
||
namespace Lylibs
|
||
{
|
||
public class LyRawImage : MonoBehaviour, IPointerClickHandler
|
||
{
|
||
// 点击RawImage时,相对RawImage自身的坐标
|
||
private Vector2 ClickPosInRawImg;
|
||
|
||
// 预览映射相机
|
||
private Camera PreviewCamera;
|
||
private Camera UICamera;
|
||
private Canvas canvasa;
|
||
public static LyRawImage instance;
|
||
|
||
[Header("PLC指示灯状态图")]
|
||
public GameObject GDCGQ_StateDefault;
|
||
public GameObject GDCGQ_State1;
|
||
public GameObject GDCGQ_State2;
|
||
|
||
|
||
[Header("3D指示灯状态")]
|
||
public GameObject Default_3D;
|
||
public GameObject State1_3D;
|
||
public GameObject State2_3D;
|
||
|
||
|
||
private void Awake()
|
||
{
|
||
instance = this;
|
||
}
|
||
|
||
public void Start()
|
||
{
|
||
// 初始获取预览映射相机
|
||
if (PreviewCamera == null)
|
||
{
|
||
PreviewCamera = GameObject.Find("PreviewCamera").transform.GetComponent<Camera>();
|
||
PreviewCamera.targetTexture = Resources.Load<RenderTexture>("CameraRenderTexture");
|
||
//Debug.Log("测试到!!!!!!!!");
|
||
}
|
||
|
||
if (UICamera == null)
|
||
{
|
||
UICamera = GameObject.Find("Main Camera").transform.GetComponent<Camera>();
|
||
}
|
||
|
||
if (canvasa == null)
|
||
{
|
||
canvasa = transform.GetComponentInParent<Canvas>();
|
||
}
|
||
|
||
canvasa.gameObject.SetActive(false);
|
||
}
|
||
|
||
public void Run()
|
||
{
|
||
|
||
canvasa.gameObject.SetActive(true);
|
||
GDCGQ_State1.gameObject.SetActive(false);
|
||
GDCGQ_State2.gameObject.SetActive(false);
|
||
|
||
|
||
Default_3D.gameObject.SetActive(true);
|
||
State1_3D.gameObject.SetActive(false);
|
||
State2_3D.gameObject.SetActive(false);
|
||
|
||
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// 光电传感器 不亮光
|
||
/// </summary>
|
||
public void RunGDCGQ_Default()
|
||
{
|
||
|
||
|
||
GDCGQ_StateDefault.gameObject.SetActive(true);
|
||
GDCGQ_State1.gameObject.SetActive(false);
|
||
GDCGQ_State2.gameObject.SetActive(false);
|
||
Debug.Log("调用一次状态0000");
|
||
Default_3D.gameObject.SetActive(true);
|
||
State1_3D.gameObject.SetActive(false);
|
||
State2_3D.gameObject.SetActive(false);
|
||
|
||
|
||
|
||
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 光电传感器
|
||
/// </summary>
|
||
public void RunGDCGQ_State1()
|
||
{
|
||
|
||
|
||
GDCGQ_State1.gameObject.SetActive(true);
|
||
GDCGQ_State2.gameObject.SetActive(false);
|
||
Debug.Log("调用一次状态1");
|
||
Default_3D.gameObject.SetActive(false);
|
||
State1_3D.gameObject.SetActive(true);
|
||
State2_3D.gameObject.SetActive(false);
|
||
|
||
|
||
|
||
}
|
||
|
||
|
||
|
||
public void RunGDCGQ_State2()
|
||
{
|
||
|
||
GDCGQ_State1.gameObject.SetActive(false);
|
||
GDCGQ_State2.gameObject.SetActive(true);
|
||
Debug.Log("调用一次状态2");
|
||
Default_3D.gameObject.SetActive(false);
|
||
State1_3D.gameObject.SetActive(false);
|
||
State2_3D.gameObject.SetActive(true);
|
||
|
||
|
||
}
|
||
void IPointerClickHandler.OnPointerClick(PointerEventData eventData)
|
||
{
|
||
RectTransform rectTransform = this.GetComponent<RectTransform>();
|
||
if (AppManagement.Instance._CurType == EnumCtrl.Type.CLFJ)
|
||
{
|
||
GameObject.Find("材料分拣虚拟仿真系统").GetComponent<PracticeAndExamInSceneCtrl>()
|
||
.ClFJ_ButtonLogic(GetRawImageObj(eventData, rectTransform, PreviewCamera));
|
||
}
|
||
|
||
if (AppManagement.Instance._CurType == EnumCtrl.Type.XHBY)
|
||
{
|
||
Debug.Log("调用循环搬运机械手虚拟仿真系统RayHit");
|
||
GameObject.Find("循环搬运机械手虚拟仿真系统").GetComponent<PracticeAndExamInSceneCtrl>()
|
||
.Set_XHBY_State(GetRawImageObj(eventData, rectTransform, PreviewCamera));
|
||
}
|
||
}
|
||
|
||
#region UI不绑定相机
|
||
|
||
/// <summary>
|
||
/// 通过点击RawImage中映射的RenderTexture画面,对应的相机发射射线,得到物体
|
||
/// </summary>
|
||
/// <param name="data">rawimage点击的数据</param>
|
||
/// <param name="rawImgRectTransform">rawimage的recttransfotm</param>
|
||
/// <param name="previewCamera">生成rendertexture中画面的相机</param>
|
||
/// <returns>返回射线碰撞到的物体</returns>
|
||
// private GameObject GetRawImageObj(PointerEventData data, RectTransform rawImgRectTransform, Camera previewCamera)
|
||
// {
|
||
// GameObject obj = null;
|
||
// var pos = (data.position - (Vector2)rawImgRectTransform.position) / rawImgRectTransform.lossyScale - rawImgRectTransform.rect.position;
|
||
// var rate = pos / rawImgRectTransform.rect.size;
|
||
// var ray = previewCamera.ViewportPointToRay(rate);
|
||
// RaycastHit raycastHit;
|
||
// if (Physics.Raycast(ray, out raycastHit))
|
||
// {
|
||
// // Debug.Log(raycastHit.transform.name);
|
||
// obj = raycastHit.transform.gameObject;
|
||
// }
|
||
// return obj;
|
||
// }
|
||
//
|
||
private RaycastHit GetRawImageObj(PointerEventData data, RectTransform rawImgRectTransform,
|
||
Camera previewCamera)
|
||
{
|
||
GameObject obj = null;
|
||
var pos = (data.position - (Vector2) rawImgRectTransform.position) / rawImgRectTransform.lossyScale -
|
||
rawImgRectTransform.rect.position;
|
||
var rate = pos / rawImgRectTransform.rect.size;
|
||
var ray = previewCamera.ViewportPointToRay(rate);
|
||
RaycastHit raycastHit;
|
||
if (Physics.Raycast(ray, out raycastHit))
|
||
{
|
||
// Debug.Log(raycastHit.transform.name);
|
||
obj = raycastHit.transform.gameObject;
|
||
}
|
||
|
||
return raycastHit;
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region UI有专门的UI相机
|
||
|
||
/// <summary>
|
||
/// 射线投射
|
||
/// </summary>
|
||
/// <param name="canvas">画布</param>
|
||
/// <param name="mousePosition">当前Canvas下点击的鼠标位置</param>
|
||
/// <param name="previewImage">预览图</param>
|
||
/// <param name="previewCamera">预览映射图的摄像机</param>
|
||
private void CheckDrawRayLine(Canvas canvas, Vector3 mousePosition, RawImage previewImage, Camera previewCamera,
|
||
Camera UiCamera)
|
||
{
|
||
Vector2 ClickPosInRawImg;
|
||
// 将UI相机下点击的UI坐标转为相对RawImage的坐标
|
||
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.transform as RectTransform,
|
||
mousePosition, UiCamera, out ClickPosInRawImg))
|
||
{
|
||
//获取预览图的长宽
|
||
float imageWidth = previewImage.rectTransform.rect.width;
|
||
float imageHeight = previewImage.rectTransform.rect.height;
|
||
//获取预览图的坐标,此处RawImage的Pivot需为(0,0),不然自己再换算下
|
||
float localPositionX = previewImage.rectTransform.localPosition.x;
|
||
float localPositionY = previewImage.rectTransform.localPosition.y;
|
||
|
||
//获取在预览映射相机viewport内的坐标(坐标比例)
|
||
float p_x = (ClickPosInRawImg.x - localPositionX) / imageWidth;
|
||
float p_y = (ClickPosInRawImg.y - localPositionY) / imageHeight;
|
||
|
||
//从视口坐标发射线
|
||
Ray p_ray = previewCamera.ViewportPointToRay(new Vector2(p_x, p_y));
|
||
RaycastHit p_hitInfo;
|
||
if (Physics.Raycast(p_ray, out p_hitInfo))
|
||
{
|
||
//显示射线,只有在scene视图中才能看到
|
||
Debug.DrawLine(p_ray.origin, p_hitInfo.point);
|
||
// Debug.Log(p_hitInfo.transform.name);
|
||
}
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
}
|
||
} |