Search Unity

Editor Script for Scriptable Object

Discussion in 'Scripting' started by gibberingmouther, Oct 23, 2017.

  1. gibberingmouther

    gibberingmouther

    Joined:
    Dec 13, 2016
    Posts:
    259
    so i'm working on this:
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEditor;
    6.  
    7. namespace CoS
    8. {
    9.     [CustomEditor(typeof(ItemHolder))]
    10.     public class InspectorItemHolder : Editor
    11.     {
    12.  
    13.         ItemHolder itemh;
    14.  
    15.         void OnEnable()
    16.         {
    17.             itemh = (ItemHolder)target;
    18.         }
    19.  
    20.         public override void OnInspectorGUI()
    21.         {
    22.             itemh.quantity = EditorGUILayout.IntField("Quantity: ", itemh.quantity);
    23.  
    24.             EditorGUILayout.Space();
    25.  
    26.             itemh.item = EditorGUILayout.ObjectField("Item: ", itemh.item);
    27.  
    28.  
    29.             if (GUI.changed)
    30.                 EditorUtility.SetDirty(itemh);
    31.         }
    32.     }
    33. }
    34.  
    it says ObjectField is deprecated (and my syntax may be wrong) so what do i use instead? and do i have all the bracketed []'s i need?
     
  2. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    It's likely only the specific overload (string, object) that's deprecated- use the one that includes the specific type in the object picker, and whether to include scene objects in the search (string, object, type, bool) and it should work fine. You may need to cast the result back to the Item class.
     
    gibberingmouther likes this.
  3. gibberingmouther

    gibberingmouther

    Joined:
    Dec 13, 2016
    Posts:
    259
    i'm having trouble with the casting and i did a google search. how do i cast item or itemholder.item to an object?
     
  4. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    It should cast to Object automatically I should think, so this isn't necessary, but for the record it's like this:
    Code (CSharp):
    1. itemh.item = EditorGUILayout.ObjectField("Item: ", (Object)itemh.item);
    2.  
    3. // ... or...
    4.  
    5. itemh.item = EditorGUILayout.ObjectField("Item: ", itemh.item as Object);
    What I meant was that the ObjectField actually returns an Object, so you likely need to cast it back to whatever your Item class is. Like this:
    Code (CSharp):
    1. itemh.item = (Item)EditorGUILayout.ObjectField("Item: ", itemh.item, typeof(Item), true);
    ... but replace "Item" with whatever your actual item class is called.
     
    gibberingmouther likes this.