#How are you doing this in the first

1 messages · Page 1 of 1 (latest)

stuck turtle
#

Very briefly...

  • I create a prefab with a name pattern, then I add a MonoBehaviour to it with a partial class
  • When PrefabStage.prefabStageClosing triggers, i get the assetPath, 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

pure spindle
#

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?

stuck turtle
#

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

pure spindle
#

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

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

pure spindle
#

but why do you need these things in the first place?

#

wanna show code?

stuck turtle
#

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

pure spindle
#

No no, not that part

pure spindle
#

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

stuck turtle
#

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

pure spindle
#

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 🤷‍♂️

stuck turtle
pure spindle
#

no, getcomponent works at all times just fine

stuck turtle
#

This is how I template the class creation.. I require a partial that inherits mono to be manually created

pure spindle
#

right, I want the code that calls this

#

or the one that's... on the top level, so to speak

stuck turtle
#

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

pure spindle
#

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

stuck turtle
#

i mean, that's all i have?

pure spindle
#

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

pure spindle
#

seems like you're making it 10 times more complicated with the yaml

#

that's just my 2 cents

#

maybe I'm missing something 🤷‍♂️

stuck turtle
#

but if i rely on compilation i'd have to hop onto the roslyn analyzers wagon, no?

pure spindle
#

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?

stuck turtle
#

but if you assign the references in editor time, are they saved to disk?

#

i save the references on the method MaybePatchYaml

#

(first pic)

stuck turtle
stuck turtle
#

the script runs on every prefab stage close

pure spindle
#

the only real tricky part is waiting for the compiler to compile and doing something after

stuck turtle
#

let me try some things and I'll get back to you

stuck turtle
#

It seems to work nice with OnValidate plus EditorUtility.SetDirty(this); at the end

#

thank you for the suggestion Thumbglass

pure spindle
#

Happy to help 🙂

stuck turtle
#

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

pure spindle
stuck turtle
#

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

pure spindle
#

In any case, there are methods to see which prefab stuff comes from in that class

stuck turtle
stuck turtle
#

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

▶ Play video