using System.Collections; using System.Collections.Generic; using UnityEngine; using System; using UnityEngine.EventSystems; public class Optional : MonoBehaviour { /// /// 高亮MR /// MeshRenderer[] highlightRenderers; /// /// 对应材料、工器具 /// public ErrorModelType modelType; /// /// 是否为缺陷 /// public bool isIssue = true; /// /// 故障类型 /// public FaultType faultType; /// /// 是否为蓝图模式 /// public bool isBlueprint = false; //蓝图模式,将物体摆放至该物体下 /// /// 是否显示子物体下隐藏mesh的高亮 /// public bool highlightWithHideChilds = true; /// /// 是否为自身材质 /// public bool SelfMR = true; /// /// 缺陷操作物体 /// public List IssueObjects = new List(); /// /// 是否初始化缺陷 /// public bool issueInit; /// /// 缺陷是否解决 /// public bool IssueSolved; /// /// 类型名称 /// public string typeName; /// /// 细节 /// public string toolDetails; /// /// 是否在高亮 /// bool highLighting; /// /// 鼠标是否在该物体上 /// bool isHovering; /// /// 非缺陷 是否已有物体 /// public bool hasObject; /// /// 蓝图物体 /// public GameObject blueprintObject; /// /// 缓存下发数据 /// public AttrData data; void Start() { if (!isIssue) //禁止合闸警示牌 { IssueSolved = false; hasObject = false; typeName = GetComponent().modelType.ToString(); } } private void FixedUpdate() { if (highLighting) { if (Camera.main == null || Vector3.Distance(Camera.main.transform.position, transform.position) > SelectionManage.OperableDistance) { HighlightOff(); } } if (isHovering && !highLighting) { if (Camera.main != null && Vector3.Distance(Camera.main.transform.position, transform.position) < SelectionManage.OperableDistance) { HighlightOn(); } } } /// /// 缺陷初始化 /// /// /// 是否为缺失 public void IssueInit(AttrData data/*DefectDetails details*/,Texture2D texture2D = null,bool inactave = true) { var eb = GetComponent(); if (eb != null) modelType = eb.modelType; if (faultType.Equals(FaultType.Malposition)) { } else if (faultType.Equals(FaultType.Error)) { var _target = gameObject.GetChildByNameContains("_错误", true); if (inactave) { _target.SetActive(false); } else { if (texture2D != null) _target.SetMeshRendererActive(true, texture2D); _target.SetActive(true); } var _true = gameObject.GetChildByNameContains("_正确", true); _true.SetActive(false); } else { if (SelfMR) { gameObject.SetMeshRendererActive(false); } else { IssueObjects.ForEach(issue => { issue.SetMeshRendererActive(false); }); } } IssueSolved = false; hasObject = false; issueInit = true; this.data = data; } /// /// 正常初始化 /// public void NormalInit() { issueInit = false; var normal = transform.Find("正确"); if (normal != null) { if (normal.childCount == 1) { normal.GetChild(0).gameObject.SetActive(true); } else if (normal.childCount > 1) { var area = ScenesRecorder.instance == null ? normal.Find("敏行") : normal.Find(ScenesRecorder.AreaName); if (area != null) { gameObject.SetMeshRendererActive(false); area.gameObject.SetActive(true); } else Debug.Log("未找到 :" + transform.name + "_" + normal.name + "_" + ScenesRecorder.AreaName + " !"); } } else { switch (faultType) { case FaultType.Defect: if (SelfMR) { gameObject.SetMeshRendererActive(true); } else { IssueObjects.ForEach(issue => { issue.SetMeshRendererActive(true); }); } break; case FaultType.Damage: Array.ForEach(GetComponentsInChildren(), (tra) => { if (tra.name.Contains("_正确")) { tra.gameObject.SetMeshRendererActive(true); } else if (tra.name.Contains("_错误")) { tra.gameObject.SetMeshRendererActive(false); } }); break; default: break; } } } /// /// 正常初始化 附带贴图 /// public void NormalInit(Texture2D texture2D) { issueInit = false; var normal = transform.Find("正确"); if (normal != null) { if (normal.childCount == 1) { normal.GetChild(0).gameObject.SetActive(true); } else if (normal.childCount > 1) { var area = ScenesRecorder.instance == null ? normal.Find("敏行") : normal.Find(ScenesRecorder.AreaName); if (area != null) { area.gameObject.SetMeshRendererActive(true, texture2D); area.gameObject.SetActive(true); } else Debug.Log("未找到 :" + transform.name + "_" + normal.name + "_" + ScenesRecorder.AreaName + " !"); } } else { switch (faultType) { case FaultType.Defect: if (SelfMR) { gameObject.SetMeshRendererActive(true); } else { IssueObjects.ForEach(issue => { issue.SetMeshRendererActive(true); }); } break; case FaultType.Damage: Array.ForEach(GetComponentsInChildren(), (tra) => { if (tra.name.Contains("_正确")) { tra.gameObject.SetMeshRendererActive(true); } else if (tra.name.Contains("_错误")) { tra.gameObject.SetMeshRendererActive(false); } }); break; case FaultType.Error: if (texture2D != null) { Array.ForEach(GetComponentsInChildren(true), (tra) => { if (tra.name.Contains("_正确")&& !tra.Equals(this.transform)) { tra.gameObject.SetActive(true); tra.gameObject.SetMeshRendererActive(true,texture2D); } else if (tra.name.Contains("_错误") && !tra.Equals(this.transform)) { tra.gameObject.SetActive(false); tra.gameObject.SetMeshRendererActive(false); } }); } break; default: break; } } } /// /// 直接处理缺陷 显示 /// public void SolveIssue() { switch (faultType) { case FaultType.Defect: if (SelfMR) { gameObject.SetMeshRendererActive(true); } else { IssueObjects.ForEach(issue => { issue.SetMeshRendererActive(true); }); } break; case FaultType.Damage: Array.ForEach(GetComponentsInChildren(), (tra) => { //场景物体需整理结构 if (tra.name.Contains("_正确")) { tra.gameObject.SetMeshRendererActive(true); } else if (tra.name.Contains("_错误")) { tra.gameObject.SetMeshRendererActive(false); } }); break; case FaultType.Error: break; default: break; } if (isIssue) IssueSolved = true; else hasObject = true; // 直接处理 上传处理结果 Request.SendFaultData(data, ""); } /// /// 非直接处理缺陷 显示并替换贴图 /// public void SolveIssue(Texture _tex) { if (SelfMR) { gameObject.SetMeshRendererActive(true, _tex); } else { if(faultType.Equals(FaultType.Error)) { Array.ForEach(GetComponentsInChildren(true), (tra) => { if (tra.name.Contains("_错误") && !tra.Equals(this.transform)) { tra.gameObject.SetActive(false); } }); } for (int i = 0; i < IssueObjects.Count; i++) { if (i == 0) { IssueObjects[i].SetMeshRendererActive(true, _tex); } else { IssueObjects[i].SetMeshRendererActive(true); } } } IssueSolved = true; // 直接处理 并 上传贴图类型 data.Details.CurreSelectDetail = _tex.name; Request.SendFaultData(data, ""); } /// /// 蓝图模式修复缺陷 /// /// public void SolveIssue(GameObject _blueprint) { blueprintObject = _blueprint; } private void OnMouseEnter() { MouseEnter(); #region //if (EventSystem.current.IsPointerOverGameObject()) return; //if (Camera.main == null) return; //if (typeName.Equals("禁止合闸警示牌")) //{ // if (!hasObject) // if (!SelectionManage.Hand(this)) return; // isHovering = true; //} //else //{ // if (ScenesRecorder.instance != null && !ScenesRecorder.OperationPhase.Equals(OperationPhase.Troubleshooting)) return; // if (isIssue) // { // //是否处理缺陷后禁止再次操作 // //if (IssueSolved) return; // if (!isBlueprint && !SelectionManage.Hand(this) || isBlueprint && blueprintObject == null) return; //非蓝图下未持有对应物体 或 蓝图下未安装到位 不予高亮 // isHovering = true; // } // else // { // if (!hasObject) // if (!SelectionManage.Hand(this)) return; // isHovering = true; // } //} #endregion } private void OnMouseDown() { MouseDown(); #region //if (EventSystem.current.IsPointerOverGameObject()) return; //if (!highLighting) return; //if (typeName.Equals("禁止合闸警示牌")) //{ // //已有,则取出 // if (hasObject) // { // //取出放回背包 // PackagePanel.instacne.AddToolIntoPackageFromHand(this.IssueObjects[0].GetComponentInChildren(true)); // IssueInit(); // } // else // hasObject = SelectionManage.instance.UpdateSelection(this, highlightRenderers); //} //else //{ // if (ScenesRecorder.instance != null && !ScenesRecorder.OperationPhase.Equals(OperationPhase.Troubleshooting)) return; // if (isIssue) // { // //是否处理缺陷后禁止再次操作 // //if (IssueSolved) return; // if (isBlueprint) // { // Debug.Log("取回蓝图[" + blueprintObject.name + "]"); // } // else // { // IssueSolved = SelectionManage.instance.UpdateSelection(this, highlightRenderers); // //解决缺陷后取消高亮 // if (IssueSolved) HighlightOff(); // } // } // else // { // //已有,则取出 // if (hasObject) // { // //取出放回背包 // PackagePanel.instacne.AddToolIntoPackageFromHand(this.IssueObjects[0].GetComponentInChildren(true)); // IssueInit(); // } // else // hasObject = SelectionManage.instance.UpdateSelection(this, highlightRenderers); // } //} #endregion } private void OnMouseExit() { MouseExit(); #region //isHovering = false; //HighlightOff(); #endregion } /// /// 鼠标进入执行 /// public void MouseEnter() { if (EventUtilities.IsOverUI) return; if (Camera.main == null) return; if (typeName.Equals("禁止合闸警示牌")) { if (!hasObject) if (!SelectionManage.Hand(this)) return; isHovering = true; } else { if (!SelectionManage.CanOperate()) return; if (faultType.Equals(FaultType.Malposition)) { if (!issueInit) return; isHovering = true; } else { if (isIssue) { //if (IssueSolved) return; if (!issueInit) return; if (!isBlueprint && !SelectionManage.Hand(this) || isBlueprint && blueprintObject == null) return; //非蓝图下未持有对应物体 或 蓝图下未安装到位 不予高亮 isHovering = true; } else { if (!hasObject) if (!SelectionManage.Hand(this)) return; isHovering = true; } } } } /// /// 鼠标点击执行 /// public void MouseDown() { if (EventUtilities.IsOverUI) return; if (!highLighting) return; if (typeName.Equals("禁止合闸警示牌")) { //已有,则取出 if (hasObject) { //取出放回背包 ToolBase tool= ToolHolder.instance.GetCurrentTool(); if (tool) { if (tool.toolName == "禁止合闸警示牌") { PackagePanel.instacne.AddToolIntoPackageFromHand(tool); } } //PackagePanel.instacne.AddToolIntoPackageFromHand(this.IssueObjects[0].GetComponentInChildren(true)); IssueInit(null); } else hasObject = SelectionManage.instance.UpdateSelection(this, highlightRenderers); } else { if (!SelectionManage.CanOperate()) return; if (faultType.Equals(FaultType.Malposition)) { var right = gameObject.GetChildsByNameContains("_正确"); var fault = gameObject.GetChildsByNameContains("_错误"); right.ForEach(x => x.SetMeshRendererActive(true)); fault.ForEach(x => x.SetMeshRendererActive(false)); IssueSolved = true; HighlightOff(); //上传数据 Request.SendFaultData(data, ""); } else { if (isIssue) { //if (IssueSolved) return; if (isBlueprint) { Debug.Log("取回蓝图[" + blueprintObject.name + "]"); } else { IssueSolved = SelectionManage.instance.UpdateSelection(this, highlightRenderers); //解决缺陷后取消高亮 if (IssueSolved) HighlightOff(); } } else { //已有,则取出 if (hasObject) { //取出放回背包 PackagePanel.instacne.AddToolIntoPackageFromHand(this.IssueObjects[0].GetComponentInChildren(true)); IssueInit(null); } else hasObject = SelectionManage.instance.UpdateSelection(this, highlightRenderers); } } } } /// /// 鼠标退出执行 /// public void MouseExit() { isHovering = false; HighlightOff(); } /// /// 打开高亮 /// void HighlightOn() { highLighting = true; if (isBlueprint || !highlightWithHideChilds) HighLight_VR.instance.SetHight(this.transform); else HighLight_VR.instance.SetHight(this.transform, true, ref highlightRenderers); } /// /// 关闭高亮 /// void HighlightOff() { highLighting = false; HighLight_VR.instance.CloseHight(); } /// /// 获取位置 将手上的物体放入该位置 /// /// public Transform GetTransform() { return SelfMR ? transform : (IssueObjects.Count > 0 ? (IssueObjects.Count > 1 ? transform : IssueObjects[0].transform) : transform); } }