有没有办法破坏/替换Prefab实例内部或一部分的GameObject?

时间:2019-04-04 22:46:09

标签: c# unity3d

Exception

我在编辑器中遇到异常:

InvalidOperationException:不允许在Prefab实例中销毁GameObject。

然后,我必须手动打开预制板,然后从其中移除游戏对象。但是有没有办法通过脚本自动执行此操作?

这是我的脚本,用于用预制件替换游戏对象。 我对预制件所做的唯一更改是获取了一个gmeobject,并为其添加了一些颜色和纹理,所有脚本和其他内容都是相同的。

我正在尝试用预制件替换场景中的所有门,预制件也是一些颜色和纹理的门。

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class PrefabReplace : EditorWindow
{
    [SerializeField] private GameObject prefab;
    private bool selectionChanged;
    private string objectsToSearch = "";
    private List<GameObject> foundObjects = new List<GameObject>();
    private List<GameObject> duplicatedObjects = new List<GameObject>();
    private bool searched = false;
    private int count = 0;
    private int countChilds = 0;
    private bool countChildren = false;
    private GUIStyle guiStyle = new GUIStyle(); //create a new variable
    private Texture timage;

    [MenuItem("Tools/Prefab Replace")]
    static void CreateReplaceWithPrefab()
    {
        int width = 340;
        int height = 300;

        int x = (Screen.currentResolution.width - width) / 2;
        int y = (Screen.currentResolution.height - height) / 2;

        GetWindow<PrefabReplace>().position = new Rect(x, y, width, height);
    }

    private void OnGUI()
    {
        Texture oo = null;
        Texture texture = (Texture)oo;
        //EditorGUI.DrawTextureTransparent(new Rect(10, 10, 20, 20), timage);

        guiStyle.fontSize = 20; //change the font size
        Searching();
        GUILayout.Space(50);
        Replacing();
    }

    private void Searching()
    {
        GUI.Label(new Rect(10, 20, 150, 20), "Search by name", guiStyle);
        objectsToSearch = GUI.TextField(new Rect(90, 60, 150, 20), objectsToSearch, 25);

        if (objectsToSearch != "")
        {
            GUI.enabled = true;
        }
        else
        {
            GUI.enabled = false;
        }
        GUILayout.Space(40);
        if (GUILayout.Button("Search"))
        {
            foundObjects = new List<GameObject>();
            duplicatedObjects = new List<GameObject>();
            countChildren = true;
            countChilds = 0;
            count = 0;

            foreach (GameObject gameObj in GameObject.FindObjectsOfType<GameObject>())
            {
                if (gameObj.name == objectsToSearch)
                {
                    count += 1;
                    foundObjects.Add(gameObj);
                    Transform[] childs = gameObj.GetComponentsInChildren<Transform>();
                    foreach (Transform go in childs)
                    {
                        foundObjects.Add(go.gameObject);
                    }
                }
            }

            if (foundObjects.Count > 0)
            {
                searched = true;
            }
            else
            {
                searched = false;
            }
        }

        GUI.enabled = true;
        if (count > 0)
            GUI.TextField(new Rect(90, 85, 60, 15), count.ToString(), 25);

        if (foundObjects.Count > 0 && countChildren == true)
        {
            for (int i = 0; i < foundObjects.Count; i++)
            {
                if (foundObjects[i].transform.childCount > 0)
                {
                    countChilds += foundObjects[i].transform.childCount;
                }
            }

            countChildren = false;
        }
        GUI.enabled = true;
        if (countChilds > 0)
            GUI.TextField(new Rect(90, 105, 60, 15), countChilds.ToString(), 25);

        GUILayout.Space(100);

        if (foundObjects.Count > 0)
            EditorGUILayout.LabelField("Test");
    }

    private void Replacing()
    {
        GUILayout.Space(20);
        GUILayout.BeginVertical(GUI.skin.box);
        GUILayout.Label("Replacing");
        GUILayout.Space(20);

        prefab = (GameObject)EditorGUILayout.ObjectField("Prefab", prefab, typeof(GameObject), false);

        var selection = Selection.objects.OfType<GameObject>().ToList();
        if (selectionChanged)
        {
            if (selection.Count == 0)
                GUI.enabled = false;

            for (var i = selection.Count - 1; i >= 0; --i)
            {
                var selectedObject = selection[i];
                if (prefab != null && selection.Count > 0 &&
                    selectedObject.scene.name != null
                    && prefab != PrefabUtility
                    .GetCorrespondingObjectFromSource(selectedObject))
                {
                    GUI.enabled = true;
                }
                else
                {
                    GUI.enabled = false;
                }
            }
        }
        else
        {
            GUI.enabled = false;
        }

        if (GUILayout.Button("Replace"))
        {
            InstantiatePrefab(selection);
            selectionChanged = false;
        }

        GUILayout.Space(10);
        GUI.enabled = true;
        EditorGUILayout.LabelField("Selection count: " + Selection.objects.OfType<GameObject>().Count());

        GUILayout.EndVertical();
    }

    private void OnInspectorUpdate()
    {
        Repaint();
    }

    private void OnSelectionChange()
    {
        selectionChanged = true;
    }

    private void InstantiatePrefab(List<GameObject> selection)
    {
        if (prefab != null && selection.Count > 0)
        {
            for (var i = selection.Count - 1; i >= 0; --i)
            {
                var selected = selection[i];
                Component[] components = selected.GetComponents(typeof(MonoBehaviour));
                if (components.Length == 0)
                {
                    SceneManager.SetActiveScene(SceneManager.GetSceneByName(selected.scene.name));

                    var prefabType = PrefabUtility.GetPrefabType(prefab);
                    GameObject newObject;

                    if (prefabType == PrefabType.Prefab)
                    {
                        newObject = (GameObject)PrefabUtility.InstantiatePrefab(prefab);
                    }
                    else
                    {
                        newObject = Instantiate(prefab);
                        newObject.name = prefab.name;
                    }
                    if (newObject == null)
                    {
                        Debug.LogError("Error instantiating prefab");
                        break;
                    }

                    Undo.RegisterCreatedObjectUndo(newObject, "Replace With Prefabs");
                    newObject.transform.parent = selected.transform.parent;
                    newObject.transform.localPosition = selected.transform.localPosition;
                    newObject.transform.localRotation = selected.transform.localRotation;
                    newObject.transform.localScale = selected.transform.localScale;
                    newObject.transform.SetSiblingIndex(selected.transform.GetSiblingIndex());
                    Undo.DestroyObjectImmediate(selected);
                }
            }
        }
    }
}

所有门都被很好地替换了,但是屏幕截图中的门是唯一属于预制实例的门。

2 个答案:

答案 0 :(得分:0)

PrefabUtility.LoadPrefabContents()

此方法执行以下操作:

  

将给定路径上的Prefab资产加载到隔离的场景中,并返回Prefab的根GameObject。

     

您可以使用它来获取Prefab的内容并直接对其进行修改,而不用遍历Prefab的实例。这对于批处理操作很有用。

这将使您可以按照所需的方式修改预制件,

  

修改了预制件后,您必须使用SaveAsPrefabAsset写回,然后调用UnloadPrefabContents从内存中释放预制件和孤立的场景。

您必须再次将其保存回原始预制件并覆盖您的更改。

答案 1 :(得分:0)

我能做到,我解决了这个问题。只需销毁所有预制转换即可。我有 root _Canvas 和 Prefab _Panel。我像这样删除:

ImageIcon imageIcon = new ImageIcon(new URL(url)); // load the image to a imageIcon
Image image = imageIcon.getImage(); // transform it 
Image newimg = image.getScaledInstance(120, 120,  java.awt.Image.SCALE_SMOOTH); // scale it the smooth way  
imageIcon = new ImageIcon(newimg);
cp.add(new JLabel(imageIcon));

子对象是预制对象。