Custom Inspector: Instantiate Prefab in view

Hi friends,
I try to make my work easier and decided to create a custom inspector script for that :wink: …I’m trying to instantiate an prefab from my assets folder in scene view, what would you suggest will be the finest solution ?
My code so far:

using UnityEditor;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class CostumInspectorTest{
public  static GameObject tempObject;

[MenuItem("Tools/Create/Barrel", false, 101)]
    public static void InsertPrefab(string objectName)
    {
     objectName = "Barrel";
     string fileLocation="Assets/_MyStuff/Prefabs/"+objectName+".prefab";
     tempObject=PrefabUtility.CreateEmptyPrefab(fileLocation);
     Debug.Log("Barrel created!");
    }
}

Any suggestion for achieving this ? Thanks !

I believe this may of be help to you, creates the prefab in the scene and keeps the link. :slight_smile:

edit: So in your case I believe you will just pass in tempObject, and it will return the object in the scene.

Hi GiTS_ ,
I tried what you suggest :

[MenuItem("Tools/Create/Barrel", false, 101)]
    public static void InsertPrefab(string objectName)
    {
     objectName = "Barrel";
     string fileLocation="Assets/_MyStuff/Prefabs/"+objectName+".prefab";
     tempObject=PrefabUtility.CreatePrefab(fileLocation, tempObject);
if (tempObject== null)
            return false;
        return PrefabUtility.GetPrefabType(tempObject) == PrefabType.Prefab || PrefabUtility.GetPrefabType(tempObject) == PrefabType.ModelPrefab;
     Debug.Log("Barrel created!");
    }
}

…and I get this:
Assets/_MyStuff/Editor/CostumInspectorTest.cs(149,24): error CS0501: `CostumInspectorTest.InsertPrefab()’ A return keyword must not be followed by any expression when method returns void

Cannot implicitly convert type UnityEditor.PrefabType' to void’

*Sorry but I’m very new in Unity and definitely in C# :wink:

Your return type for the method is void, but you are returning false and another bool with the second return. The page I linked for some reason doesn’t have a C# example, just JS so partially my fault as I didn’t notice that.

What you want is something like this,

public class CostumInspectorTest
{
    public  static GameObject tempObject;

    [MenuItem("Tools/Create/Barrel", false, 101)]
    public static void InsertPrefab(string objectName)
    {
        objectName = "Barrel";
        string fileLocation="Assets/_MyStuff/Prefabs/" + objectName + ".prefab";
        tempObject = PrefabUtility.CreateEmptyPrefab(fileLocation) as GameObject;

        if (tempObject != null)
        {
            PrefabUtility.InstantiatePrefab(tempObject);
            Debug.Log("Barrel created!");
        }
        else
        {
            Debug.Log("Unabled to create barrel.");
        }
    }
}

Also you need to do “as GameObject” with CreateEmptryPrefab because it returns an Object and not a GameObject. If you want to reference the prefab object that is in the scene, then you need to add “as GameObject” after InstantiatePrefab as well.

@ now when I’m pressing Create—>Barrel I get : Debug.Log("Unabled to create barrel.");
And in my path appears (empty prefab I think):

In my prefabs folder I have already the barrel prefab…I just need to insert the prefab from location in my scene :frowning:
What I’m doing wrong ?! Thanks a lot for your support!

Right, so it turns out the prefab is not a GameObject (this is new to me as well, never had to create an empty prefab before :p), but, we can replace it with a GameObject!

public class CostumInspectorTest
{
    [MenuItem("Tools/Create/Barrel", false, 101)]
    public static void InsertPrefab(string objectName)
    {
        objectName = "Barrel";
        string fileLocation="Assets/_MyStuff/Prefabs/" + objectName + ".prefab";

        // Try and load an existing prefab, if it's null then it does not exist.
        GameObject barrelPrefab = AssetDatabase.LoadAssetAtPath(fileLocation, typeof(GameObject)) as GameObject;

        if (barrelPrefab == null)
        {
            Object tempObject = PrefabUtility.CreateEmptyPrefab(fileLocation);
            barrelPrefab = PrefabUtility.ReplacePrefab(new GameObject(objectName), tempObject, ReplacePrefabOptions.ConnectToPrefab);
        }

        if (barrelPrefab != null)
        {
            PrefabUtility.InstantiatePrefab(barrelPrefab);
            Debug.Log("Barrel created!");
        }
        else
        {
            Debug.Log("Unable to create barrel.");
        }
    }
}

edit: fixed some issues in the original code I posted.
edit2: Because I am in the strangest generous mood today, added it so it checks if the prefab already exists

1 Like

GiTS_ now I get this kind of error:

Sorry I’ve edited the code above, make tempObject an Object instead of a GameObject.

1 Like

Thanks a lot GiTS_ work’s perfect now, like this :

public class CostumInspectorTest
{
    [MenuItem("Tools/Create/Barrel", false, 101)]
    public static void InsertPrefab()
    {
        string objectName = "Barrel";
        string fileLocation="Assets/_MyStuff/Prefabs/" + objectName + ".prefab";
        // Try and load an existing prefab, if it's null then it does not exist.
        GameObject barrelPrefab = AssetDatabase.LoadAssetAtPath(fileLocation, typeof(GameObject)) as GameObject;
        if (barrelPrefab == null)
        {
             //REMOVED THIS LINES BECAUSE I DON'T NEED TO CREATE A NEW PREFAB
            //Object tempObject = PrefabUtility.CreateEmptyPrefab(fileLocation);
            //barrelPrefab = PrefabUtility.ReplacePrefab(new GameObject(objectName), tempObject, ReplacePrefabOptions.ConnectToPrefab);
Debug.Log("Unable to create barrel.");
        }
        if (barrelPrefab != null)
        {
            PrefabUtility.InstantiatePrefab(barrelPrefab);
//HOW TO ADJUST THE PREFAB POSITION TO BE INSTANTIATED INTO THE SCENE WHERE I'M LOOKING
//LIKE CURRENT POSITION ...CAN THIS BE DONE ?
barrelPrefab.transform.position = new Vector3(0f,0f,0f);
            Debug.Log("Barrel created!");
        }       
    }
}

Thanks again, you helped me a lot !!!