Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Instantiating prefabs and maintaining the editor link for Select/Revert/Apply functionality

Discussion in 'Scripting' started by wynott, Feb 27, 2017.

  1. wynott

    wynott

    Joined:
    Nov 16, 2016
    Posts:
    29
    I'm working on an architechural visualization type project and using instantiate in an editor script to populate the world with objects exported from my CAD model, (wrote export script for CAD).

    The majority of my "game" world is instantiated this way via this scripting.

    When I'm instantiating prefabs, the instances don't seem to be updating when I change the original prefab.

    I am lead to believe instantiate is doing the same thing as dragging the prefab manually in the editor, but when it happens in the script, the editor doesn't know to maintain the link between the instance and the original prefab.

    I'd really like changes to the prefab to propagate down to the instances.

    Particular issue right now is tweaking LOD distances. When I change the prefab I have to re-instantiate everything to check settings, delete them all, adjust prefab, re-instantiate etc. It's taking me way too long to fine-tune this stuff.

    Possible? Ideas?
     
    brunogattai1996 likes this.
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
  3. AndyGainey

    AndyGainey

    Joined:
    Dec 2, 2015
    Posts:
    216
    Is this all in editor scripts, with prefabs instantiated at editor-time? If so, maybe try UnityEditor.PrefabUtility.InstantiatePrefab(). As described in the docs, "This is similar to Instantiate but creates a prefab connection to the prefab."
     
    brunogattai1996 likes this.
  4. wynott

    wynott

    Joined:
    Nov 16, 2016
    Posts:
    29
    Thanks @StarManta @AndyGainey

    This smells like it's definitely what I need. Sorry, not trying to be dense but I'm not a programmer and the structure looks different to me. Struggling with how to implement this, can't seem to find a code snippet that makes sense to me. Basically need to replace a typical Instantiate. When I try and use the code in the docs it just turns red and barfs everywhere. I have included "using UnityEditor;"
     
  5. wynott

    wynott

    Joined:
    Nov 16, 2016
    Posts:
    29
    var newBlock = (GameObject)Instantiate(ObjectToInstance, matrix.GetRow(3), Quaternion.LookRotation(matrix.GetColumn(1), matrix.GetColumn(2)));

    The transform matrix it's using is coming from Rhinoceros. Looks like I'll have to reposition and reorient the prefab afterwards in this case but I think I can handle that.
     
  6. wynott

    wynott

    Joined:
    Nov 16, 2016
    Posts:
    29