I ran into a problem: I created scripts to select objects on the stage. Everything stands out well, BUT not quite as I would like: I need to, when I select 1 object, and then 2, the selection from 1 is removed, and 2 is superimposed. I cannot solve this problem for a long time, I hope for your help. PS Isolation is carried out through the replacement of materials.
Texts codes: [First code]
using UnityEngine; using System.Collections; using System; public class DragAndDrop : MonoBehaviour { public Transform[] original; public Transform[] mask; public float shift = 0.01f; public string respawnTag = "Respawn"; public static bool isOn; private Transform original_tmp; private Transform mask_tmp; private Vector3 curPos; public bool set; public SelectObj selectObj; void Start() { isOn = false; set = true; } public void SetMask(string id) { if (set == true) { foreach (Transform obj in original) { string name = obj.name.Split(new char[] { '_' }, StringSplitOptions.RemoveEmptyEntries)[0]; if (id.ToString() == name) { original_tmp = Instantiate(obj); original_tmp.gameObject.SetActive(false); } set = false; } foreach (Transform obj in mask) { string name = obj.name.Split(new char[] { '_' }, StringSplitOptions.RemoveEmptyEntries)[0]; if (id.ToString() == name) { mask_tmp = Instantiate(obj); } } } } void Update() { RaycastHit hit; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit)) { curPos = hit.point + hit.normal * shift; } if (mask_tmp) { mask_tmp.position = curPos; if (Input.GetAxis("Mouse ScrollWheel") > 0) { mask_tmp.localEulerAngles += new Vector3(0, 45, 0); } if (Input.GetAxis("Mouse ScrollWheel") < 0) { mask_tmp.localEulerAngles -= new Vector3(0, 45, 0); } if (Input.GetMouseButtonDown(0) && isOn) { original_tmp.gameObject.SetActive(true); original_tmp.position = mask_tmp.position; //original_tmp.position = new Vector3(-20,0.5f,-10); original_tmp.localEulerAngles = mask_tmp.localEulerAngles; original_tmp = null; isOn = false; Destroy(mask_tmp.gameObject); set = true; } else if (Input.GetMouseButtonDown(1)) { Destroy(original_tmp.gameObject); Destroy(mask_tmp.gameObject); set = true; } } Transform objectHit = hit.transform; if (Input.GetMouseButtonDown(0)&&(objectHit!=null)&&(objectHit.tag!="Respawn")) { GlobalVar.DontSelect = true; objectHit.tag = "Selected"; GlobalVar.StopSelect = false; return; } if (Input.GetMouseButtonDown(1)) { GlobalVar.StopSelect = true; } } }
[Second code]
using System.Collections; using System.Collections.Generic; using UnityEngine; public class SelectObj : MonoBehaviour { public Material matSelect; public Material matNormal; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { if ((gameObject.tag == "Selected")) { gameObject.GetComponentInChildren<MeshRenderer>().material = matSelect; gameObject.tag = "Untagged"; } if ((GlobalVar.StopSelect == true)) { gameObject.GetComponentInChildren<MeshRenderer>().material = matNormal; } if ((GlobalVar.DontSelect == true) && (gameObject.tag == "Untagged")) { gameObject.GetComponentInChildren<MeshRenderer>().material = matNormal; GlobalVar.DontSelect = false; } } }