Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Custom Editor lost on reload

Discussion in 'Scripting' started by FisherM, Dec 13, 2014.

  1. FisherM

    FisherM

    Joined:
    Dec 28, 2013
    Posts:
    366
    So I have two dictionary files the first defines a dictionary of character blueprints and the second attack blueprints, I then have a custom editor to create the character blueprints but every time I reload the scene I completely loose all the information from the character editor. I have yet to use serialization and I'm not sure how to correctly apply it to this example.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. [System.Serializable]
    5. public class CharacterDictionary : MonoBehaviour
    6. {
    7.     Dictionary<string, CharacterPrototype> Characters = new Dictionary<string, CharacterPrototype>();
    8.     [SerializeField]
    9.     public List<CharacterPrototype> CharactersInspec;
    10.     void Start()
    11.     {
    12.         //Characters.Add ("Templar", new CharacterPrototype("Templar", ));
    13.     }
    14.     public CharacterPrototype FindItem(string name){
    15.         if (name != null) {
    16.             return(Characters [name]);
    17.         }
    18.         Debug.Log ("given null");
    19.         return(null);
    20.     }
    21.     public void DebugMe(){  
    22.         Debug.Log ("Debug");
    23.     }
    24. }
    25.  
    26.  
    27. public class CharacterPrototype : MonoBehaviour
    28. {
    29.     public string CharacterName;
    30.     public GameObject CharacterModel;
    31.  
    32.     public List<AttackPrototype> Melee_Attacks;
    33.     public List<AttackPrototype> Ranged_Attacks;
    34.  
    35.     public float SightRange;
    36.    
    37.     public CharacterPrototype(string newName, /*GameObject newModel,*/ List<AttackPrototype> newMelee, List<AttackPrototype> newRanged, float newSightRange)
    38.     {
    39.         CharacterName = newName;
    40.         /*CharacterModel = newModel;*/
    41.         Melee_Attacks = newMelee;
    42.         Ranged_Attacks = newRanged;
    43.         SightRange = newSightRange;
    44.     }
    45. }
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System;
    5. [System.Serializable]
    6. public class AttackDictionary : MonoBehaviour
    7. {
    8.     public Dictionary<string, AttackPrototype> Attacks = new Dictionary<string, AttackPrototype>();
    9.     void Start()
    10.     {
    11.         CreateDictionary ();
    12.     }
    13.    
    14.     public void CreateDictionary()
    15.     {
    16.         Attacks.Add ("Attack High", new AttackPrototype("Attack High", 60.0f, 0.0f, 2.0f, 0.1f, 40.0f, 1, false, 80.0f, 1.2f, 0.5f, 0.5f, 0.0f));
    17.         Attacks.Add ("Attack Medium_Left", new AttackPrototype("Attack Medium_Left", 35.0f, 0.0f, 2.0f, 0.1f, 40.0f, 1, false, 60.0f, 1.2f, 0.5f, 0.5f, 0.0f));
    18.         Attacks.Add ("Attack Medium_Right", new AttackPrototype("Attack Medium_Right", 35.0f, 0.0f, 2.0f, 0.1f, 40.0f, 1, false, 60.0f, 1.2f, 0.5f, 0.5f, 0.0f));
    19.         Attacks.Add ("Attack Medium_Stab", new AttackPrototype("Attack Medium_Stab", 50.0f, 0.0f, 2.0f, 0.1f, 40.0f, 1, false, 90.0f, 1.2f, 0.5f, 0.5f, 0.0f));
    20.     }
    21.  
    22.     public void DestroyDictionary()
    23.     {
    24.         Attacks = new Dictionary<string, AttackPrototype>();
    25.     }
    26.  
    27.     public AttackPrototype FindItem(string name){
    28.         if (name != null) {
    29.             return(Attacks [name]);
    30.         }
    31.         Debug.Log ("given null");
    32.         return(null);
    33.     }
    34. }
    35.  
    36.  
    37. [System.Serializable]
    38. public class AttackPrototype : MonoBehaviour
    39. {
    40.     public string AttackName;
    41.     public float AngleSwipe;
    42.     public float delay = 0.0f;
    43.  
    44.     public float MaxDistance;
    45.     public float MinDistance;
    46.    
    47.     public float Damage;
    48.     public int MaxCollisions;
    49.  
    50.     public bool Ranged;
    51.  
    52.     public float CritDamage;
    53.     public float CritDistance;
    54.     public float CritBracket;
    55.     public float CritChance;
    56.    
    57.     public float AttackFloat;
    58.  
    59.     public AttackPrototype(string NewName, float NewAngleSwipe, float NewDelay, float NewMaxDistance, float NewMinDistance, float NewDamage, int NewMaxColl, bool NewRanged,
    60.                     float NewCritDamage, float NewCritDistance, float NewCritBracket, float NewCritChance, float NewAttackFloat)
    61.     {
    62.         AttackName = NewName;
    63.         AngleSwipe = NewAngleSwipe;
    64.         delay = NewDelay;
    65.         MaxDistance = NewMaxDistance;
    66.         MinDistance = NewMinDistance;
    67.         Damage = NewDamage;
    68.         MaxCollisions = NewMaxColl;
    69.         Ranged = NewRanged;
    70.         CritDamage = NewCritDamage;
    71.         CritDistance = NewCritDistance;
    72.         CritBracket = NewCritBracket;
    73.         CritChance = NewCritChance;
    74.         AttackFloat = NewAttackFloat;
    75.     }
    76. }


    Here is the editor which I have trimmed slightly just so it is easier to read.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEditor;
    5.  
    6.  
    7. [CustomEditor(typeof(CharacterDictionary))]
    8. public class CharacterDesigner : Editor {
    9.     public enum tabs
    10.     {
    11.         Characters,
    12.         Inspector
    13.     };
    14.  
    15.     tabs ActiveTab;
    16.  
    17.     List<CharacterPrototype> Characters;
    18.  
    19.     int ActiveSelection;
    20.  
    21.     bool[] Attacks;
    22.     string visRange = "", user = "";
    23.     AttackDictionary AD;
    24.  
    25.     public override void OnInspectorGUI()
    26.     {
    27.         DrawDefaultInspector();
    28.         CharacterDictionary myScript = (CharacterDictionary)target;
    29.         Characters = myScript.CharactersInspec;
    30.         if(Characters == null)
    31.         {
    32.             Debug.Log ("no list of characters");
    33.             Characters = new List<CharacterPrototype>();
    34.         }
    35.         if(GUILayout.Button("Reset Attacks", GUILayout.Width(250)))
    36.         {
    37.             Attacks = null;
    38.         }
    39.  
    40.  
    41.         GUILayout.BeginHorizontal ();
    42.         if(GUILayout.Button("Characters", GUILayout.Width(125)))
    43.         {
    44.             ActiveTab = tabs.Characters;
    45.         }
    46.         if(GUILayout.Button("Inspector", GUILayout.Width(125)))
    47.         {
    48.             ActiveTab = tabs.Inspector;
    49.         }
    50.         GUILayout.EndHorizontal ();
    51.      
    52.     }
    53. }
     
  2. ErrorX

    ErrorX

    Joined:
    May 21, 2014
    Posts:
    21
  3. FisherM

    FisherM

    Joined:
    Dec 28, 2013
    Posts:
    366
  4. FisherM

    FisherM

    Joined:
    Dec 28, 2013
    Posts:
    366
  5. ErrorX

    ErrorX

    Joined:
    May 21, 2014
    Posts:
    21
    The Editor class is only a helper class to modify existing data from another class. If you what to store the data you need to move the List<CharacterPrototype>Characters to a MonoBehaviour class , Scriptableobject class or save it to a file, like XML. Then use the editor class to load the data so the end user can modify it.