Search Unity

  1. Unity 2019.1 beta is now available.
    Dismiss Notice
  2. We're excited to be bringing you the newest Unity features, news, demos, and we've even got some special guests from some of the industry’s most storied franchises. Set a reminder to tune in on YouTube here
    Dismiss Notice
  3. Unity 2018.3 is now released.
    Dismiss Notice

[Solved] Property Drawer not calling OnGUI()

Discussion in 'Extensions & OnGUI' started by scmulagah, Dec 18, 2018.

  1. scmulagah

    scmulagah

    Joined:
    Jun 27, 2018
    Posts:
    12
    Calling debug on the script doesnt work either. There is no error or what not. Its just doesnt want to display.

    The script i am trying to draw a custom property drawer for.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CreepWalkerSpawner : MonoBehaviour
    6. {
    7.     public enum TravelType
    8.     {
    9.         Loop,
    10.         PingPong,
    11.     }
    12.  
    13.     public string creepWalkerPrefab;
    14.  
    15.     [Header("Settings")]
    16.     public List<Transform> visitPoints = new List<Transform>();
    17.     public TravelType travelType;
    18.  
    19.     private CreepWalker _currentWalker;
    20.  
    21.     private void OnDrawGizmos()
    22.     {
    23.         Gizmos.color = Color.cyan;
    24.         if (visitPoints.Count > 0)
    25.         {
    26.             Gizmos.DrawLine(transform.position, visitPoints[0].position);
    27.             for (int i = 0; i < visitPoints.Count; i++)
    28.             {
    29.                 if (visitPoints[i] == null)
    30.                 {
    31.                     visitPoints.RemoveAt(i);
    32.                     i = 0;
    33.                     continue;
    34.                 }
    35.  
    36.                 if ((i + 1) >= visitPoints.Count)
    37.                 {
    38.                     if (travelType == TravelType.Loop)
    39.                     {
    40.                         Gizmos.DrawLine(visitPoints[i].position, visitPoints[0].position);
    41.                     }
    42.                 }
    43.                 else
    44.                 {
    45.                     Gizmos.DrawLine(visitPoints[i].position, visitPoints[i + 1].position);
    46.                 }
    47.             }
    48.         }
    49.         Gizmos.color = Color.white;
    50.     }
    51.  
    52.     public void SpawnWalker()
    53.     {
    54.         if (_currentWalker != null)
    55.         {
    56.             return;
    57.         }
    58.  
    59.         GameObject creep = GameObject.Instantiate(EntityManager.instance.GetCreepData(creepWalkerPrefab).m_modelPrefab);
    60.  
    61.         Vector3 spawnPos = transform.position;
    62.         spawnPos.y = 0;
    63.  
    64.         creep.transform.position = spawnPos;
    65.         CreepWalker walker = creep.GetComponent<CreepWalker>();
    66.         walker.InitWalker(this);
    67.  
    68.         _currentWalker = walker;
    69.     }
    70. }
    71.  
    The custom property drawer code.
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. [CustomPropertyDrawer(typeof(CreepWalkerSpawner), true)]
    5. public class CreepWalkerSpawnerDrawer : PropertyDrawer
    6. {
    7.     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    8.     {
    9.         Debug.Log("HMM?");
    10.         base.OnGUI(position, property, label);
    11.  
    12.         EditorGUILayout.BeginVertical();
    13.         {
    14.             if (GUILayout.Button("Add Node"))
    15.             {
    16.                 GameObject node = new GameObject("Node");
    17.                 GameObject parent = fieldInfo.GetValue(property.serializedObject.targetObject) as GameObject;
    18.                 node.transform.SetParent(parent.transform);
    19.             }
    20.         }
    21.  
    22.         EditorGUILayout.EndVertical();
    23.     }
    24. }
    25.  
     
  2. samizzo

    samizzo

    Joined:
    Sep 7, 2011
    Posts:
    449
    So definitely no errors in the console when you click on a game object with the CreepWalkerSpawner component? And you haven't locked the inspector?

    Sam
     
  3. scmulagah

    scmulagah

    Joined:
    Jun 27, 2018
    Posts:
    12
    Yep the inspector is not locked. And i am sure there is no error
     
  4. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    684
    CreepWalkerSpawner is a MonoBehaviour. Use "Editor" instead of "PropertyDrawer":

    Code (CSharp):
    1. [CustomEditor(typeof(CreepWalkerSpawner))]
    2. public class CreepWalkerSpawnerEditor : Editor {
    3.  
    4. public override void OnInspectorGUI() {}
    5. }
    https://docs.unity3d.com/ScriptReference/Editor.html
     
    scmulagah likes this.
  5. scmulagah

    scmulagah

    Joined:
    Jun 27, 2018
    Posts:
    12
    Thanks. Got it to work.
     
  6. samizzo

    samizzo

    Joined:
    Sep 7, 2011
    Posts:
    449
    Woops! I totally overlooked that!