277 lines
10 KiB
C#
277 lines
10 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using DG.Tweening;
|
|
using System.Linq;
|
|
using System;
|
|
using UnityEngine.EventSystems;
|
|
using static UnityEditor.Progress;
|
|
|
|
/// <summary>
|
|
/// 柜子点击脚本
|
|
/// </summary>
|
|
public class ClickEvent : MonoBehaviour
|
|
{
|
|
/// <summary>
|
|
/// 物体是否透明
|
|
/// </summary>
|
|
public bool isTransparentGlow = false;
|
|
|
|
/// <summary>
|
|
/// 放大状态
|
|
/// </summary>
|
|
public bool magnifyState = false;
|
|
|
|
private List<MeshRenderer> meshRenderers;
|
|
/// <summary>
|
|
/// 自身的
|
|
/// </summary>
|
|
private List<Material> materials;
|
|
/// <summary>
|
|
/// 复制出来的
|
|
/// </summary>
|
|
public List<Material> empty = new List<Material>();
|
|
public Shader TransparentGlow_Shader;
|
|
|
|
/// <summary>
|
|
/// 坐标偏移值
|
|
/// </summary>
|
|
[SerializeField] Vector3 yiDong_pos = new Vector3(1.5f, 0.5f, 0);
|
|
/// <summary>
|
|
/// 角度偏移值
|
|
/// </summary>
|
|
[SerializeField] Vector3 fangDa_rot = new Vector3(0, -90, 0);
|
|
|
|
public List<MeshRenderer> MeshRenderers
|
|
{
|
|
get
|
|
{
|
|
if (meshRenderers == null)
|
|
meshRenderers = GetComponentsInChildren<MeshRenderer>().ToList();
|
|
return meshRenderers;
|
|
}
|
|
}
|
|
|
|
public List<Material> Materials
|
|
{
|
|
get
|
|
{
|
|
if (materials == null)
|
|
{
|
|
materials = new List<Material>();
|
|
MeshRenderers.ForEach(render =>
|
|
{
|
|
Array.ForEach(render.materials, (_mat) =>
|
|
{
|
|
if (!materials.Contains(_mat))
|
|
{
|
|
materials.Add(_mat);
|
|
Material _emp = new Material(_mat);
|
|
empty.Add(_emp);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
return materials;
|
|
}
|
|
}
|
|
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
TransparentGlow_Shader = Resources.Load<Shader>("TransparentGlow");
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
if (!EventSystem.current.IsPointerOverGameObject() && Input.GetMouseButtonDown(0) && TransparentGlowManage.Inst.gameObject.activeSelf)
|
|
{
|
|
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
|
|
RaycastHit hit;
|
|
|
|
//不虚化
|
|
if (Physics.Raycast(ray, out hit, 100, 1 << 6))
|
|
{
|
|
//放大
|
|
if (hit.collider.gameObject == gameObject && !magnifyState)
|
|
{
|
|
GameManager.Inst.nowDeviceID = hit.collider.gameObject.GetComponent<DeviceQuery>().deviceList.id;
|
|
ExtendedFlycam.Inst.init_mainCamera_rot();
|
|
// 物体被点击
|
|
Debug.Log("放大点击到" + gameObject.name);
|
|
Renderer renderer = null;
|
|
try
|
|
{
|
|
renderer = transform.Find(transform.name).GetComponent<Renderer>();
|
|
|
|
}
|
|
catch (Exception)
|
|
{
|
|
if (!renderer) renderer = transform.GetComponentInChildren<Renderer>();
|
|
|
|
}
|
|
Array.ForEach(GameObject.FindObjectsOfType<ClickEvent>(), itme =>
|
|
{
|
|
itme.magnifyState = false;
|
|
});
|
|
magnifyState = true;
|
|
TransparentGlowManage.Inst.is_magnify = true;
|
|
Vector3 center = renderer.bounds.center;
|
|
Vector3 targetPosition = new Vector3(center.x + yiDong_pos.x, center.y + yiDong_pos.y, center.z + yiDong_pos.z);
|
|
Quaternion targetRotation = Quaternion.Euler(fangDa_rot);
|
|
Camera.main.transform.DOMove(targetPosition, 1f);
|
|
Camera.main.transform.DORotateQuaternion(targetRotation, 1f).OnComplete(() =>
|
|
{
|
|
//更新相机初始旋转角度
|
|
ExtendedFlycam.Inst.initialRotationEulerAngles = Camera.main.transform.localEulerAngles;
|
|
});
|
|
}
|
|
//缩小
|
|
else if (hit.collider.gameObject == gameObject && magnifyState)
|
|
{
|
|
GameManager.Inst.nowDeviceID = null;
|
|
magnifyState = false;
|
|
TransparentGlowManage.Inst.is_magnify = false;
|
|
ExtendedFlycam.Inst.init_mainCamera_rot();
|
|
// 物体被点击
|
|
Debug.Log("缩小点击到" + gameObject.name);
|
|
Camera.main.transform.DOMove(TransparentGlowManage.Inst.MainCamera_pos, 1f);
|
|
Camera.main.transform.DORotateQuaternion(Quaternion.Euler(TransparentGlowManage.Inst.MainCamera_rot), 1f).OnComplete(() =>
|
|
{
|
|
//更新相机初始旋转角度
|
|
ExtendedFlycam.Inst.initialRotationEulerAngles = Camera.main.transform.localEulerAngles;
|
|
});
|
|
}
|
|
}
|
|
//点击模型(配置)
|
|
else if (Physics.Raycast(ray, out hit, 100, 1 << 8))
|
|
{
|
|
|
|
}
|
|
//虚化非点击
|
|
else if (Physics.Raycast(ray, out hit, 100, 1 << 6))
|
|
{
|
|
//点到,本身透明,场景透明模式
|
|
if (hit.collider.gameObject == gameObject && isTransparentGlow && TransparentGlowManage.Inst.is_transparency)
|
|
{
|
|
ExtendedFlycam.Inst.init_mainCamera_rot();
|
|
isTransparentGlow = true;
|
|
// 物体被点击
|
|
Debug.Log("放大点击到" + gameObject.name);
|
|
//Renderer renderer = GetComponent<Renderer>();
|
|
Renderer renderer = null;
|
|
//if (transform.childCount != 0)
|
|
// renderer = transform.Find(transform.name).GetComponent<Renderer>();
|
|
//else
|
|
// renderer = transform.GetComponent<Renderer>();
|
|
try
|
|
{
|
|
renderer = transform.Find(transform.name).GetComponent<Renderer>();
|
|
}
|
|
catch (Exception)
|
|
{
|
|
if (!renderer) renderer = transform.GetComponentInChildren<Renderer>();
|
|
}
|
|
|
|
Vector3 center = renderer.bounds.center;
|
|
Vector3 targetPosition = new Vector3(center.x + yiDong_pos.x, center.y + yiDong_pos.y, center.z + yiDong_pos.z);
|
|
Quaternion targetRotation = Quaternion.Euler(fangDa_rot);
|
|
Camera.main.transform.DOMove(targetPosition, 1f);
|
|
Camera.main.transform.DORotateQuaternion(targetRotation, 1f).OnComplete(() =>
|
|
{
|
|
//更新相机初始旋转角度
|
|
ExtendedFlycam.Inst.initialRotationEulerAngles = Camera.main.transform.localEulerAngles;
|
|
});
|
|
|
|
|
|
TransparentGlowManage.Inst.transparencyALL();
|
|
F2();
|
|
}
|
|
//点到,本身非透明,场景透明模式
|
|
else if (hit.collider.gameObject == gameObject && !isTransparentGlow && TransparentGlowManage.Inst.is_transparency)
|
|
{
|
|
ExtendedFlycam.Inst.init_mainCamera_rot();
|
|
isTransparentGlow = false;
|
|
// 物体被点击
|
|
Debug.Log("缩小点击到" + gameObject.name);
|
|
Camera.main.transform.DOMove(TransparentGlowManage.Inst.MainCamera_pos, 1f);
|
|
Camera.main.transform.DORotateQuaternion(Quaternion.Euler(TransparentGlowManage.Inst.MainCamera_rot), 1f).OnComplete(() =>
|
|
{
|
|
//更新相机初始旋转角度
|
|
ExtendedFlycam.Inst.initialRotationEulerAngles = Camera.main.transform.localEulerAngles;
|
|
});
|
|
|
|
|
|
TransparentGlowManage.Inst.renewALL();
|
|
}
|
|
|
|
|
|
|
|
//点到,本身非透明,场景非透明模式
|
|
else if (hit.collider.gameObject == gameObject && !isTransparentGlow && !TransparentGlowManage.Inst.is_transparency)
|
|
{
|
|
ExtendedFlycam.Inst.init_mainCamera_rot();
|
|
isTransparentGlow = true;
|
|
// 物体被点击
|
|
Debug.Log("放大点击到" + gameObject.name);
|
|
//Renderer renderer = GetComponent<Renderer>();
|
|
Renderer renderer = null;
|
|
//if (transform.childCount != 0)
|
|
// renderer = transform.Find(transform.name).GetComponent<Renderer>();
|
|
//else
|
|
// renderer = transform.GetComponent<Renderer>();
|
|
try
|
|
{
|
|
renderer = transform.Find(transform.name).GetComponent<Renderer>();
|
|
}
|
|
catch (Exception)
|
|
{
|
|
if (!renderer) renderer = transform.GetComponentInChildren<Renderer>();
|
|
}
|
|
Vector3 center = renderer.bounds.center;
|
|
Vector3 targetPosition = new Vector3(center.x + yiDong_pos.x, center.y + yiDong_pos.y, center.z + yiDong_pos.z); Quaternion targetRotation = Quaternion.Euler(fangDa_rot);
|
|
Camera.main.transform.DOMove(targetPosition, 1f);
|
|
Camera.main.transform.DORotateQuaternion(targetRotation, 1f).OnComplete(() =>
|
|
{
|
|
//更新相机初始旋转角度
|
|
ExtendedFlycam.Inst.initialRotationEulerAngles = Camera.main.transform.localEulerAngles;
|
|
});
|
|
|
|
|
|
TransparentGlowManage.Inst.transparencyALL();
|
|
F2();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 本身透明
|
|
/// </summary>
|
|
[ContextMenu("透明")]
|
|
public void F1()
|
|
{
|
|
isTransparentGlow = true;
|
|
for (int i = 0; i < Materials.Count; i++)
|
|
{
|
|
Materials[i].shader = TransparentGlow_Shader;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 本身恢复原材质
|
|
/// </summary>
|
|
[ContextMenu("恢复原材质")]
|
|
public void F2()
|
|
{
|
|
isTransparentGlow = false;
|
|
for (int i = 0; i < Materials.Count; i++)
|
|
{
|
|
Materials[i].shader = empty[i].shader;
|
|
}
|
|
}
|
|
|
|
}
|