简体   繁体   中英

Dynamically Create GameObject Tags in Unity

Hullo all,

I'd like to implement a feature for the current unity project I'm working on that allows for a user to provide a formatted text file, and for Unity to instantiate objects based on that text file (position, rotation, scale, color, etc.). Naturally, I'd like those objects to have tags associated with them, allowing for quickly and easily grouping similar objects by tag.

This post was helpful in showing how the unity editor can be modified upon hitting 'play' to add and remove tags directly, but it unfortunately glosses over the fact that the UnityEditor namespace cannot be used in builds! (mainly complaining about the serializedObject and serializedProperty data types seen in the link provided)

As it stands, my project works as intended in the editor proper, but if I move my current tag creator and associated scritps to a separate 'Assets/Editor' directory and actually build my project, I of course lose all functionality associated with those scripts.

Is there any other way to either

  • Somehow include UnityEditor in some contradictory manner that I'm unaware of;
  • Create/edit tags dynamically based on user input via text without using UnityEditor;
  • Implement a similar functionality that mimics but does not require tags

Thanks a bunch!

You can't include the UnityEditor in the build and, as far as I know, there's no way to create tags at runtime. The solution would be to implement your own indexing system to quickly find objects that can be instantiated at runtime.

You could make a class DynamicObject : Monobehaviour to store tags and every other information related to the indexing/instantiation system.

A simple implementation would be using a Dictionary<string, List<DynamicObject>> where the key is a tag. On indexing, you would add the object to as many lists as it has tags.

You would register to the system only Objects that need to be instantiated at runtime, thus making the search faster.

I suspect even such a simple implementation would be much faster than Unity's built-in methods GameObject.FindGameObjectsWithTag .

Just as a side note, if you also need to dynamically define new types by composing reusable components, I suggest looking into Entity Component System libraries.

Solved!

Further experimentation did indeed confirm that the UnityEditor namespace simply cannot be used in builds. As proposed by Trey , a user who commented on the question above, creating a simple list of strings was ideal.

Each object now has a psuedoTag parameter, simply a string directly taken from a list stored in a tagMaster object, and instead of checking for whatever.tag , I simply get the component in which the psuedoTag in stored and compare string values. Additionally, the nature of lists in C# allow for dynamic creation and removal of psuedoTags, and my project has the exact flexibility I desired.

Thanks again to all for the suggestions!

I was facing a similar problem where I had to dynamically create objects. I needed a way to tag them so that I could control them using my scripts. So I tried to tag them dynamically but it showed an error that the tag doesn't exit. So tried to fool Unity by creating a cube and assigning it the tag I wanted for my dynamic objects. I kept the cube beyond the camera focus and I was able to successfully nail it. Try it may work for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM