如图:随意点击一个按键,开启检测,当连线触发到另外一个按键的时候,会检测他需要连接的其他物体。这种功能类似我们的大脑神经元。废话不多说,直接上代码(需要用到Vectrosity插件,下载地址:链接:http://pan.baidu.com/s/1hs7u7cg 密码:zq1w):
1.新建一个脚本,这个脚本用来计时,并且整个系统的初始化方法也在这
using System.Collections;
using UnityEngine;
///游戏控制器,挂载在摄像机上
public class GameMgr : MonoBehaviour
{
private static GameMgr instance;
public static GameMgr Instance
{
get
{
return instance;
}
}
///
/// 计时器
///
private float timer = 0.1f;
///
/// 时长
///
private float duration = 0.1f;
private void Awake()
{
instance = this;
}
private void Update()
{
timer += Time.deltaTime;
if (timer >= duration)
{
GlobalData.running = true;
timer = 0;
}
else
{
GlobalData.running = false;
}
}
///
/// 神经元路径初始化
///
public IEnumerator NeurePathReset()
{
Debug.Log("重置神经元路径");
yield return new WaitForSeconds(3f);
GlobalData.connectStart = false;
GlobalData.connectOver = false;
for (int i = 0; i < GlobalData.connectPath.Count; i++)
{
GameObject obj = GlobalData.connectPath[i].gameObject;
GlobalData.connectPath[i].ResetNeure();
ObjectPools.Instance.IntoPool(obj, "Path");
}
}
}
2.在写一个对象池,挂载在摄像机上(画线的物体使用对象池,减少消耗)
using System.Collections.Generic;
using UnityEngine;
///
/// 对象池(用路径来创建池)
///
public class ObjectPools : MonoBehaviour
{
private static ObjectPools instance;
///
/// 对象池管理字典{池名 : 池内对象 }
///
Dictionary> PoolController = new Dictionary>() { };
public static ObjectPools Instance
{
get
{
return instance;
}
set
{
instance = value;
}
}
void Awake()
{
instance = this;
}
///
/// 对象池名字
///
/// 对象池的名字为该物体Resources下路径
///
public GameObject GetPool(string poolName)
{
GameObject obj;
if (PoolController.ContainsKey(poolName) && PoolController[poolName].Count > 0)
{
obj = PoolController[poolName][0];
PoolController[poolName].RemoveAt(0);//把第一个位置释放;
}
else if (PoolController.ContainsKey(poolName) && PoolController[poolName].Count <= 0)
{
obj = Instantiate(Resources.Load(poolName));
}
else
{
obj = Instantiate(Resources.Load(poolName));
PoolController.Add(poolName, new List() { });
}
obj.SetActive(true);
return obj;
}
///
/// 放入池子中的方法
///
///
public void IntoPool(GameObject obj,string key)
{
obj.transform.parent = null;
obj.SetActive(false);
PoolController[key].Add(obj);
}
}
- 新建一个脚本,存储数据
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class GlobalData
{
///
/// 开启连接检测?
///
public static bool connectStart = false;
///
/// 连接线是否开始 -- 经纬度画线控制
///
public static bool running;
///
/// 所有相关联的连接是否完成
///
public static bool connectOver;
///
/// 连接路径
///
public static List connectPath = new List();
}
4.新建一个类,用来管理Tag值
public class Tags
{
///
/// 神经元标签
///
public const string Neure = "Neure";
///
/// 路径标签
///
public const string Path = "Path";
}
5.接下来是重点:新建一个脚本,神经元脚本,这个脚本挂载在每一个需要检测的物体身上(改物体的Tag值为:Neure)
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Neure : MonoBehaviour
{
private Button OnClick;
///
/// 是否被触发
///
[HideInInspector]
public bool isTrigger = false;
///
/// 检测其他神经元
///
List checkNeure = new List();
private void Awake()
{
OnClick = transform.GetComponent
6.新建一个空物体,放到Resources文件夹下(Tag值为Path)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Vectrosity;
public class Path : MonoBehaviour
{
///
/// 两点之间的距离
///
private int DistanceBetweenPoints = 1;
///
/// 发起者神经元
///
[HideInInspector]
public Neure sponsorNeure;
///
/// 目标点
///
[HideInInspector]
public Neure targetNeure;
///
/// 路径
///
private VectorLine pathLine;
///
/// 画线材质
///
public Material lineMaterial;
///
/// 方向向量
///
private Vector3 direction;
///
/// 偏移向量
///
private Vector3 offsetDirection;
///
/// 路径被分割的点
///
private int pathCount = 0;
///
/// 下标
///
private int index = 0;
private bool isCallBack = false;
private void Update()
{
if (GlobalData.connectStart == false)
{
return;
}
if (index >= pathCount) // 该条神经元连接完毕
{
if (isCallBack == false)
{
isCallBack = true;
GlobalData.connectPath.Add(this);
ConnectSuccseCallBack();
targetNeure.CheckOtherNeure();
}
return;
}
if (GlobalData.running && pathCount != 0)
{
index++;
transform.position = index * direction / pathCount - offsetDirection;
pathLine.points3.Add(transform.position);
pathLine.Draw();
}
}
///
/// 链接其他神经元
///
public void ConnectOtherNeure(Neure neure)
{
targetNeure = neure;
direction = targetNeure.transform.position - transform.position;
offsetDirection = Vector3.zero - transform.position;
pathCount = (int)(Vector3.Distance(transform.position, neure.transform.position) / DistanceBetweenPoints);
index = 0;
pathLine = new VectorLine("Path", new List(), lineMaterial, 12.0f, LineType.Continuous);
pathLine.textureScale = 1.0f;
pathLine.points3.Add(transform.position);
}
///
/// 连接成功回调
///
private void ConnectSuccseCallBack()
{
for (int i = 0; i < GameObject.FindGameObjectsWithTag(Tags.Path).Length; i++)
{
if (GameObject.FindGameObjectsWithTag(Tags.Path)[i].GetComponent().isCallBack == false)
{
Debug.Log("还未链接完毕");
break;
}
if (i == GameObject.FindGameObjectsWithTag(Tags.Path).Length - 1)
{
GlobalData.connectOver = true;
StartCoroutine(GameMgr.Instance.NeurePathReset());
Debug.Log("链接完毕");
}
}
}
///
/// 清空该条神经元上的所有数据
///
public void ResetNeure()
{
isCallBack = false;
index = 0;
pathCount = 0;
sponsorNeure.isTrigger = false;
targetNeure.isTrigger = false;
pathLine.points3.Clear();
pathLine.Draw();
}
}