Unity Plugin Development: PREFABUTILITY--PREFAB instance reference break and replace

Source: Internet
Author: User

In unity using the prefab process, we sometimes need to prefab instances to break references and replace references. To achieve these requirements, the core class used is prefabutility. Prefabutility is a static class that is used primarily for prefab related processing. Here are some of the main ways to use

    • Prefabutility.createemptyprefab
    • Prefabutility.replaceprefab
    • Prefabutility.disconnectprefabinstance
    • Prefabutility.getprefabparent
    • Prefabutility.connectgameobjecttoprefab
Disconnecting references

The code to disconnect the prefab reference is as follows

[MenuItem ("tools/prefab/removal reference")]

public static void Breakprefabref ()

{

var select = Selection.activegameobject;

if (select.activeinhierarchy)

{

Prefabutility.disconnectprefabinstance (select);

Selection.activegameobject = null;

var prefab = Prefabutility.createemptyprefab ("Assets/empty.prefab");

Prefabutility.replaceprefab (SELECT, prefab, Replaceprefaboptions.connecttoprefab);

Prefabutility.disconnectprefabinstance (select);

Assetdatabase.deleteasset ("Assets/empty.prefab");

}

}

Although the prefabutility.disconnectprefabinstance has the meaning of disconnecting prefab, but if only using this function will appear in the following case, the name of the color from blue to white, it does not appear to be a prefab, but from the Inspector panel can still see The three select, Revert, apply buttons that appear to the prefab tag and the prefab instance.

If you try to remove the source prefab from project at this time, you will see that the prefab tag disappears.

Therefore, the following method is used to implement the entire rundown reference

    • Use Selection.activegameobject to get the currently selected object
    • Use Prefabutility.createemptyprefab to create an empty prefab first
    • Use Prefabutility.replaceprefab to make a prefab of the selected prefab instances in the scene and overwrite the previous empty prefab
    • Using Prefabutility.disconnectprefabinstance to break references
    • To delete a newly created prefab in project using Assetdatabase.deleteasset

This completes the function of disconnecting references. Here, in the function Plus [MenuItem] tab, place this function on the Unity menu "tools->prefab-> Remove Reference".

Replace reference

The code to replace the reference is as follows

[MenuItem ("tools/prefab/Replacement Reference")]

public static void Relocalprefabref ()

{

var select = Selection.activegameobject;

if (select.activeinhierarchy)

{

var ab = prefabutility.getprefabparent (select);

if (AB = = null)

Return

var oripath = Assetdatabase.getassetpath (AB);

var filters = new[] {"Prefab file", "Prefab"};

var tar = editorutility.openfilepanelwithfilters ("Select Target", Application.datapath, filters);

if (string. IsNullOrEmpty (TAR))

Return

tar = Fileutil.getprojectrelativepath (TAR);

var tarprefab = assetdatabase.loadassetatpath<gameobject> (TAR);

if (Tarprefab = = null)

Return

var gname = Select.name;

var enable = Select.activeinhierarchy;

var pos = select.transform.localPosition;

var rot = select.transform.localRotation;

var scale = Select.transform.localScale;

var go = Prefabutility.connectgameobjecttoprefab (SELECT, Tarprefab);

Go.transform.localPosition = pos;

Go.transform.localRotation = rot;

Go.transform.localScale = scale;

Go.name = Gname;

Go. SetActive (enable);

Debug.logformat ("Replace Prefab from:{0} to {1}", Oripath, tar);

}

}

The main process in the code is

    • Use Selection.activegameobject to get the selected object
    • Use Prefabutility.getprefabparent to get the source of this object in Project prefab
    • Use Assetdatabase.getassetpath to get the path of the source prefab in project (later for log)
    • Open a File Selection window with Editorutility.openfilepanelwithfilters to let the player select a source prefab
    • Use Fileutil.getprojectrelativepath to get this source prefab path relative to the project
    • Use to load this source prefab into memory
    • In order to maintain the position relationship after the replacement, the original location information is recorded here.
    • Use Prefabutility.connectgameobjecttoprefab to re-link the selected object to the player's chosen source prefab to complete the prefab reference replacement
    • Restore the original location relationship

This completes the functionality of the Prefab instance replacement reference. Choose a menu tools->prefab-> replace reference in unity and choose a Prefab to implement the replacement

Reprint Reservation: Http://www.cnblogs.com/CodeGize http://www.codegize.com

Unity Plugin Development: PREFABUTILITY--PREFAB instance reference break and replace

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.