столкнулся с проблемой: создал скрипты, позволяющие выделять объекты на сцене. Все выделяет хорошо, НО не совсем так, как хотелось бы: мне надо, чтобы когда я выделил 1 объект, а затем 2, выделение с 1 снималось, а на 2 накладывалось. Решить эту проблему не могу уже долгое время, надеюсь на вашу помощь.
P.S. Выделение осуществляю посредством замены материалов.
Тексты кодов: [Первый код]
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;
}
}
}
[Второй код]
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;
}
}
}