Search Unity

Detect Duplication of assets & get the duplicates

Discussion in 'Scripting' started by ESG-Steven, Sep 12, 2016.

  1. ESG-Steven

    ESG-Steven

    Joined:
    Mar 18, 2015
    Posts:
    38
    Hey Everyone!

    I need to detect when a new asset (or assets if multiple were imported) was the result of a duplication. Very specifically to do some data transforms if the asset has a custom property of mine (which has a custom PropertyDrawer for it). I've tried the Unity AssetPostProcessor but that only does general imports and the Events.current seemed to do absolutely nothing whenever I duplicated an asset.

    Any help is appreciated!
     
  2. Dave-Hampson

    Dave-Hampson

    Unity Technologies

    Joined:
    Jan 2, 2014
    Posts:
    150
    Hi Steven,

    At a higher level, what is it that you are trying to do?

    Dave
     
  3. ESG-Steven

    ESG-Steven

    Joined:
    Mar 18, 2015
    Posts:
    38
    I have a custom property that we'll just call 'MyProperty.' Any object that derives from ScriptableObject can have a MyProperty field so long as the MyProperty has a [SerializeField]. MyProperty is essentially a custom string field that generates and stores a guid that matches to an external system, essentially binding the string to certain data. (the guid is serialized but also hidden from the visual editor)

    The issue is that when a designer duplicates an asset, the serialized guid that MyProperty generated is also duplicated, making all of the MyProperty data in the duplicated object bound to the same data as the original. Sometimes these assets are large with a lot of useful data that would be a time saver for designers to duplicate.

    My approach was to track duplication from Unity and, if any of the objects were a ScriptableObject, go in and regenerate the guid for any fields that are of type MyProperty.

    NOTE: MyProperty is NOT a MonoBehaviour. Just a basic class with a CustomPropertyDrawer and a couple serialized primitives
     
  4. Dave-Hampson

    Dave-Hampson

    Unity Technologies

    Joined:
    Jan 2, 2014
    Posts:
    150
    What is the external system exactly? Sounds like a very specific use case!

    How about going through all of the assets, detecting duplicate 'MyProperty' values, and then regenerating them? Or is that the approach you are already taking and you are searching for a better way? (If so, what are the main drawbacks with that approach that you are looking to avoid?)
     
  5. ESG-Steven

    ESG-Steven

    Joined:
    Mar 18, 2015
    Posts:
    38
    Hey Dave! Our external system is a localization management system. Essentially, our designers can alter the localization data straight from an asset, which allows them to look at how the localization will look on screen while still actually applying data.

    Also that approach would take a very long time given the amount of data in our project. I've made a quick button that lets them regenerate localization on one entry (doing it to all child entries could result in infinite loops if two assets referenced each other) which takes quite a while if an asset that's duplicated has 20+ localization entries on it.

    Perhaps there's not a better approach, but I just find it odd that I can't listen to an event or something from the editor (duplicate, state changes, etc) and react to them with information based on their action (IE: the new duplicates after a duplication)

    Thanks a lot for the help you're giving me =)
     
  6. Dave-Hampson

    Dave-Hampson

    Unity Technologies

    Joined:
    Jan 2, 2014
    Posts:
    150
    I guess it could be staggered, e.g. iterate and check one asset per frame. As long as the duplicate is detected within a few seconds (i.e. between duplicating an object and checking it in), it might not be so bad, so I guess the search could be throttled right down to use minimal CPU.

    I don't know of an event offhand.
    Is this related? : http://answers.unity3d.com/answers/445239/view.html
     
    Alverik likes this.
  7. ESG-Steven

    ESG-Steven

    Joined:
    Mar 18, 2015
    Posts:
    38
    Hmmm. This definitely inspires a few ideas! I will definitely try something similar (if I'm given the time) and tell you how it goes! Thanks a lot. Despite all my scouring, I didn't know AssetModificationProcessor was a thing! I only knew about the AssetPostProcessor D=
     
  8. ESG-Steven

    ESG-Steven

    Joined:
    Mar 18, 2015
    Posts:
    38
    What I meant was that I couldn't just do it to all SerializedPropertys of my SerializedObject because some of those properties aren't my LocalizableString, they're other types that contain and render their own LocalizableString. Theirfore, I'd have to go an unknown number of levels deep, which may not have an end of depth if I have a circular reference.

    So my button only refreshes a single entry, meaning they must open the dropdown and hit the button for each LocalizableString that the duplicated asset renders in the inspector