- Home /
How exactly are prefabs tagged with PrefabAssetType enums?
So... I have looked this up, but information seems scarce. The enum values and descriptions here: https://docs.unity3d.com/ScriptReference/PrefabAssetType.html are quite confusing. My goal is basically to determine whether an object is a prefab in the project windows that lives in my assets, or an instance that exists in the scene. I was trying to do this with gameObject.scene.name==null, but that seems to have changed since nested prefabs were introduced and it is very hacky anyway. So in Reset() and OnValidate(), I am calling PrefabUtility.GetPrefabAssetType(this.gameObject) and setting a bool which can be checked at runtime. Or I am trying to. I do not understand the output I am getting from the function at all. It looks like when I have a prefab in my project folder it returns PrefabAssetType.NotAPrefab, but when it checks the prefab instance in the scene, it returns PrefabAssetType.Regular? To me, this seems like very weird behavior? And I don't understand what PrefabAssetType.Variant would be at all... Can someone please clearly explain to me exactly what the types are and how prefabs are tagged? I'm very confused if this is even the intended behavior or if I've somehow broken something as it just seems to be very counter intuitive labeling, or I am missing something about how prefabs are categorized(and it is probably the later)
Are you usie Selection.activeGameObject
to call to the selected object in the Project folder?
No, is that function related to PrefabAssetType
? I am grabbing all Item components with a script from this post: https://answers.unity.com/questions/486545/getting-all-assets-of-the-specified-type.html and I just want to be sure that it is always grabbing a prefab(Which I think will always be true?). Then I grab all the item components later using Item[] instances = (Item[])Resources.FindObjectsOfTypeAll(typeof(Item));
and I want to be sure that I am only acting on the item components in all my scenes, not the prefabbed ones in my project folder. This is important because all my item prefabs need a unique GUID and after a GUID is added/changed I update all the instances to ensure everything stays in sync. I can then use the GUID to get the prefab at run-time and instantiate more as needed, and because I am using an int for my ItemGUID it can serialize it and use it to save/load my game's items to JSON. GUIDs are unique to the prefab in my architecture though, not necessarily the item script subclass. Thus I am trying to generate and track these GUIDs in editor scripting and I need to differentiate between components in prefabs and components actually on GameObjects in the scene.
Edit: I also need to do a check in the Item's Reset()
and OnValidate()
callbacks.
Pretty sure you have to load the resource before Resources.FindObjectsOfTypeAl
l does anything with your assets. You can use something like Resources.LoadAll("/Items", typeof(Item));
and then your FindAllOfType
might actually do something that doesn't return scene assets. If you want to get ONLY assets from the project resources folder, use the returned list from Resources.LoadAll
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Custom Inspector to Load Instance Values 0 Answers
[solved]Using WWW instead of resources.load causes hang 1 Answer
When do the Attributes get set? 1 Answer