Custom Editor from a Non-Monobehavior Script
So I am new to a custom editor, but the system I want to write is big for my production. I am essentially working on an item library for the random drops when enemies die, or what a chest/pickup box should be filled with.
I have a simple item script (below) using UnityEngine; using System.Collections;
public enum ItemType
{
Junk,
Quest,
Weapon,
Trap,
Health,
AntiVirus,
Armor
}
public enum WeaponType
{
Ranged,
Melee,
Thrown
}
public enum HandRequirements
{
OneHanded,
TwoHanded
}
public enum ArmorType
{
Head,
Torso,
Hands,
Legs,
Feet
}
[System.Serializable]
public class Item: MonoBehaviour
{
//ItemInfo itemInfo;
public ItemType itemType;
public string itemName;
public string itemDescription;
public int weight;
public int value;
public int hpValue {get;set;}
public int avValue {get;set;}
public int tier {get;set;}
public int questID {get;set;}
public Vector2 attackValue {get;set;}
public WeaponType weaponType {get;set;}
public HandRequirements handRequirements {get;set;}
public Vector2 defenseValue {get;set;}
public ArmorType armorType {get;set;}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
and a custom editor, which so far works pretty fine. (below)
using UnityEngine;
using System.Collections;
using UnityEditor;
[CanEditMultipleObjects]
[CustomEditor (typeof(Item)), CanEditMultipleObjects]
public class ItemEditor : Editor
{
public override void OnInspectorGUI ()
{
Item myItem = (Item)target;
DrawDefaultInspector();
switch (myItem.itemType)
{
case ItemType.AntiVirus:
myItem.avValue = EditorGUILayout.IntField ("AntiVirus Effectiveness",myItem.avValue);
if (myItem.tier > 4)
{
myItem.tier = 4;
}
if (myItem.tier < 1)
{
myItem.tier = 1;
}
break;
case ItemType.Armor:
//myItem.armorType = EditorGUILayout.EnumPopup ("Armor Type", myItem.armorType);
myItem.defenseValue = EditorGUILayout.Vector2Field ("Defense Value", myItem.defenseValue);
break;
case ItemType.Health:
myItem.tier = EditorGUILayout.IntField("Class", myItem.tier);
myItem.hpValue = EditorGUILayout.IntField ("AntiVirus Effectiveness",myItem.hpValue);
if (myItem.tier > 4)
{
myItem.tier = 4;
}
if (myItem.tier < 1)
{
myItem.tier = 1;
}
break;
case ItemType.Quest:
myItem.questID = EditorGUILayout.IntField ("Class",myItem.questID);
break;
case ItemType.Trap:
myItem.tier = EditorGUILayout.IntField ("Trap Tier",myItem.tier);
myItem.attackValue = EditorGUILayout.Vector2Field ("Trap Damage", myItem.attackValue);
break;
case ItemType.Weapon:
myItem.attackValue = EditorGUILayout.Vector2Field ("Attack Value",myItem.attackValue);
break;
}
}
}
The problem is, I want to turn the item script into a non-monobehavior class and have it accessed as an array of "Item" from another script. When i do that the editor script throws an error that I do not know how to fix. (below)
Cannot convert type UnityEngine.Object' to
Item'
If anyone knows how to fix my problem and/or a better way to go about writing this please let me know. I appreciate your time!
Your answer
![](https://koobas.hobune.stream/wayback/20220612053233im_/https://answers.unity.com/themes/thub/images/avi.jpg)