Unity給物體添加多個Tag的實現(xiàn)
在unity中,我們經(jīng)常通過給物體添加標(biāo)簽來判斷這個物體是不是我們想要的

但是unity默認(rèn)只能添加一個標(biāo)簽,那如果我們要給一個物體添加多個標(biāo)簽應(yīng)該怎么辦
首先,我們定義一個Tag.cs類,這個類用來存儲物體的tag信息
public class Tags : MonoBehaviour{
public List<string> tags=new List<string>();
}
然后創(chuàng)建一個單例類TagManager.cs用來管理tag
public class TagManager : MonoBehaviour {
public static TagManager Instance { get; private set; }
private void Awake() {
if (Instance == null) {
Instance = (TagManager) this;
DontDestroyOnLoad(gameObject);
}
else {
Destroy(gameObject);
}
}
}
在TagManager中增加存儲tag的list和對應(yīng)物體的dictionary
public List<string> tagsList = new List<string>(); [ShowInInspector] public Dictionary<string, List<GameObject>> tagsDictionary = new Dictionary<string, List<GameObject>>();
但是inspector窗口中修改dictionary中的值后,播放時會自動清除數(shù)據(jù),所以還要給dictionary賦值
private void InitTags() {
int length = tagsList.Count;
tagsDictionary = new Dictionary<string, List<GameObject>>(length);
for (int i = 0; i < length; i++) {
tagsDictionary.Add(tagsList[i], new List<GameObject>());
}
}
然后我們通過在inspector窗口修改list來修改物體的taf

新建一個Extensions.cs,我們在這個類里面寫一些擴(kuò)展方法
public static class Extensions {
//包含所有的 tag 返回true
public static bool HasTag(this GameObject gameObject, params string[] tag) {
if (gameObject.TryGetComponent<Tags>(out Tags t)) {
for (int i = 0; i < tag.Length; i++) {
if (!t.tags.Contains(tag[i])) {
Debug.Log(gameObject.name+"不存在"+tag+"標(biāo)簽");
return false;
}
}
}
return true;
}
//給物體增加 tag
public static void AddTag(this GameObject gameObject, params string[] tags) {
if (gameObject.TryGetComponent<Tags>(out Tags t)) {
foreach (var tag in tags) {
if (!TagManager.Instance.tagsList.Contains(tag)) {
Debug.Log("增加一個tag:" + tag);
TagManager.Instance.tagsDictionary.Add(tag, new List<GameObject>());
Debug.Log(tag + "增加一個物體" + gameObject.name);
TagManager.Instance.tagsDictionary[tag].Add(gameObject);
}
else {
Debug.Log(tag + "增加一個物體" + gameObject.name);
TagManager.Instance.tagsDictionary[tag].Add(gameObject);
}
}
}
}
//給物體刪除tag
public static void RemoveTag(this GameObject gameObject,params string[] tags) {
for (int i = 0; i < tags.Length; i++) {
if (gameObject.HasTag(tags[i])) {
gameObject.GetComponent<Tags>().tags.Remove(tags[i]);
Debug.Log(gameObject.name+"移除"+tags+"標(biāo)簽");
TagManager.Instance.tagsDictionary[tags[i]].Remove(gameObject);
}
else {
Debug.LogWarning(gameObject.name+"不存在"+tags[i]+"標(biāo)簽");
}
}
}
}
這樣像下面這樣直接調(diào)用即可
gameObject.AddTag("Player","Tag1");
gameObject.HasTag("Player","Tag1");
gameObject.RemoveTag("Player");
最后TagManager.cs中通過標(biāo)簽獲取所有物體
public List<GameObject> FindObjsWithTag(string tag) {
if (tagsDictionary.ContainsKey(tag)) {
return tagsDictionary[tag];
}
Debug.Log("不存在標(biāo)簽為" + tag + "的物體");
return null;
}
測試一下
創(chuàng)建兩個掛有Tags.cs腳本的物體
public class Tags : MonoBehaviour{
public List<string> tags=new List<string>();
void Start() {
gameObject.AddTag(tags.ToArray());
gameObject.HasTag("Player");
}
void Update(){
if (Input.GetKeyDown(KeyCode.A)) {
gameObject.RemoveTag("Player","tag1");
}
}
}
兩個物體具有的tag分別為tag1,tag2和tag1
TagManager只有tag1

運行

但是這樣當(dāng)退出Play模式后,List中的數(shù)據(jù)會被清空,所以可以使用ScriptableObject進(jìn)行持久化存儲也可以使用Odin等插件直接對字典進(jìn)行序列化
使用ScriptableObject
新建一個TagsAsset.cs
[CreateAssetMenu]
public class TagsAsset : ScriptableObject{
public List<string> tags=new List<string>();
}
將原先代碼中tagsList相關(guān)的代碼修改為tagsAsset.tags并修繕一下即可
使用Odin
導(dǎo)入Odin插件,使TagManager繼承自SerializedMonoBehaviour
[NonSerialized, OdinSerialize] public Dictionary<string, List<GameObject>> tagsDictionary = new Dictionary<string, List<GameObject>>();
關(guān)于Odin的進(jìn)階可以看https://zhuanlan.zhihu.com/p/268324619
到此這篇關(guān)于Unity給物體添加多個Tag的實現(xiàn)的文章就介紹到這了,更多相關(guān)Unity 物體添加多個Tag內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Unity?UGUI的MaskableGraphic可遮罩圖形組件介紹使用
這篇文章主要為大家介紹了Unity?UGUI的MaskableGraphic可遮罩圖形組件介紹使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
用C#獲取硬盤序列號,CPU序列號,網(wǎng)卡MAC地址的源碼
用C#獲取硬盤序列號,CPU序列號,網(wǎng)卡MAC地址的源碼...2007-03-03
Visual Studio C#創(chuàng)建windows服務(wù)程序
用Visual C#創(chuàng)建Windows服務(wù)不是一件困難的事,本文就將指導(dǎo)你一步一步創(chuàng)建一個Windows服務(wù)并使用它,本文主要介紹了Visual Studio C#創(chuàng)建windows服務(wù)程序,感興趣的可以了解一下2024-01-01

