unity 射线点击 点击到模型 UI文本改变 长按模型3.5秒 模型隐藏

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections;
public class Inspection : MonoBehaviour
{
public GameObject point;//当前射中的物体

public Vector3 hitInfoPos;

private LineRenderer lineRenderer;

public static InspectionEvent InstanceEvent = new InspectionEvent();
public static Inspection Instance;
public bool IsRayHint = false;

public Camera cameras;
public int index;

public float dis;

public float times;

void Awake()
{
    Instance = this;
}

private void OnRayExit(GameObject collider)
{
    // Debug.Log(collider.name);
}

//进入
private void OnRayEnter(GameObject collider)
{

    // Debug.Log(collider.name);
}

void Start()
{
    Inspection.InstanceEvent.OnRayEnter += OnRayEnter;
    Inspection.InstanceEvent.OnRayExit += OnRayExit;
    index = 0;
    times = 0;
}

public List<GameObject> hideObjs = new List<GameObject>();

public void RecoverHideObjs()
{
    for (int i = 0; i < hideObjs.Count; i++)
    {
        //hideObjs[i].transform.GetComponent<MeshRenderer>().enabled = true;
        hideObjs[i].transform.gameObject.SetActive(true);
    }
    hideObjs.Clear();
}

void LateUpdate()
{
    // 是否基于用户的需求将光线投射到世界上
    // 头部位置和方向。
    if (Input.GetMouseButton(0) || Input.touchCount > 0)
    {
        var headPosition = transform.position;
        var gazeDirection = transform.forward;
        Vector3 inputPos = Vector3.zero;

#if UNITY_EDITOR
inputPos = Input.mousePosition;
#else
inputPos = Input.GetTouch(0).position;

#endif
Ray ray = cameras.ScreenPointToRay(inputPos);
RaycastHit hit;

       // Debug.Log(cameras.ScreenToViewportPoint(inputPos));
        
        LayerMask mask = 1 << 0 | 1 << 1;

            
            if (Physics.Raycast(ray, out hit, dis, mask.value))
            {
                IsRayHint = true;
                hitInfoPos = hit.point;
                //Debug.Log(" 当前鼠标点击物体的名字是————" + hit.collider.name);

                times += Time.deltaTime;
                if (times >= 3.5)
                {
                    //hit.transform.GetComponent<MeshRenderer>().enabled = false;
                    hit.transform.gameObject.SetActive(false);
                    Layer2.instance.modelshow_btn.gameObject.SetActive(true);
                    hideObjs.Add(hit.transform.gameObject);
                    times = 0;
                }

                if(point!=null)
                {
                    if (Modulontroller.instance == null)
                    {
                        return;
                    }
                    if (Modulontroller.instance.type1 >= 0 && Modulontroller.instance.type1 < LoadXml.instance.data_list.Count)
                    {
                        for (int i = 0; i < LoadXml.instance.data_list[Modulontroller.instance.type1].datas.Count; i++)
                        {
                            for (int j = 0; j < LoadXml.instance.data_list[Modulontroller.instance.type1].datas[i].datas.Count; j++)
                            {
                                if (LoadXml.instance.data_list[Modulontroller.instance.type1].datas[i].datas[j].Model != null)
                                {
                                    modelControl.Instance.SetHighlighter(LoadXml.instance.data_list[Modulontroller.instance.type1].datas[i].datas[j].Model);
                                    if (LoadXml.instance.data_list[Modulontroller.instance.type1].datas[i].datas[j].Model.name.Contains(hit.transform.name) ||
                                        hit.transform.name.Contains(LoadXml.instance.data_list[Modulontroller.instance.type1].datas[i].datas[j].Model.name))
                                    {
                                        Debug.Log("2222" + (LoadXml.instance.data_list[Modulontroller.instance.type1].datas[i].datas[j].Model.name) + (hit.transform.name));
                                        Layer2.instance.system_obj.SetActive(false);
                                        Layer2.instance.action_obj.SetActive(true);

                                        Layer2.instance.text_hight.text = LoadXml.instance.data_list[Modulontroller.instance.type1].datas[i].datas[j].text;
                                    }
                                }
                            }
                        }
                    }
                }






                if (hit.collider.transform == null)
                {
                    return;
                }
                if (point != hit.collider.transform.gameObject)
                {
                    //Debug.Log(" 当前鼠标点击物体的名字是————" + hit.collider.name);
                    //前射线照射物体退出
                    if (point != null)
                    {
                        if (InstanceEvent != null)
                        {
                            InstanceEvent.OnRayExit(point);
                        }
                    }
                    point = hit.collider.transform.gameObject;
                    if (InstanceEvent.OnRayEnter != null)
                    {
                        InstanceEvent.OnRayEnter(point);
                    }
                }
            }
            else
            {
                if (IsRayHint)
                {
                    if (InstanceEvent.OnRayExit != null)
                    {
                        InstanceEvent.OnRayExit(point);
                    }
                    point = null;
                }
                IsRayHint = false;
            }
            
        }
    else
    {
        times = 0;
    }
}






public class InspectionEvent
{
    public delegate void Ray(GameObject collider);
    //当射线进入
    public Ray OnRayEnter;
    //当射线退出
    public Ray OnRayExit;


}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值