#How are you doing this in the first
1 messages · Page 1 of 1 (latest)
Very briefly...
- I create a prefab with a name pattern, then I add a MonoBehaviour to it with a partial class
- When
PrefabStage.prefabStageClosingtriggers, i get theassetPath, read it and parse the nodes I am interested in, mainly GameObject and MonoBehaviour, lookup for their components then I grab their script guids, look for them with AssetDatabase and write the cs class... Being the name of the GameObject the name of the property concatenated with the type so I dont hit repeated properties (see the ide image) - Then after triggering an AssetDatabase.Refresh I add each cs field (StartGame_Image, etc) to the end of my partial class monobehaviour on the prefab yaml (I find that doing a loopkup with the assetdatabase based on the script guid
This works well without including nested prefabs, however when I add those to the mix I hit a dead-end because I need some chunks of the yaml unity will only add when i manually assign the inner prefab reference to a field on my mono
How do you know which components to add there?
But also, why not just do GetComponentsInChildren on the root and create code based on that instead of yamls?
I only add MonoBehaviours, because this way unity tells me right away if something is a mono or not
tbf i havent considered the other options since i found out how to understand the yamls
also, editing the yaml i can automatically assign the references to my mono class
you can do that just fine with GetComponentsInChildren too (probably)
like, you'll have the components already, that's what the codegen will be based on 🙂
@stuck turtle
Oh yea, I realised that as well
The next step will require some other API i dont know of to find the matching fileIDs to link them and then my problem will still exist, which is, by the time I need it, the PrefabInstance still doesn't have a MonoBehaviour component with a fileID i can use
Why I bother doing this? So I don't have to "GetComponent" on Awakes or have to link the instances to the Mono while editing the UI. It will be automatically generated and linked for me after the prefab is saved
No no, not that part
why do you need the file IDs and bla bla bla
show code, that will simplify things, I believe
if you don't wanna... then it's hard for me to help... but I think you can achieve the same without yaml and just GetComponentsInChildren in which case you'll just have a direct reference to the thing
Oh because the fileID is what is used when you link the instance to the field
GetComponents would work but it would have to be runtime, which I’m trying to avoid to rely on.. even if it’s running only once in awake
why? no, not at runtime
same as what you're doing now, only no yamls
that's why I wanted code though, I can't guess what you're doing now 🤷♂️
But ain’t the GetComponents meant to be done at runtime? They do not write to the asset file
no, getcomponent works at all times just fine
This is how I template the class creation.. I require a partial that inherits mono to be manually created
right, I want the code that calls this
or the one that's... on the top level, so to speak
it's just plain parsing the yaml without any libs
then after writing the cs, i use the propmappings containing the field name and the fileID to link
okay... I'm sorry, but I can't help you like this... I need more of the code in code form, the yaml parsing not important. I don't know where you start and what you have as a starting point, and what do you need to do the stuff
i mean, that's all i have?
generally, if you have a prefab, you can get its GO, do GetComponentsInChildren and that'll give you all of them no matter if they're from nested prefabs
it's 7 separate things without function names and all that, hard to see the pbig picture from these
so, this and then after you've created the cs file and stuff has recompiled (okay, that part might be tricky), you get the instance and set the values by the references that you already have
seems like you're making it 10 times more complicated with the yaml
that's just my 2 cents
maybe I'm missing something 🤷♂️
but if i rely on compilation i'd have to hop onto the roslyn analyzers wagon, no?
why? no
you just need to do stuff after it's compiled
(assign the references)
you may wanna do that in OnValidate or something of the class anyway 🤔
when are you assigning the references now?
ah, you have one class per prefab?
but if you assign the references in editor time, are they saved to disk?
i save the references on the method MaybePatchYaml
(first pic)
that's the part im missing
yes, one class per prefab
the script runs on every prefab stage close
sure thing, you can force save them or just mark the prefab as dirty and it'll save next time you save
the only real tricky part is waiting for the compiler to compile and doing something after
let me try some things and I'll get back to you
It seems to work nice with OnValidate plus EditorUtility.SetDirty(this); at the end
thank you for the suggestion 
Happy to help 🙂
Hey 👋
Sorry for reviving an old thread…
I found a way of getting through the compiling times… Unity has an attribute you can mark a method with and it will call it whenever it’s done compiling. So I got around a manual step in process with this attribute
The last thing I’m trying to get around to ditch reading the yaml is because GetComponentsInChildren<MonoBehaviour>() flattens out everything, so if I have a a prefab, all the children of the prefab are also included in the list… and reading the yaml I can’t keep my references to that prefab only.. can you think of any way of getting the components excluding prefab instances
There's PrefabUtility which should have the methods that you need
I was looking at that api but it seems that for each of the methods I’m getting a positive saying the object is a prefab because indeed the parent is
Whilst with the yaml I can safely assume every field belongs to the prefab I’m editing because everything else is a PrefabInstance
Wasn't that the main problem of the yaml - that you don't get all the nested stuff?
In any case, there are methods to see which prefab stuff comes from in that class
the yaml didn't tell me the the fileID of the nested prefab so I could link it
Didnt get back to you... found the APIs to make this work without the yaml.. thank you again
https://www.youtube.com/watch?v=J5xtZhOu3bw
Hate writing code to bind the views and end up with a polluted class full of [SerializeField]?
Look no more, this asset is for you. It allows you to generate code for a given prefab ending in a configurable suffix (default to .View), and also allow you to link nested prefabs.
Soon on the Unity AssetStore.
#unity3d #coding #codinglife