Search Unity

Any way to get something when calling methods from prefab asset??? Im broken, help me

Discussion in 'Prefabs' started by Nirvan, Jan 12, 2020.

  1. Nirvan

    Nirvan

    Joined:
    Nov 16, 2013
    Posts:
    134
    Hi, I was working about 3 months on certain package for asset store.
    I was working on unity 2017, package is very complicated at ScriptableObjects references side.
    I want my system to be flexible and after few nights I figured out way to sync scriptables without saving as assets (storing them inside prefab asset instead) in moment when prefab is created.
    This is involving stuff like "PrefabUtility.GetPrefabObject" which now is obsolete and "Asset Handles are discontinued" o_Oo_Oo_O
    Not mentioning that now I can't define in any way when I am calling method from prefab asset file, if certain object from scene comes from the same prefab which is calling (from project asset folder)
    now I can't even define prefab asset object.

    Code (CSharp):
    1. PrefabUtility.GetPrefabInstanceHandle(componentCalling)
    2. PrefabUtility.GetCorrespondingObjectFromSource(componentCalling.gameObject)
    3. PrefabUtility.GetCorrespondingObjectFromSource(componentCalling)
    4. PrefabUtility.GetPrefabInstanceHandle(componentCalling.gameObject)
    It's just nulls, NULL.
    I can't even get path to prefab!
    Gosh please help my case.
    I was working so much this weeks, sleeping for 5 hours and at final this nonsense
    Everything works great in unity 2017 before changes to the prefab system o_O

    Is there some other way?
    In addition I can describe in short my case for the system but it can be hard to understand.
    Maybe you'll give me idea if there is other way to build my system, but it's finished... just new prefab system killed it's flexibility, it can work but workflow will be slowed down.

    So:
    I am using ScriptableObject to store some settings in.
    I derive from this class to create different settings types also as scriptables.
    There is another Scriptable class which is containing list (like few quality levels) of first this scriptables, settings works like L.O.D.
    so in this class are few setting scriptable classes for different quality for one certain component type.
    There is MonoBehaviour handling all of that, it's generating settings scriptables for some component,
    if there are more components Monobehaviour can handle, then it generates another list of settings.
    It's holding this scriptable settings inside as scene assets.
    Until this game object not becomes prefab everything is alright.
    If I create prefab this settings would be lost because they're scene asset.
    So when I create prefab from it i generate new settings to prefabed object and store settings in it.
    In unity 2017 I can copy settings made in scene object and copy them into prefab so nothing is lost. (tweaking settings before creating prefab)
    (this is my case here, clean work)
    In new prefab system I can't save generated settings to prefab file because editor can't define if prefab asset is anything, all methods return null or empty path string :rolleyes::rolleyes::rolleyes: cool upgrade thank you :(
    Of course I can save settings scriptables as project asset files, but them my system would work only on files saved in project,
    I wanted give my system possibility to work without that.

    Edit: After calming down I checked more variants in clean way and probably will solve this case. But not done it yet.

    Edit 2: Ok, I needed to delay checking for path of new created prefab. (GetAssetPath(gameObject)) In OnValidate() I was making serialized object dirty until GetAssetPath return not empty string (about 4 editor refresh frames) and it works.
     
    Last edited: Jan 13, 2020