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 hideObjs = new List();

public void RecoverHideObjs()
{
    for (int i = 0; i < hideObjs.Count; i++)
    {
        //hideObjs[i].transform.GetComponent().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().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;


}

}

你可能感兴趣的:(unity,音视频,ui,c#)