Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Prefabs not working!?!

Discussion in 'Editor & General Support' started by NintendoMaster00, Feb 18, 2016.

  1. NintendoMaster00

    NintendoMaster00

    Joined:
    Jan 31, 2015
    Posts:
    86
    I have been using a custom editor script that allows for tiling in Unity. It works fine but, when the script is created even if it is not being used. Prefabs will not work. I can "Create" them but I can't add them to the scene. If I delete the editor script and try to add it to the scene it works. If I try to drag a prefab into the scene when there is this script all it does is the mouse flashes and if I let go of click it acts as if did not try to add a prefab. The prefabs created while the script is active still work when the script is gone.


    The script is here:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Collections;
    4. using System.IO;
    5. using System.Linq;
    6. using System.Collections.Generic;
    7.  
    8. enum DRAWOPTION {select, paint, paintover, erase};
    9.  
    10. public class TileWindow : EditorWindow
    11. {
    12.     private static bool isEnabled;
    13.     private Vector2 _scrollPos;
    14.     private static Vector2 gridSize = new Vector2(0.32f, 0.32f);
    15.     private static bool isGrid;
    16.     private static bool isDraw;
    17.     private static bool addPolygonCollider;
    18.     private static bool isObjmode;
    19.     private static DRAWOPTION selected;
    20.     private static GameObject parentObj;
    21.     private static int layerOrd;
    22.     private static string tagName = "Untagged";
    23.     private int index;
    24.     private string[] options;
    25.     private Sprite[] allSprites;
    26.     private string[] files;
    27.     private static Sprite activeSprite;
    28.     private static GameObject activeGo;
    29.     public GUIStyle textureStyle;
    30.     public GUIStyle textureStyleAct;
    31.  
    32.     [MenuItem("TileEditor/TilemapEditor")]
    33.     private static void TilemapEditor()
    34.     {
    35.         EditorWindow.GetWindow(typeof (TileWindow));
    36.     }
    37.  
    38.     void Awake()
    39.     {
    40.  
    41.     }
    42.  
    43.     void Update()
    44.     {
    45.  
    46.     }
    47.  
    48.     public void OnInspectorUpdate()
    49.     {
    50.         // This will only get called 10 times per second.
    51.         Repaint();
    52.     }
    53.  
    54.     void OnEnable() {
    55.  
    56.         isEnabled = true;
    57.         Editor.CreateInstance(typeof(SceneViewEventHandler));
    58.     }
    59.  
    60.     void OnDestroy() {
    61.         isEnabled = false;
    62.     }
    63.  
    64.     public class SceneViewEventHandler : Editor
    65.     {
    66.         static SceneViewEventHandler()
    67.         {
    68.             SceneView.onSceneGUIDelegate += OnSceneGUI;
    69.         }
    70.  
    71.         static void OnSceneGUI(SceneView aView)
    72.         {
    73.             Event hotkey_e = Event.current;
    74.             switch (hotkey_e.type) {
    75.             case EventType.KeyDown:
    76.                 if (hotkey_e.shift)
    77.                 {
    78.                     switch (hotkey_e.keyCode) {
    79.                     case KeyCode.P:
    80.                         selected = DRAWOPTION.paint;
    81.                         break;
    82.                     case KeyCode.E:
    83.                         selected = DRAWOPTION.erase;
    84.                         break;
    85.                     case KeyCode.O:
    86.                         selected = DRAWOPTION.paintover;
    87.                         break;
    88.                     case KeyCode.S:
    89.                         selected = DRAWOPTION.select;
    90.                         break;
    91.                     case KeyCode.F:
    92.                         isDraw = !isDraw;
    93.                         break;
    94.                     case KeyCode.G:
    95.                         isGrid = !isGrid;
    96.                         break;
    97.                     }
    98.                 }
    99.                 break;
    100.             }
    101.  
    102.             if (isEnabled)
    103.             {
    104.                 if(isDraw)
    105.                 {
    106.                     Event e = Event.current;
    107.                     if (selected != DRAWOPTION.select)
    108.                     {
    109.                         HandleUtility.AddDefaultControl(GUIUtility.GetControlID(FocusType.Passive));
    110.                         if ((e.type == EventType.MouseDrag || e.type == EventType.MouseDown) && e.button == 0 && activeSprite != null)
    111.                         {
    112.                             Vector2 mousePos = Event.current.mousePosition;
    113.                             mousePos.y = SceneView.currentDrawingSceneView.camera.pixelHeight - mousePos.y;
    114.                             Vector3 mouseWorldPos = SceneView.currentDrawingSceneView.camera.ScreenPointToRay(mousePos).origin;
    115.                             mouseWorldPos.z = layerOrd;
    116.                             if (gridSize.x > 0.05f && gridSize.y > 0.05f)
    117.                             {
    118.                                 mouseWorldPos.x = Mathf.Floor(mouseWorldPos.x / gridSize.x) * gridSize.x + gridSize.x / 2.0f;
    119.                                 mouseWorldPos.y = Mathf.Ceil(mouseWorldPos.y / gridSize.y) * gridSize.y - gridSize.y / 2.0f;
    120.                             }
    121.                             if(isObjmode)
    122.                                 mouseWorldPos.z = mouseWorldPos.y + (activeSprite.bounds.size.y / -2.0f);
    123.                             GameObject[] allgo = GameObject.FindObjectsOfType(typeof (GameObject)) as GameObject[];
    124.                             int brk = 0;
    125.                             if (selected == DRAWOPTION.paint)
    126.                             {
    127.                                 for (int i = 0; i < allgo.Length;i++)
    128.                                 {
    129.                                     if (Mathf.Approximately(allgo[i].transform.position.x, mouseWorldPos.x) && Mathf.Approximately(allgo[i].transform.position.y, mouseWorldPos.y) && Mathf.Approximately(allgo[i].transform.position.z, mouseWorldPos.z))
    130.                                     {
    131.                                         brk++;
    132.                                         break;
    133.                                     }
    134.                                 }
    135.                                 if (brk == 0)
    136.                                 {
    137.                                     GameObject newgo = new GameObject(activeSprite.name, typeof(SpriteRenderer));
    138.                                     newgo.transform.position = mouseWorldPos;
    139.                                     newgo.GetComponent<SpriteRenderer>().sprite = activeSprite;
    140.                                     newgo.tag = tagName;
    141.                                     if (addPolygonCollider)
    142.                                         newgo.AddComponent<PolygonCollider2D>();
    143.                                     if (parentObj != null)
    144.                                         newgo.transform.parent = parentObj.transform;
    145.                                 }
    146.                             }
    147.                             else if (selected == DRAWOPTION.paintover)
    148.                             {
    149.                                 for (int i = 0; i < allgo.Length;i++)
    150.                                 {
    151.                                     if (allgo[i].GetComponent<SpriteRenderer>() != null & Mathf.Approximately(allgo[i].transform.position.x, mouseWorldPos.x) && Mathf.Approximately(allgo[i].transform.position.y, mouseWorldPos.y) && Mathf.Approximately(allgo[i].transform.position.z, mouseWorldPos.z))
    152.                                     {
    153.                                         allgo[i].GetComponent<SpriteRenderer>().sprite = activeSprite;
    154.                                         brk++;
    155.                                     }
    156.                                 }
    157.                                 if (brk == 0)
    158.                                 {
    159.                                     GameObject newgo = new GameObject(activeSprite.name, typeof(SpriteRenderer));
    160.                                     newgo.transform.position = mouseWorldPos;
    161.                                     newgo.GetComponent<SpriteRenderer>().sprite = activeSprite;
    162.                                     newgo.tag = tagName;
    163.                                     if (addPolygonCollider)
    164.                                         newgo.AddComponent<PolygonCollider2D>();
    165.                                 }
    166.                             }
    167.                             else if (selected == DRAWOPTION.erase)
    168.                             {
    169.                                 for (int i = 0; i < allgo.Length;i++)
    170.                                 {
    171.                                     if (Mathf.Approximately(allgo[i].transform.position.x, mouseWorldPos.x) && Mathf.Approximately(allgo[i].transform.position.y, mouseWorldPos.y) && Mathf.Approximately(allgo[i].transform.position.z, mouseWorldPos.z))
    172.                                         GameObject.DestroyImmediate(allgo[i]);
    173.                                 }
    174.                             }
    175.                         }
    176.                     }
    177.                     else
    178.                     {
    179.                         HandleUtility.AddDefaultControl(GUIUtility.GetControlID(FocusType.Passive));
    180.                         Vector2 mousePos = Event.current.mousePosition;
    181.                         mousePos.y = SceneView.currentDrawingSceneView.camera.pixelHeight - mousePos.y;
    182.                         Vector3 mouseWorldPos = SceneView.currentDrawingSceneView.camera.ScreenPointToRay(mousePos).origin;
    183.                         mouseWorldPos.z = layerOrd;      
    184.  
    185.                         if (e.type == EventType.MouseDown && e.button == 0)
    186.                         {
    187.                             Selection.activeGameObject = null;
    188.                             GameObject[] allgo = GameObject.FindObjectsOfType(typeof (GameObject)) as GameObject[];
    189.                             int brk = 0;
    190.                             for (int i = 0; i < allgo.Length;i++)
    191.                             {
    192.                                 if (allgo[i].GetComponent<SpriteRenderer>() != null && allgo[i].GetComponent<SpriteRenderer>().bounds.Contains(mouseWorldPos))
    193.                                 {
    194.                                     brk++;
    195.                                     activeGo = allgo[i];
    196.                                     break;
    197.                                 }
    198.                             }
    199.                             if (brk == 0)
    200.                                 activeGo = null;
    201.  
    202.                         }
    203.                         if (e.type == EventType.MouseDrag && e.button == 0 && activeGo != null)
    204.                         {
    205.                             if (gridSize.x > 0.05f && gridSize.y > 0.05f)
    206.                             {
    207.                                 mouseWorldPos.x = Mathf.Floor(mouseWorldPos.x / gridSize.x) * gridSize.x + gridSize.x / 2.0f;
    208.                                 mouseWorldPos.y = Mathf.Ceil(mouseWorldPos.y / gridSize.y) * gridSize.y - gridSize.y / 2.0f;
    209.                             }
    210.                             activeGo.transform.position = mouseWorldPos;
    211.                         }
    212.                     }
    213.                 }
    214.             }
    215.         }
    216.     }
    217.  
    218.     [CustomEditor(typeof(GameObject))]
    219.     public class SceneGUITest : Editor
    220.     {
    221.         [DrawGizmo(GizmoType.NotInSelectionHierarchy)]
    222.         static void RenderCustomGizmo(Transform objectTransform, GizmoType gizmoType)
    223.         {
    224.             if (isEnabled && isGrid)
    225.             {
    226.                 Gizmos.color = Color.white;
    227.                 Vector3 minGrid = SceneView.currentDrawingSceneView.camera.ScreenPointToRay(new Vector2(0f, 0f)).origin;
    228.                 Vector3 maxGrid = SceneView.currentDrawingSceneView.camera.ScreenPointToRay(new Vector2(SceneView.currentDrawingSceneView.camera.pixelWidth, SceneView.currentDrawingSceneView.camera.pixelHeight)).origin;
    229.                 for (float i = Mathf.Round(minGrid.x / gridSize.x) * gridSize.x; i < Mathf.Round(maxGrid.x / gridSize.x) * gridSize.x && gridSize.x > 0.05f; i+=gridSize.x)
    230.                     Gizmos.DrawLine(new Vector3(i,minGrid.y,0.0f), new Vector3(i,maxGrid.y,0.0f));
    231.                 for (float j = Mathf.Round(minGrid.y / gridSize.y) * gridSize.y; j < Mathf.Round(maxGrid.y / gridSize.y) * gridSize.y && gridSize.y > 0.05f; j+=gridSize.y)
    232.                     Gizmos.DrawLine(new Vector3(minGrid.x,j,0.0f), new Vector3(maxGrid.x,j,0.0f));
    233.                 SceneView.RepaintAll();
    234.             }
    235.         }
    236.  
    237.     }
    238.  
    239.     void OnGUI()
    240.     {
    241.         textureStyle = new GUIStyle(GUI.skin.button);
    242.         textureStyle.margin = new RectOffset(2,2,2,2);
    243.         textureStyle.normal.background = null;
    244.         textureStyleAct = new GUIStyle(textureStyle);
    245.         textureStyleAct.margin = new RectOffset(0,0,0,0);
    246.         textureStyleAct.normal.background = textureStyle.active.background;
    247.  
    248.         if (!Directory.Exists(Application.dataPath + "/Tilemaps/"))
    249.         {
    250.             //Directory.CreateDirectory(Application.dataPath + "/Tilemaps/");
    251.             AssetDatabase.CreateFolder("Assets", "Tilemaps");
    252.             AssetDatabase.Refresh();
    253.             Debug.Log("Created Tilemaps Directory");
    254.         }
    255.         files = Directory.GetFiles(Application.dataPath + "/Tilemaps/", "*.png");
    256.         options = new string[files.Length];
    257.         EditorGUILayout.LabelField("Tile Map", GUILayout.Width(256));
    258.         for(int i = 0; i < files.Length; i++)
    259.         {
    260.             options[i] = files[i].Replace(Application.dataPath + "/Tilemaps/", "");
    261.         }
    262.         index = EditorGUILayout.Popup(index, options, GUILayout.Width(256));
    263.         GUILayout.BeginHorizontal();
    264.         isGrid = EditorGUILayout.Toggle(isGrid, GUILayout.Width(16));
    265.         gridSize = EditorGUILayout.Vector2Field("Grid Size (0.05 minimum)", gridSize,  GUILayout.Width(236));
    266.         GUILayout.EndHorizontal();
    267.  
    268.         EditorGUILayout.LabelField("Parent Object", GUILayout.Width(256));
    269.         parentObj = (GameObject)EditorGUILayout.ObjectField(parentObj, typeof(GameObject),true,GUILayout.Width(256));
    270.  
    271.         GUILayout.BeginHorizontal();
    272.         addPolygonCollider = EditorGUILayout.Toggle(addPolygonCollider, GUILayout.Width(16));
    273.         EditorGUILayout.LabelField("Add Polygon Collider", GUILayout.Width(256));
    274.         GUILayout.EndHorizontal();
    275.  
    276.         EditorGUILayout.LabelField("Layer Order", GUILayout.Width(256));
    277.  
    278.         GUILayout.BeginHorizontal();
    279.         layerOrd = EditorGUILayout.IntField(layerOrd,  GUILayout.Width(126));
    280.         isObjmode = EditorGUILayout.Toggle(isObjmode, GUILayout.Width(16));
    281.         EditorGUILayout.LabelField("Layer based on Y", GUILayout.Width(110));
    282.         GUILayout.EndHorizontal();
    283.  
    284.         EditorGUILayout.LabelField("Tag", GUILayout.Width(32));
    285.         GUILayout.BeginHorizontal();
    286.         tagName = EditorGUILayout.TagField(tagName, GUILayout.Width(236));
    287.         GUILayout.EndHorizontal();
    288.  
    289.         GUILayout.BeginHorizontal();
    290.         isDraw = EditorGUILayout.Toggle(isDraw, GUILayout.Width(16));
    291.         selected = (DRAWOPTION)EditorGUILayout.EnumPopup(selected, GUILayout.Width(236));
    292.         GUILayout.EndHorizontal();
    293.  
    294.         _scrollPos = EditorGUILayout.BeginScrollView(_scrollPos);
    295.         GUILayout.BeginHorizontal();
    296.         float ctr = 0.0f;
    297.         if (options.Length > index)
    298.         {
    299.         allSprites = AssetDatabase.LoadAllAssetsAtPath("Assets/Tilemaps/" + options[index]).Select(x => x as Sprite).Where(x => x != null).ToArray();  
    300.         foreach(Sprite singsprite in allSprites)
    301.         {
    302.             if (ctr > singsprite.textureRect.x)
    303.             {
    304.                 GUILayout.EndHorizontal();
    305.                 GUILayout.BeginHorizontal();
    306.             }
    307.             ctr = singsprite.textureRect.x;
    308.             if (activeSprite == singsprite)
    309.             {
    310.                 GUILayout.Button("", textureStyleAct, GUILayout.Width(singsprite.textureRect.width + 6), GUILayout.Height(singsprite.textureRect.height + 4));
    311.                 GUI.DrawTextureWithTexCoords(new Rect(GUILayoutUtility.GetLastRect().x + 3f,
    312.                                                       GUILayoutUtility.GetLastRect().y + 2f,
    313.                                                       GUILayoutUtility.GetLastRect().width - 6f,
    314.                                                       GUILayoutUtility.GetLastRect().height - 4f),
    315.                                              singsprite.texture,
    316.                                              new Rect(singsprite.textureRect.x / (float)singsprite.texture.width,
    317.                          singsprite.textureRect.y / (float)singsprite.texture.height,
    318.                          singsprite.textureRect.width / (float)singsprite.texture.width,
    319.                          singsprite.textureRect.height / (float)singsprite.texture.height));
    320.             }
    321.             else
    322.             {
    323.                 if (GUILayout.Button("", textureStyle, GUILayout.Width(singsprite.textureRect.width + 2), GUILayout.Height(singsprite.textureRect.height + 2)))
    324.                     activeSprite = singsprite;
    325.                 GUI.DrawTextureWithTexCoords(GUILayoutUtility.GetLastRect(), singsprite.texture,
    326.                                              new Rect(singsprite.textureRect.x / (float)singsprite.texture.width,
    327.                                                          singsprite.textureRect.y / (float)singsprite.texture.height,
    328.                                                          singsprite.textureRect.width / (float)singsprite.texture.width,
    329.                                                          singsprite.textureRect.height / (float)singsprite.texture.height));
    330.             }
    331.         }
    332.         }
    333.         GUILayout.EndHorizontal();
    334.         EditorGUILayout.EndScrollView();
    335.         SceneView.RepaintAll();
    336.     }
    337. }
     
    Last edited: Feb 24, 2016
  2. NintendoMaster00

    NintendoMaster00

    Joined:
    Jan 31, 2015
    Posts:
    86
    Bump I still have no idea what is going on here and I need some help.
     
  3. aer0ace

    aer0ace

    Joined:
    May 11, 2012
    Posts:
    1,511
    That's a lot of moving pieces, any of which can be the cause of your issue. Take this opportunity to practice debugging systematically. Comment out some parts of your code and test each aspect of it to make sure it's working before adding more complicated parts to the system.