HighLightingSystem插件下载链接:http://download.csdn.net/download/qq_34818497/9973320
代码如下:
/*
* 1.此脚本需要物体边缘发光的插件Highlighting System
*
* 2.此脚本用的边缘发光插件的版本:Highlighting System V4.2
*
* 3.给选中的物体手动添加脚本Highlighter Flashing组件即可.通过修改该组件下的Flashing Start Color以及Flashing End Color参数来改变颜色
*
* 4.与此脚本相关联的脚本: StaticFunction.cs
*/ using System.Collections;
usi
ng System.Collections.Generic;
using UnityEngine;
using HighlightingSystem;
public class HighlightingForObject : MonoBehaviour {
public LayerMask m_LayerMask; //只与该layer层的游戏物体发生碰撞
private GameObject m_ClickedObject = null;
public Color FlashingStartColor = Color.blue;
public Color FlashingEndColor = Color.yellow;
private void Awake()
{
//该插件的运用需要先给主相机添加如下脚本
GameObject camera = Camera.main.gameObject;
camera.AddComponent<CameraTargeting>();
camera.AddComponent<HighlightingRenderer>();
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0))
{
GameObject temp = StaticFunction.GetClickedObject(Input.mousePosition, m_LayerMask);
if (temp == null || m_ClickedObject != temp)
{
if(m_ClickedObject == null)
{
}
else if (m_ClickedObject.GetComponent<Highlighter>())
{
m_ClickedObject.GetComponent<Highlighter>().FlashingOff();
Destroy(m_ClickedObject.GetComponent<Highlighter>());
}
}
if (temp != null && temp.GetComponent<Highlighter>() == null)
{
//Debug.Log(temp.name);
temp.AddComponent<Highlighter>();
temp.GetComponent<Highlighter>().FlashingOn(FlashingStartColor, FlashingEndColor, 2.0f);
m_ClickedObject = temp;
}
}
}
}
上面用到的其他脚本StaticFunction.cs如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using UnityEngine.EventSystems;
public static class StaticFunction {
//射线碰撞获取碰撞点的坐标
public static Vector3 GetClickedPoint(Vector2 _ScreenPoint, string layerName = null)
{
Vector3 temp = Vector3.zero;
Ray ray = Camera.main.ScreenPointToRay(_ScreenPoint);
RaycastHit hit;
if (layerName == null)
{
if (Physics.Raycast(ray, out hit))
{
temp = hit.point;
}
}
else if (layerName != null)
{
if (Physics.Raycast(ray, out hit, 999.99f, 1 << LayerMask.NameToLayer(layerName)))
{
temp = hit.point;
}
//else
//{
// Debug.LogError("未找到此layer!!!");
//}
}
return temp;
}
public static Vector3 GetClickedPoint(Vector2 _ScreenPoint, LayerMask _layerMask)
{
Vector3 temp = Vector3.zero;
int layerMask = GetLayerFromLayerMask(_layerMask.value);
Ray ray = Camera.main.ScreenPointToRay(_ScreenPoint);
RaycastHit hit;
if (layerMask == -1)
{
if (Physics.Raycast(ray, out hit))
{
temp = hit.point;
}
}
else if (layerMask != -1)
{
if (Physics.Raycast(ray, out hit, 9999.99f, layerMask))
{
temp = hit.point;
}
//else
//{
// Debug.LogError("未找到此layer!!!");
//}
}
return temp;
}
//射线碰撞获取指定层的游戏物体
public static GameObject GetClickedObject(Vector2 _ScreenPoint, LayerMask _layerMask)
{
GameObject temp = null;
int layerMask = GetLayerFromLayerMask(_layerMask.value);
Ray ray = Camera.main.ScreenPointToRay(_ScreenPoint);
RaycastHit hit;
if (layerMask == -1)
{
if (Physics.Raycast(ray, out hit))
{
temp = hit.collider.gameObject;
}
}
else if (layerMask != -1)
{
if (Physics.Raycast(ray, out hit, 999.99f, layerMask))
{
temp = hit.collider.gameObject;
}
//else
//{
// Debug.LogError("未找到" + layerName +"层的物体!!!");
//}
}
//if(temp != null) Debug.Log("Clicked Object:" + temp.name);
return temp;
}
public static GameObject GetClickedObject(Vector2 _ScreenPoint, string layerName = null)
{
GameObject temp = null;
Ray ray = Camera.main.ScreenPointToRay(_ScreenPoint);
RaycastHit hit;
if (layerName == null)
{
if (Physics.Raycast(ray, out hit))
{
temp = hit.collider.gameObject;
}
}
else if (layerName != null)
{
if (Physics.Raycast(ray, out hit, 999.99f, 1 << LayerMask.NameToLayer(layerName)))
{
temp = hit.collider.gameObject;
}
//else
//{
// Debug.LogError("未找到" + layerName +"层的物体!!!");
//}
}
//if (temp != null) Debug.Log("Clicked Object:" + temp.name);
return temp;
}