using DefaultNamespace.ProcessMode; using DG.Tweening; using MotionFramework; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; /// /// 螺丝刀 /// public class Screwdriver : MonoBehaviour { /// /// 螺丝刀坐标 /// public Transform ScrewdriverTrans; /// /// 接线盒右边螺丝 /// public GameObject ScrewR; /// /// 接线盒左边螺丝 /// public GameObject ScrewL; /// /// 集中器左侧螺丝 /// public GameObject LconcentratorScrew; /// /// 集中器右侧螺丝 /// public GameObject RconcentratorScrew; /// /// 集线器外壳螺丝和封印 /// public GameObject[] concentratorScrew; /// /// 旧集中器下端螺丝 /// public GameObject[] ScrewObj; /// /// 新集中器下端螺丝 /// public GameObject[] NewScrewObj; /// /// 集线盒电线动画 /// public SkinnedMeshRenderer ElectricWireSMR; /// /// 电线碰撞 /// public BoxCollider ElectricWireBcr; /// /// 集中器盖子 /// public GameObject HubCoverObj; /// /// 集中器后边螺丝 /// public GameObject[] ConcentratorScrewObj; /// /// 更换下的旧的集中器 /// public Transform ConcentratorObj; /// /// 电线前段胶带 /// public Material TapeMar; /// /// 螺丝刀钳拧开开每个螺丝位置 /// public Transform[] OpenScrewScrewdriverTrans; public InteractiveObjects[] interactiveObjects; /// /// 拆完旧集中器下端螺丝后 /// bool issplit = true; /// /// 螺丝刀的原始位置 /// private Vector3 originalPosition; /// /// 螺丝刀的原始旋转 /// private Quaternion originalRotation; /// /// 工具解绑的父物体 /// public GameObject PlayerCamera; /// /// 旧集中器 /// public GameObject OldConcentrator; [Header("集中器盖子")] /// /// 集中器盖子 /// public GameObject Cover; [Header("新集中器盖子上左右两颗螺丝")] /// /// 新集中器盖子上左右两颗螺丝 /// public GameObject[] screw; [Header("是否播放集线器螺丝拆完后电线动画")] /// /// 是否播放集线器螺丝拆完后电线动画 /// public static bool isPlayAni = true; bool Ischaiwan = true; // Start is called before the first frame update void Start() { Init(); } /// /// 初始化 /// void Init() { ElectricWireBcr.enabled = false; //ElectricWireSMR.SetBlendShapeWeight(0, 0); //ElectricWireSMR.SetBlendShapeWeight(1, 0); //ElectricWireSMR.SetBlendShapeWeight(2, 0); //ElectricWireSMR.SetBlendShapeWeight(3, 0); //ElectricWireSMR.SetBlendShapeWeight(4, 0); //ElectricWireSMR.SetBlendShapeWeight(5, 0); TapeMar.SetFloat("_step_p1", 0); TapeMar.SetFloat("_step_p2", 0); TapeMar.SetFloat("_step_p3", 0); TapeMar.SetFloat("_step_p4", 0); TapeMar.SetFloat("_step_p5", 0); TapeMar.SetFloat("_step_p6", 0); //TapeMar.SetFloat("_step_p1", 1); originalPosition = ScrewdriverTrans.transform.localPosition; originalRotation = ScrewdriverTrans.transform.localRotation; } float weight; // Update is called once per frame void Update() { //if (EventSystem.current.IsPointerOverGameObject()) // return; if (!gameObject.activeSelf) return; if (Ischaiwan == true) { chaiwan(); } if (Input.GetMouseButtonDown(0)) {//Camera.transform.forward Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; bool raycast = Physics.Raycast(ray, out hit); if (raycast) { if (TooslManager.instance.currentTool != null && TooslManager.instance.currentTool.name == "螺丝刀") { if (hit.collider.gameObject.name == "盖_固定螺丝_Rup") { if (MotionEngine.GetModule().HandleClick(hit.collider.gameObject.name)) { ScrewdriverTrans.gameObject.transform.SetParent(null); StartCoroutine(Rscrew()); Debug.Log(hit.collider.gameObject.name); } } if (hit.collider.gameObject.name == "固定螺丝_Ldown") { if (MotionEngine.GetModule().HandleClick(hit.collider.gameObject.name)) { ScrewdriverTrans.gameObject.transform.SetParent(null); StartCoroutine(Lscrew()); Debug.Log(hit.collider.gameObject.name); } } if (hit.collider.gameObject.name == "壳固定螺丝_R") { if (MotionEngine.GetModule().HandleClick(hit.collider.gameObject.name)) { ScrewdriverTrans.gameObject.transform.SetParent(null); Debug.Log(hit.collider.gameObject.name); StartCoroutine(IERconcentratorScrew()); } } if (hit.collider.gameObject.name == "壳固定螺丝_L") { if (MotionEngine.GetModule().HandleClick(hit.collider.gameObject.name)) { ScrewdriverTrans.gameObject.transform.SetParent(null); Debug.Log(hit.collider.gameObject.name); StartCoroutine(IELconcentratorScrew()); } } if (hit.collider.gameObject.name == "固定螺丝_Rdown") { if (MotionEngine.GetModule().HandleClick(hit.collider.gameObject.name)) { ScrewdriverTrans.gameObject.transform.SetParent(null); Debug.Log(hit.collider.gameObject.name); StartCoroutine(right()); } } if (hit.collider.gameObject.name == "集电器固定螺丝_Ldown") { if (MotionEngine.GetModule().HandleClick(hit.collider.gameObject.name)) { ScrewdriverTrans.gameObject.transform.SetParent(null); Debug.Log(hit.collider.gameObject.name); StartCoroutine(left()); } } } for (int i = 0; i < ScrewObj.Length; i++) { if (ScrewObj[i] != null && hit.collider.gameObject.name == ScrewObj[i].name) { if (TooslManager.instance.currentTool != null && TooslManager.instance.currentTool.name == "螺丝刀") { if (MotionEngine.GetModule().HandleClick(hit.collider.gameObject.name)) { ScrewdriverTrans.gameObject.transform.SetPositionAndRotation(OpenScrewScrewdriverTrans[4].gameObject.transform.position, OpenScrewScrewdriverTrans[4].gameObject.transform.rotation); ScrewdriverTrans.gameObject.transform.SetParent(null); Debug.Log(hit.collider.gameObject.name); ScrewObj[i].gameObject.SetActive(false); switch (hit.collider.gameObject.name) { case "JZQ_螺丝001": ScrewObj[i].gameObject.SetActive(false); break; case "JZQ_螺丝0011": ScrewObj[i].gameObject.SetActive(false); break; case "JZQ_螺丝002": ScrewObj[i].gameObject.SetActive(false); break; case "JZQ_螺丝0022": ScrewObj[i].gameObject.SetActive(false); break; case "JZQ_螺丝003": ScrewObj[i].gameObject.SetActive(false); break; case "JZQ_螺丝0033": ScrewObj[i].gameObject.SetActive(false); break; case "JZQ_螺丝004": ScrewObj[i].gameObject.SetActive(false); break; case "JZQ_螺丝0044": ScrewObj[i].gameObject.SetActive(false); break; case "JZQ_螺丝005": ScrewObj[i].gameObject.SetActive(false); break; case "JZQ_螺丝006": ScrewObj[i].gameObject.SetActive(false); break; } } } } } if (AreAllObjectsHidden()) { isPlayAni = false; } } } if (ConcentratorScrew()) { StartCoroutine(IEConcentratorScrew()); } //集中器螺丝全隐藏后播放电线动画 if (AreAllObjectsHiddens() /*&& concentrator.isPlayAni*/) { StartCoroutine(ScrewdriverBack()); ElectricWireBcr.enabled = true; //拆完集中器下端螺丝后才能拆掉集中器后端四颗螺丝 for (int i = 0; i < ConcentratorScrewObj.Length; i++) { ConcentratorScrewObj[i].GetComponent().enabled = true; } //StartCoroutine(IEElectricWire()); // Debug.Log("所有螺丝都已隐藏"); } //集中器外壳上的封印和螺丝全拆完后盖子位移 if (AllHubOfHubs()) { HubCoverObj.gameObject.transform.DOLocalMove(new Vector3(0, 0.0167f, -0.2387f), 1); // Debug.Log("所有集线器盖子螺丝都已隐藏"); } } public void chaiwan() { if (!issplit) { //判断上下两颗螺丝是否全部取下 if (!ScrewObj[0].activeInHierarchy && !ScrewObj[1].activeInHierarchy) { StartCoroutine(Frist()); issplit = true; //Ischaiwan = false; } if (!ScrewObj[2].activeInHierarchy && !ScrewObj[3].activeInHierarchy) { StartCoroutine(Second()); issplit = true; } if (!ScrewObj[4].activeInHierarchy && !ScrewObj[5].activeInHierarchy) { StartCoroutine(Thrid()); issplit = true; } if (!ScrewObj[6].activeInHierarchy && !ScrewObj[7].activeInHierarchy) { StartCoroutine(Fourth()); issplit = true; } if (!ScrewObj[9].activeInHierarchy) { StartCoroutine(Sixth()); issplit = true; } if (!ScrewObj[8].activeInHierarchy) { StartCoroutine(Fivth()); issplit = true; } //if (!ScrewObj[0].activeInHierarchy && !ScrewObj[1].activeInHierarchy && !ScrewObj[2].activeInHierarchy && !ScrewObj[3].activeInHierarchy && !ScrewObj[4].activeInHierarchy && !ScrewObj[5].activeInHierarchy && !ScrewObj[6].activeInHierarchy && !ScrewObj[7].activeInHierarchy && !ScrewObj[8].activeInHierarchy) //{ // Ischaiwan = false; //} } //Ischaiwan = false; } /// /// 螺丝是否全部显示 /// int allls; /// /// 新集中器螺丝全部安装上 /// /// public bool AreAllObjectsHidden() { allls = 0; for (int i = 0; i < NewScrewObj.Length; i++) { if (NewScrewObj[i].gameObject.GetComponent().enabled == true) { //StartCoroutine(IEElectricWire()); allls++; } } if (allls >= NewScrewObj.Length) { StartCoroutine(IEElectricWireGai()); return true; } else { return false; } } /// /// 判断集中器螺丝下端螺丝 /// /// public bool AreAllObjectsHiddens() { foreach (GameObject obj in ScrewObj) { if (obj.activeInHierarchy) { return false; } } return true; } /// /// 判断集中器外壳上的封印和螺丝 /// /// public bool AllHubOfHubs() { foreach (GameObject obj in concentratorScrew) { if (obj.activeInHierarchy) { return false; } } return true; } /// /// 判断集中器后部四颗螺丝 /// /// public bool ConcentratorScrew() { foreach (GameObject obj in ConcentratorScrewObj) { if (obj != null && obj.activeInHierarchy) { issplit = false; return false; } } return true; } /// /// 集中器盖子后续动画 /// /// IEnumerator IEElectricWireGai() { ElectricWireBcr.enabled = false; yield return new WaitForSeconds(1f); Cover.gameObject.SetActive(true); yield return new WaitForSeconds(0.5f); Cover.gameObject.transform.DOLocalMove(new Vector3(0, 0.01672375f, -0.09654188f), 1f); yield return new WaitForSeconds(1.1f); screw[0].gameObject.SetActive(true); yield return new WaitForSeconds(1.0f); screw[1].gameObject.SetActive(true); } /// /// 螺丝刀返回动画 /// /// IEnumerator ScrewdriverBack() { ScrewdriverTrans.gameObject.transform.SetParent(PlayerCamera.transform); yield return new WaitForSeconds(0.5f); ScrewdriverTrans.gameObject.transform.localPosition = originalPosition; ScrewdriverTrans.gameObject.transform.localRotation = originalRotation; } /// /// 接线盒右边螺丝 /// /// IEnumerator Rscrew() { //ScrewdriverTrans.transform.DOLocalRotate(new Vector3(-19.741f, -113.296f, 90), 0.5f); //yield return new WaitForSeconds(0.6f); //ScrewdriverTrans.transform.DOLocalMove(new Vector3(3.2373f, 1.8549f, -1.244f), 1f); ScrewdriverTrans.transform.position = OpenScrewScrewdriverTrans[0].transform.position; ScrewdriverTrans.transform.eulerAngles = new Vector3(-6f, -86.466f, 90f); yield return new WaitForSeconds(1); //ScrewdriverTrans.transform.DOLocalRotate(new Vector3(-19.741f, -113.296f, -104.747f), 1f); //yield return new WaitForSeconds(1.2f); //ScrewdriverTrans.transform.DOLocalMove(new Vector3(3.191f, 1.831f, -1.158f), 1f); //yield return new WaitForSeconds(1.5f); ScrewR.transform.DOLocalMove(new Vector3(0.1316f, 0.036f, 0.0034f), 1f); yield return new WaitForSeconds(1.2f); StartCoroutine(ScrewdriverBack()); ScrewR.gameObject.SetActive(false); } /// /// 接线盒左边螺丝 /// /// IEnumerator Lscrew() { //ScrewdriverTrans.transform.DOLocalMove(new Vector3(3.2373f, 1.8284f, -1.3885f), 0.5f); //yield return new WaitForSeconds(0.6f); //ScrewdriverTrans.transform.DOLocalRotate(new Vector3(-19.741f, -113.296f, -104.747f), 1f); ScrewdriverTrans.transform.position = OpenScrewScrewdriverTrans[1].transform.position; ScrewdriverTrans.transform.eulerAngles = new Vector3(-6f, -86.466f, 90f); yield return new WaitForSeconds(1f); //ScrewdriverTrans.transform.DOLocalRotate(new Vector3(-19.741f, -113.296f, 90), 1f); //yield return new WaitForSeconds(1.5f); ScrewL.transform.DOLocalMove(new Vector3(-0.07357633f, -0.01137948f, -0.0366f), 1f); yield return new WaitForSeconds(1.2f); StartCoroutine(ScrewdriverBack()); ScrewL.gameObject.SetActive(false); } /// /// 集中器右侧螺丝 /// /// IEnumerator IERconcentratorScrew() { ScrewdriverTrans.transform.position = OpenScrewScrewdriverTrans[2].transform.position; ScrewdriverTrans.transform.eulerAngles = new Vector3(-6f, -86.466f, 90f); yield return new WaitForSeconds(1.5f); RconcentratorScrew.transform.DOLocalMove(new Vector3(0.1143f, 0.0216f, -0.1031f), 0.5f); yield return new WaitForSeconds(1f); StartCoroutine(ScrewdriverBack()); RconcentratorScrew.gameObject.SetActive(false); } /// /// 集中器左侧螺丝 /// /// IEnumerator IELconcentratorScrew() { //ScrewdriverTrans.transform.DOLocalMove(new Vector3(3.2669f, 2.0962f, -1.4616f), 1f); //yield return new WaitForSeconds(1.5f); //ScrewdriverTrans.transform.DOLocalRotate(new Vector3(-19.741f, -113.296f, -104.747f), 1f); //yield return new WaitForSeconds(1.5f); //ScrewdriverTrans.transform.DOLocalRotate(new Vector3(-19.741f, -113.296f, 90), 1f); ScrewdriverTrans.transform.position = OpenScrewScrewdriverTrans[3].transform.position; ScrewdriverTrans.transform.eulerAngles = new Vector3(-6f, -86.466f, 90f); yield return new WaitForSeconds(1.5f); LconcentratorScrew.transform.DOLocalMove(new Vector3(-0.1188f, 0.0216f, -0.1031f), 0.5f); yield return new WaitForSeconds(1f); StartCoroutine(ScrewdriverBack()); LconcentratorScrew.gameObject.SetActive(false); } /// /// 集线器螺丝拆完后电线动画 /// /// IEnumerator IEElectricWire(string name, GameObject[] Screw) { for (int a = 0; a < Screw.Length; a++) { if (Screw[a].gameObject.activeInHierarchy) { for (int i = 0; i <= 100; i++) { yield return new WaitForSeconds(0.001f); ElectricWireSMR.SetBlendShapeWeight(0, i); } } } } #region 电线动画 IEnumerator Frist() { //yield return new WaitForSeconds(1f); for (int i = 0; i <= 100; i++) { yield return new WaitForSeconds(0.001f); ElectricWireSMR.SetBlendShapeWeight(0, i); } } IEnumerator Second() { for (int i = 0; i <= 100; i++) { yield return new WaitForSeconds(0.002f); ElectricWireSMR.SetBlendShapeWeight(1, i); } } IEnumerator Thrid() { for (int i = 0; i <= 100; i++) { yield return new WaitForSeconds(0.002f); ElectricWireSMR.SetBlendShapeWeight(2, i); } } IEnumerator Fourth() { for (int i = 0; i <= 100; i++) { yield return new WaitForSeconds(0.002f); ElectricWireSMR.SetBlendShapeWeight(3, i); } } IEnumerator Fivth() { for (int i = 0; i <= 100; i++) { yield return new WaitForSeconds(0.002f); ElectricWireSMR.SetBlendShapeWeight(4, i); } } IEnumerator Sixth() { for (int i = 0; i <= 100; i++) { yield return new WaitForSeconds(0.002f); ElectricWireSMR.SetBlendShapeWeight(5, i); } } #endregion /// /// 第一个螺丝和第二个安装完成后卸下胶带和电线安装的动画 /// /// //IEnumerator TapeBackFrist() //{ // TapeMar.SetFloat("_step_p1", 0); // if (ElectricWireSMR.GetBlendShapeWeight(0) != 0 && ElectricWireSMR.GetBlendShapeWeight(0) == 100) // { // for (int i = 100; i >= 0; i--) // { // yield return new WaitForSeconds(0.01f); // ElectricWireSMR.SetBlendShapeWeight(0, i); // } // } //} //IEnumerator TapeBackSecond() //{ // TapeMar.SetFloat("_step_p2", 0); // if (ElectricWireSMR.GetBlendShapeWeight(1) != 0 && ElectricWireSMR.GetBlendShapeWeight(1) == 100) // { // for (int i = 100; i >= 0; i--) // { // yield return new WaitForSeconds(0.01f); // ElectricWireSMR.SetBlendShapeWeight(1, i); // } // } //} //IEnumerator TapeBackThird() //{ // TapeMar.SetFloat("_step_p3", 0); // if (ElectricWireSMR.GetBlendShapeWeight(2) != 0 && ElectricWireSMR.GetBlendShapeWeight(2) == 100) // { // for (int i = 100; i >= 0; i--) // { // yield return new WaitForSeconds(0.01f); // ElectricWireSMR.SetBlendShapeWeight(2, i); // } // } //} //IEnumerator TapeBackFourth() //{ // TapeMar.SetFloat("_step_p4", 0); // if (ElectricWireSMR.GetBlendShapeWeight(3) != 0 && ElectricWireSMR.GetBlendShapeWeight(3) == 100) // { // for (int i = 100; i >= 0; i--) // { // yield return new WaitForSeconds(0.01f); // ElectricWireSMR.SetBlendShapeWeight(3, i); // } // } //} //IEnumerator TapeBackFifth() //{ // TapeMar.SetFloat("_step_p5", 0); // if (ElectricWireSMR.GetBlendShapeWeight(4) != 0 && ElectricWireSMR.GetBlendShapeWeight(4) == 100) // { // for (int i = 100; i >= 0; i--) // { // yield return new WaitForSeconds(0.01f); // ElectricWireSMR.SetBlendShapeWeight(4, i); // } // } //} //IEnumerator TapeBackSixth() //{ // TapeMar.SetFloat("_step_p6", 0); // if (ElectricWireSMR.GetBlendShapeWeight(5) != 0 && ElectricWireSMR.GetBlendShapeWeight(5) == 100) // { // for (int i = 100; i >= 0; i--) // { // yield return new WaitForSeconds(0.01f); // ElectricWireSMR.SetBlendShapeWeight(5, i); // } // } //} /// /// 集中器背后螺丝全部拆完集中器动画 /// /// IEnumerator IEConcentratorScrew() { if (ConcentratorObj.gameObject != null) { ConcentratorObj.gameObject.transform.DOLocalMove(new Vector3(-0.2719f, 2.24768f, -0.0772f), 0.5f); yield return new WaitForSeconds(1f); //Destroy(ConcentratorObj.gameObject); OldConcentrator.gameObject.SetActive(false); } } /// /// 旧的集线器背后右边螺丝 /// /// IEnumerator right() { ScrewdriverTrans.transform.position = OpenScrewScrewdriverTrans[4].transform.position; ScrewdriverTrans.transform.eulerAngles = new Vector3(-6f, -86.466f, 90f); yield return new WaitForSeconds(0.5f); ConcentratorScrewObj[0].SetActive(false); //ConcentratorScrewObj[1].SetActive(false); } /// /// 旧的集线器背后左边螺丝 /// /// IEnumerator left() { ScrewdriverTrans.transform.position = OpenScrewScrewdriverTrans[4].transform.position; ScrewdriverTrans.transform.eulerAngles = new Vector3(-6f, -86.466f, 90f); yield return new WaitForSeconds(0.5f); ConcentratorScrewObj[1].SetActive(false); //ConcentratorScrewObj[2].SetActive(false); //ConcentratorScrewObj[3].SetActive(false); } }