- 最后登录
- 2021-7-6
- 注册时间
- 2012-12-27
- 阅读权限
- 90
- 积分
- 76145
 
- 纳金币
- 53520
- 精华
- 316
|
- using UnityEngine;
- using System.Collections;
- using UnityEditor;
- public class DemoText : MonoBehaviour {
- // Use this for initialization
- void Start () {
- bool b = isHasTag("Player"); //随意挂载进行测试
- Debug.Log(b);
- GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
- AddTag("Zombie",cube);
- }
-
- // Update is called once per frame
- void Update () {
-
- }
- //动态添加tag
- private static void AddTag(string newtag, GameObject obj)
- {
- if (!isHasTag(newtag))
- {
- SerializedObject tagManager = new SerializedObject(obj);
- SerializedProperty pro = tagManager.GetIterator();
- while (pro.NextVisible(true))
- {
- if (pro.name == "m_TagString")
- {
- pro.stringValue = newtag;
- tagManager.ApplyModifiedProperties();
- }
- }
- }
- else
- {
- obj.tag = newtag;
- }
- }
- static bool isHasTag(string newtag)
- {
- for (int i = 0; i < UnityEditorInternal.InternalEditorUtility.tags.Length; i++)
- {
- if (UnityEditorInternal.InternalEditorUtility.tags[i].Equals(newtag))
- {
- return true;
- }
- }
- return false;
- }
- }
复制代码 |
|