效果圖.png
如圖:隨意點擊一個按鍵牍氛,開啟檢測六水,當連線觸發(fā)到另外一個按鍵的時候,會檢測他需要連接的其他物體凰盔。這種功能類似我們的大腦神經(jīng)元墓卦。廢話不多說,直接上代碼(需要用到Vectrosity插件户敬,下載地址:鏈接:http://pan.baidu.com/s/1hs7u7cg 密碼:zq1w):
1.新建一個腳本落剪,這個腳本用來計時,并且整個系統(tǒng)的初始化方法也在這
using System.Collections;
using UnityEngine;
///游戲控制器尿庐,掛載在攝像機上
public class GameMgr : MonoBehaviour
{
private static GameMgr instance;
public static GameMgr Instance
{
get
{
return instance;
}
}
/// <summary>
/// 計時器
/// </summary>
private float timer = 0.1f;
/// <summary>
/// 時長
/// </summary>
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;
}
}
/// <summary>
/// 神經(jīng)元路徑初始化
/// </summary>
public IEnumerator NeurePathReset()
{
Debug.Log("重置神經(jīng)元路徑");
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;
/// <summary>
/// 對象池(用路徑來創(chuàng)建池)
/// </summary>
public class ObjectPools : MonoBehaviour
{
private static ObjectPools instance;
/// <summary>
/// 對象池管理字典{池名 : 池內(nèi)對象 }
/// </summary>
Dictionary<string, List<GameObject>> PoolController = new Dictionary<string, List<GameObject>>() { };
public static ObjectPools Instance
{
get
{
return instance;
}
set
{
instance = value;
}
}
void Awake()
{
instance = this;
}
/// <summary>
/// 對象池名字
/// </summary>
/// <param name="poolName">對象池的名字為該物體Resources下路徑</param>
/// <returns></returns>
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<GameObject>(poolName));
}
else
{
obj = Instantiate(Resources.Load<GameObject>(poolName));
PoolController.Add(poolName, new List<GameObject>() { });
}
obj.SetActive(true);
return obj;
}
/// <summary>
/// 放入池子中的方法
/// </summary>
/// <param name="go"></param>
public void IntoPool(GameObject obj,string key)
{
obj.transform.parent = null;
obj.SetActive(false);
PoolController[key].Add(obj);
}
}
- 新建一個腳本凡泣,存儲數(shù)據(jù)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class GlobalData
{
/// <summary>
/// 開啟連接檢測?
/// </summary>
public static bool connectStart = false;
/// <summary>
/// 連接線是否開始 -- 經(jīng)緯度畫線控制
/// </summary>
public static bool running;
/// <summary>
/// 所有相關聯(lián)的連接是否完成
/// </summary>
public static bool connectOver;
/// <summary>
/// 連接路徑
/// </summary>
public static List<Path> connectPath = new List<Path>();
}
4.新建一個類皮假,用來管理Tag值
public class Tags
{
/// <summary>
/// 神經(jīng)元標簽
/// </summary>
public const string Neure = "Neure";
/// <summary>
/// 路徑標簽
/// </summary>
public const string Path = "Path";
}
5.接下來是重點:新建一個腳本鞋拟,神經(jīng)元腳本,這個腳本掛載在每一個需要檢測的物體身上(改物體的Tag值為:Neure)
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Neure : MonoBehaviour
{
private Button OnClick;
/// <summary>
/// 是否被觸發(fā)
/// </summary>
[HideInInspector]
public bool isTrigger = false;
/// <summary>
/// 檢測其他神經(jīng)元
/// </summary>
List<Neure> checkNeure = new List<Neure>();
private void Awake()
{
OnClick = transform.GetComponent<Button>();
OnClick.onClick.AddListener(
()=>
{
GlobalData.connectStart = true;
CheckOtherNeure();
});
}
/// <summary>
/// 檢測其他神經(jīng)元網(wǎng)絡
/// </summary>
public void CheckOtherNeure()
{
if (GameObject.FindGameObjectsWithTag(Tags.Neure).Length <= 1)
{
return;
}
checkNeure.Clear();
this.isTrigger = true;
for (int i = 0; i < GameObject.FindGameObjectsWithTag(Tags.Neure).Length; i++)
{
Neure neure = GameObject.FindGameObjectsWithTag(Tags.Neure)[i].GetComponent<Neure>();
if (neure.isTrigger == false)
{
checkNeure.Add(neure);
}
}
if (checkNeure.Count != 0)
{
StartConnectOtherNeure();
}
}
/// <summary>
/// 開始連接其他神經(jīng)元
/// </summary>
private void StartConnectOtherNeure()
{
for (int i = 0; i < checkNeure.Count; i++)
{
GameObject obj = ObjectPools.Instance.GetPool("Path");
obj.transform.SetParent(transform);
obj.transform.localPosition = Vector3.zero;
obj.transform.localScale = new Vector3(1, 1, 1);
obj.GetComponent<Path>().sponsorNeure = this;
obj.GetComponent<Path>().ConnectOtherNeure(checkNeure[i]);
}
}
}
6.新建一個空物體惹资,放到Resources文件夾下(Tag值為Path)
LGFGR99M@8F8SM@VP7@QYI0.png
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Vectrosity;
public class Path : MonoBehaviour
{
/// <summary>
/// 兩點之間的距離
/// </summary>
private int DistanceBetweenPoints = 1;
/// <summary>
/// 發(fā)起者神經(jīng)元
/// </summary>
[HideInInspector]
public Neure sponsorNeure;
/// <summary>
/// 目標點
/// </summary>
[HideInInspector]
public Neure targetNeure;
/// <summary>
/// 路徑
/// </summary>
private VectorLine pathLine;
/// <summary>
/// 畫線材質
/// </summary>
public Material lineMaterial;
/// <summary>
/// 方向向量
/// </summary>
private Vector3 direction;
/// <summary>
/// 偏移向量
/// </summary>
private Vector3 offsetDirection;
/// <summary>
/// 路徑被分割的點
/// </summary>
private int pathCount = 0;
/// <summary>
/// 下標
/// </summary>
private int index = 0;
private bool isCallBack = false;
private void Update()
{
if (GlobalData.connectStart == false)
{
return;
}
if (index >= pathCount) // 該條神經(jīng)元連接完畢
{
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();
}
}
/// <summary>
/// 鏈接其他神經(jīng)元
/// </summary>
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<Vector3>(), lineMaterial, 12.0f, LineType.Continuous);
pathLine.textureScale = 1.0f;
pathLine.points3.Add(transform.position);
}
/// <summary>
/// 連接成功回調(diào)
/// </summary>
private void ConnectSuccseCallBack()
{
for (int i = 0; i < GameObject.FindGameObjectsWithTag(Tags.Path).Length; i++)
{
if (GameObject.FindGameObjectsWithTag(Tags.Path)[i].GetComponent<Path>().isCallBack == false)
{
Debug.Log("還未鏈接完畢");
break;
}
if (i == GameObject.FindGameObjectsWithTag(Tags.Path).Length - 1)
{
GlobalData.connectOver = true;
StartCoroutine(GameMgr.Instance.NeurePathReset());
Debug.Log("鏈接完畢");
}
}
}
/// <summary>
/// 清空該條神經(jīng)元上的所有數(shù)據(jù)
/// </summary>
public void ResetNeure()
{
isCallBack = false;
index = 0;
pathCount = 0;
sponsorNeure.isTrigger = false;
targetNeure.isTrigger = false;
pathLine.points3.Clear();
pathLine.Draw();
}
}