Does anyone want a free prefab brush?

Very simple and crude. I may continue complete it if I finish my maths homework lol.

using UnityEngine;
using UnityEditor;
using System.Collections;

class MyPrefabBrush : EditorWindow {
    GameObject prefab=null;
    string text;
    bool alignNormal=true;
    float size1=1,size2=1;
    bool painting=false;
    bool shiftOrigin=false;
    enum RandomRotateMode{
        None,Circle,Sphere
    };
    RandomRotateMode randomRotateMode=RandomRotateMode.None;
    [MenuItem ("Window/MyPrefabBrush")]
    public static void  ShowWindow () {
        EditorWindow.GetWindow(typeof(MyPrefabBrush));
    }
    void OnEnable(){
        SceneView.onSceneGUIDelegate += OnSceneGUI;
    }
    void OnDisable(){
        SceneView.onSceneGUIDelegate -= OnSceneGUI;
    }
    void OnGUI () {
        EditorGUILayout.LabelField ("Right Mouse Button to Paint");
        prefab = EditorGUILayout.ObjectField ("Prefab",prefab,typeof(GameObject)) as GameObject;
        EditorGUILayout.MinMaxSlider ("Random Scale", ref size1, ref size2,0,3);
        alignNormal = EditorGUILayout.Toggle ( "Align Normal",alignNormal);
        shiftOrigin = EditorGUILayout.Toggle ( "Shift Origin",shiftOrigin);
        randomRotateMode=(RandomRotateMode)EditorGUILayout.EnumPopup ("Rotate Mode", randomRotateMode);
        if (!painting) {
            if (GUILayout.Button ("Start Painting"))
                painting = true;
        } else {
            if (GUILayout.Button ("Stop Painting"))
                painting = false;
        }
    }
    void OnSceneGUI(SceneView sv){
        Event e = Event.current;
        if (painting && e.type==EventType.MouseDown && e.button==1 && e.alt==false && e.control==false && e.shift==false) {
            Ray r = Camera.current.ScreenPointToRay (new Vector3 (e.mousePosition.x, -e.mousePosition.y + Camera.current.pixelHeight));
            RaycastHit rh;
            if(Physics.Raycast (r,out rh)){
                if (prefab != null) {
                    var o = PrefabUtility.InstantiatePrefab (prefab) as GameObject;
                    o.transform.position = rh.point;

                    Quaternion q = Quaternion.identity;
                    if (alignNormal)
                        q = Quaternion.FromToRotation (Vector3.up, rh.normal) * q;
                    if (randomRotateMode == RandomRotateMode.Circle)
                        q = q * Quaternion.Euler (0, Random.Range (0, 360), 0);
                    if (randomRotateMode == RandomRotateMode.Sphere)
                        q = Random.rotationUniform;

                    o.transform.rotation = q*o.transform.rotation;
                    o.transform.localScale =o.transform.localScale * Random.Range (size1, size2);

                    GameObject[] go = { o };
                    Selection.objects = go;

                    Undo.RegisterCreatedObjectUndo (o,"Paint Prefab "+o.name);



                    //e.Use ();
                }
            }
        }
    }
}
2 Likes

What’s new?