- Home /
I have a custom editor for a class which work's fine, but how do i refrence it in another class and get it ti appear?
I have been working on a dialog/quest system for a game im working on and after a bit of messing i decided to make a dialog class which had different states depending on what i needed it to do then a conversation class which is attached to the npc that handles the conversation. But after fiddling with making a custom editor for the dialog class which works fine if i inherit from misbehavior and attach it to an object, but i need that editor to appear when its referenced from the conversation class, so i can have an array of dialogs.
Here are my classes sorry there a bit messy early stages :) :
Dialog:
using UnityEngine;
using System;
using System.Collections.Generic;
[Serializable]
public class Dialog
{
//Type of Dialog it is
public DialogType type;
//Bass Variables
public int id;
public string text;
//Var's for Basic Dialog
public int nextDialog;
//Var's for Dialog With Options
public Option[] options;
//Var's for Quest
public Quest quest;
public Dialog (string text, int id, int nextDialog, DialogType type)
{
this.text = text;
this.id = id;
this.nextDialog = nextDialog;
this.type = type;
}
#region Getters and Setters
public int GetID ()
{
return id;
}
public void SetID (int id)
{
this.id = id;
}
public string GetText ()
{
return text;
}
public void SetText (string text)
{
this.text = text;
}
public int GetNextDialog ()
{
return nextDialog;
}
#endregion
public enum DialogType
{
BasicDialog,
DialogWithOptions,
Quest
}
}
DialogEditor:
[CustomEditor(typeof(Dialog)), CanEditMultipleObjects]
public class DialogEditor : Editor {
public SerializedProperty
type_Prop,
id_Prop,
text_Prop,
nextDialog_Prop,
options_Prop,
quest_Prop;
private void OnEnable ()
{
//Setup Serialized properties
type_Prop = serializedObject.FindProperty("type");
id_Prop = serializedObject.FindProperty("id");
text_Prop = serializedObject.FindProperty("text");
nextDialog_Prop = serializedObject.FindProperty("nextDialog");
options_Prop = serializedObject.FindProperty("options");
quest_Prop = serializedObject.FindProperty("quest");
}
public override void OnInspectorGUI()
{
serializedObject.Update();
EditorGUILayout.PropertyField(type_Prop);
Dialog.DialogType types = (Dialog.DialogType)type_Prop.enumValueIndex;
switch(types)
{
case Dialog.DialogType.BasicDialog:
Debug.Log("Basic");
EditorGUILayout.IntField("ID: ", id_Prop.intValue);
EditorGUILayout.TextField("Text: ", text_Prop.stringValue);
EditorGUILayout.IntField("Next Dialog: ", nextDialog_Prop.intValue);
break;
case Dialog.DialogType.DialogWithOptions:
Debug.Log("Dialog");
EditorGUILayout.IntField("ID: ", id_Prop.intValue);
EditorGUILayout.TextField("Text: ", text_Prop.stringValue);
EditorGUILayout.PropertyField(options_Prop);
break;
}
serializedObject.ApplyModifiedProperties();
}
}
Conversation:
using UnityEngine;
using System.Collections.Generic;
public class Conversation : MonoBehaviour {
public Dialog[] dialogs;
public int startingDialog = 1;
private Dialog currentDialog = null;
private GameObject player;
private bool isConversing = false;
private string toolTip = "";
// Use this for initialization
void Start () {
player = GameObject.FindGameObjectWithTag("Player");
}
void Update()
{
float distance = Vector3.Distance(this.transform.position, player.transform.position);
//Debug.Log (distance);
if(distance < 5 && !isConversing && Input.GetButtonDown("Action"))
{
isConversing = true;
BeginConversation();
}
else if(distance < 5 && isConversing && Input.GetButtonDown("Action"))
{
NextDialog(currentDialog.GetNextDialog());
}
else
{
toolTip = "";
}
}
private void OnGUI()
{
}
private void BeginConversation()
{
if(currentDialog == null)
{
currentDialog = GetDialogByID(startingDialog);
}
Debug.Log(currentDialog.GetText());
}
private Dialog GetDialogByID(int id)
{
foreach(Dialog dialog in dialogs)
{
if(dialog.GetID() == id)
{
return dialog;
}
}
Debug.LogError("Dialog with ID " + id + " dosnt exsist!!");
return null;
}
private void NextDialog(int id)
{
if(id != 0)
{
currentDialog = GetDialogByID(id);
Debug.Log(currentDialog.GetText());
NextDialog(currentDialog.GetNextDialog());
}
else
{
ExitConversation();
}
}
private void ExitConversation()
{
currentDialog = GetDialogByID(startingDialog);
isConversing = false;
}
}
This is what it looks like if i inherit dialog from MonoBehaivier.
This is what i get if i use it in the conversation class without inheriting.
I'm really stuck so any help would be greatly appreciated :)
Thanks, Adam.
Answer by Jamora · Nov 06, 2013 at 05:55 AM
You will have to create a Property Drawer. You already have the drawing logic in your inspector, so you're more than halfway there.
Alternatively, you can create a custom inspector for the Conversation class, in which you display the array of Dialogs the way you want.
Another possibility still, (possibly the least work) is to add all Dialogs to the GameObject, then get all of them as an array using GetComponents whenever you need them (or cache them at appropriate times).
Thanks i shall have a look into that, still kinda new to the editor scripting so its all a bit trial and error for me. The reason i started doing it like this was to make it easier for others to use so they can't enter the wrong stuff. Thanks again!
Your answer
Follow this Question
Related Questions
Using DuplicateCommand and DeleteCommand with Properties 0 Answers
Editor Input Dialog 2 Answers
Can I serialize data in an Editor class? 1 Answer
Create Multiple Foldouts. 1 Answer
Handling Undo/REdo in EditorWindow when editing DB record. 0 Answers