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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by AmbitiousAmateur · Mar 21, 2014 at 10:03 AM · c#editorcustom-inspectorcustom-editor

Create Spells through classes in custom editor?

Hi All

I'm trying to create a spellbook through a custom editor and having little success so far.

I have a class ASpell which contains the properties within each spell and a dictionary of spells to store all these. So far I've managed to create a very basic custom editor where I can populate the list though have no idea how to ensure these aren't wiped when the game is launched.

I've looked at SerializeField, System.Serialize, ScriptableObject but I can't manage to solve this, what I'd like to think, trivial problem.

If someone could give me some pointers for what I'd need in the SpellBook class script and SpellBookEditor class script, where I need to place Serialize or whatever else I need and how to create more than one of these (if necessary) without a new overwriting everything I've done to an old dictionary.

ASpell.cs

 public class ASpell {
    public string _name;
    public int _level;
 
    public ASpell(string name, int level){
       _name = name;
       _level = level;
 
    }
 }

SpellBook.cs

 public class SpellBook : MonoBehaviour {
    public Dictionary<string, ASpell> spells = new Dictionary<string, ASpell>();

    public void Add(string name){
       ASpell spell = new ASpell(name, 1);
       spells.Add(name, spell);
    }
 }

SpellBookEditor.cs

 [CustomEditor(typeof(SpellBook))]
 public class SpellBookEditor : Editor {
    private SpellBook sb;
 
    void OnEnable(){
       sb = (SpellBook)target;
    }
 
    public override void OnInspectorGUI(){
       foreach(ASpell spell in sb.spells.Values){
          s._name = GUILayout.TextArea(s._name);
       }
 
    }
 }

Any help on this would be greatly appreciated! Thanks in advance

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
0
Best Answer

Answer by whydoidoit · Mar 21, 2014 at 10:06 AM

Your problem is that you cannot serialize a Dictionary using Unity's serialization. It's a total pain. You will need to store the spells in a List and then convert them into a Dictionary for access performance when the game starts.

Also Unity will not serialize anything that doesn't have a default constructor, so your spell also needs a parameterless constructor in addition to any parameter based ones.

ScriptableObject could be used if you wanted Spell assets which you could add in the editor, fiddle with the settings in the Inspector etc. ScriptableObjects will also be kept unique when serialized, ordinary classes will be duplicated if they appear in more than one reference (after deserialization).

Comment
Add comment · Show 2 · 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 AmbitiousAmateur · Mar 21, 2014 at 10:49 AM 0
Share

That's great, though not great, but thanks for the answer!

avatar image vexe · Mar 21, 2014 at 02:03 PM 0
Share

check out full inspector - his serialization methods are great!

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

22 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Prefabs aren't saving with Undo.RecordObject 4 Answers

How to access Custom Inspector own gameObject 1 Answer

Distribute terrain in zones 3 Answers

DrawDefaultInspector of another script in custom inspector 1 Answer

Inspector: custom property with custom type use default editor 3 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