public class HighlightControl : MonoBehaviour
{
public LayerMask targetingLayerMask = -1;//-1代表everything 如果公共属性是一个枚举值,会有选择
float targetingRayLength = Mathf.Infinity;//射线长度为无穷大
Camera cam;//不局限于主摄像机
void Start()
{
cam = GetComponent<Camera>();
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
TargetingRayCast();
}
}
public void TargetingRayCast()
{
Vector3 mp = Input.mousePosition;
Transform targetTransform = null;//设置空对象
if (cam != null)
{
RaycastHit hitInfo;
Ray ray = cam.ScreenPointToRay(new Vector3(mp.x, mp.y, 0f));
if (Physics.Raycast(ray.origin, ray.direction, out hitInfo, targetingRayLength, targetingLayerMask.value))
{
targetTransform = hitInfo.collider.transform;
}
}
if (targetTransform != null)
{
if (targetTransform.parent.name.Contains("MF"))
{
targetTransform.gameObject.AddComponent<ModelClick>();
ModelClick.instance.ClickModel(targetTransform.gameObject);
}
}
}
}
public class ModelClick : Singleton<ModelClick>
{
private GameObject objSlected; // 选中对象
public void ClickModel(GameObject obj)
{
if (objSlected != null)
{
objSlected.transform.GetChild(0).gameObject.SetActive(false);
}
objSlected = obj;
obj.transform.GetChild(0).gameObject.SetActive(true);
}
}