static void OnSavedScene()
    {
        var behaviours = GameObject.FindObjectsOfType(typeof(MonoBehaviour));
        var assemblers = new HashSet <PrefabAssembler>();

        foreach (MonoBehaviour b in behaviours)
        {
            if (b.GetType().GetCustomAttributes(typeof(AssembleOnSave), true).Length == 0)
            {
                continue;
            }
            Transform target = b.transform;
            while (target)
            {
                var a = target.GetComponent <PrefabAssembler>();
                if (a)
                {
                    assemblers.Add(a);
                }
                target = target.parent;
            }
        }
        if (assemblers.Count != 0)
        {
            PrefabAssembler.IsSaving = true;
            PrefabAssemblerUtility.Assemble(assemblers.ToArray());
            PrefabAssembler.IsSaving = false;
        }
    }
    static void InstanceAssemblerMenuItem()
    {
        if (Selection.objects.Length != 1)
        {
            return;
        }

        if (!Selection.activeGameObject)
        {
            return;
        }

        PrefabAssembler assembler = Selection.activeGameObject.GetComponent <PrefabAssembler>();

        if (!assembler)
        {
            var path = EditorUtility.SaveFilePanelInProject("Pick Prefab Path", Selection.activeGameObject.name, "prefab", "Pick a location to save the prefab.");
            if (path == null || path == "")
            {
                return;
            }

            assembler = Selection.activeGameObject.AddComponent <PrefabAssembler>();

            var t = AssetDatabase.LoadAssetAtPath(path, typeof(GameObject)) as GameObject;
            if (t)
            {
                assembler.prefab = t;
                PrefabAssemblerUtility.Assemble(assembler);
            }
            else
            {
                var go = new GameObject();
                assembler.prefab = PrefabUtility.CreatePrefab(path, go);
                PrefabAssemblerUtility.Assemble(assembler);
                GameObject.DestroyImmediate(go);
            }
        }

        var instance = PrefabUtility.InstantiatePrefab(assembler.prefab) as GameObject;

        instance.transform.position = assembler.transform.position;
        instance.transform.rotation = assembler.transform.rotation;
        instance.transform.parent   = assembler.transform.parent;

        Selection.activeObject = assembler.gameObject;
    }
    public static void SetAssemblerTarget(PrefabAssembler assembler, string path)
    {
        var t = AssetDatabase.LoadAssetAtPath(path, typeof(GameObject)) as GameObject;

        if (t)
        {
            assembler.prefab = t;
        }
        else
        {
            var go = new GameObject();
            assembler.prefab = PrefabUtility.CreatePrefab(path, go);
            PrefabAssemblerUtility.Assemble(assembler);
            EditorUtility.SetDirty(assembler);
            AssetDatabase.Refresh();
            GameObject.DestroyImmediate(go);
        }
    }
    static void NewStagingScene()
    {
        if (!EditorApplication.SaveCurrentSceneIfUserWantsTo())
        {
            return;
        }

        var path = EditorUtility.SaveFilePanelInProject("Pick Prefab Path", Selection.activeGameObject.name, "prefab", "Pick a location to save the prefab.");

        if (path == null || path == "")
        {
            return;
        }

        EditorApplication.NewScene();
        GameObject.DestroyImmediate(Camera.main.gameObject);

        var file       = new FileInfo(path);
        var name       = file.Name;
        var gameObject = new GameObject(name);
        var assembler  = gameObject.AddComponent <PrefabAssembler>();

        var t = AssetDatabase.LoadAssetAtPath(path, typeof(GameObject)) as GameObject;

        if (t)
        {
            assembler.prefab = t;
            PrefabAssemblerUtility.Assemble(assembler);
        }
        else
        {
            var go = new GameObject();
            assembler.prefab = PrefabUtility.CreatePrefab(path, go);
            PrefabAssemblerUtility.Assemble(assembler);
            GameObject.DestroyImmediate(go);
        }

        Selection.activeGameObject = gameObject;
    }
Ejemplo n.º 5
0
    static void OnHierarchyWindowItemGUI(int instanceID, Rect selectionRect)
    {
        var go = EditorUtility.InstanceIDToObject(instanceID) as GameObject;

        if (!go)
        {
            return;
        }

        var assembler = go.GetComponent <PrefabAssembler>();

        if (!assembler)
        {
            return;
        }

        var size = GUI.skin.label.CalcSize(new GUIContent(go.name));

        selectionRect.x += size.x;

        var label = new GUIContent("[P]");
        var color = new Color(0.15f, 0.15f, 0.15f, 1f);

        if (!assembler.prefab)
        {
            label = new GUIContent("[X]");
            color = new Color(1f, 0.25f, 0.25f, 1f);
        }

        size = GUI.skin.label.CalcSize(label);
        selectionRect.width = size.x;

        EditorGUIUtility.AddCursorRect(selectionRect, MouseCursor.Link);

        GUI.color = color;
        GUI.Label(selectionRect, label);
        GUI.color = Color.white;
        if (selectionRect.Contains(Event.current.mousePosition))
        {
            if (Event.current.type == EventType.MouseDown && Event.current.button == 0)
            {
                if (assembler.prefab)
                {
                    EditorGUIUtility.PingObject(assembler.prefab);
                }
                else
                {
                    BrowsePrefabPath(assembler);
                }
            }
            if ((Event.current.type == EventType.MouseDown && Event.current.button == 2) ||
                (Event.current.type == EventType.MouseDown && Event.current.button == 0 && Event.current.command))
            {
                if (assembler.prefab)
                {
                    PrefabAssemblerUtility.Assemble(new PrefabAssembler[] { assembler });
                }
                else
                {
                    BrowsePrefabPath(assembler);
                }
            }
        }
    }