Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by figman123456 · Nov 06, 2013 at 01:43 AM · guieditorserializationcustom-editordialog

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.

alt text

This is what i get if i use it in the conversation class without inheriting.

alt text

I'm really stuck so any help would be greatly appreciated :)

Thanks, Adam.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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).

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image figman123456 · Nov 06, 2013 at 02:10 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges