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.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[System.Serializable]
public class CharacterDictionary : MonoBehaviour
{
Dictionary<string, CharacterPrototype> Characters = new Dictionary<string, CharacterPrototype>();
[SerializeField]
public List<CharacterPrototype> CharactersInspec;
void Start()
{
//Characters.Add ("Templar", new CharacterPrototype("Templar", ));
}
public CharacterPrototype FindItem(string name){
if (name != null) {
return(Characters [name]);
}
Debug.Log ("given null");
return(null);
}
public void DebugMe(){
Debug.Log ("Debug");
}
}
public class CharacterPrototype : MonoBehaviour
{
public string CharacterName;
public GameObject CharacterModel;
public List<AttackPrototype> Melee_Attacks;
public List<AttackPrototype> Ranged_Attacks;
public float SightRange;
public CharacterPrototype(string newName, /*GameObject newModel,*/ List<AttackPrototype> newMelee, List<AttackPrototype> newRanged, float newSightRange)
{
CharacterName = newName;
/*CharacterModel = newModel;*/
Melee_Attacks = newMelee;
Ranged_Attacks = newRanged;
SightRange = newSightRange;
}
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
[System.Serializable]
public class AttackDictionary : MonoBehaviour
{
public Dictionary<string, AttackPrototype> Attacks = new Dictionary<string, AttackPrototype>();
void Start()
{
CreateDictionary ();
}
public void CreateDictionary()
{
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));
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));
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));
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));
}
public void DestroyDictionary()
{
Attacks = new Dictionary<string, AttackPrototype>();
}
public AttackPrototype FindItem(string name){
if (name != null) {
return(Attacks [name]);
}
Debug.Log ("given null");
return(null);
}
}
[System.Serializable]
public class AttackPrototype : MonoBehaviour
{
public string AttackName;
public float AngleSwipe;
public float delay = 0.0f;
public float MaxDistance;
public float MinDistance;
public float Damage;
public int MaxCollisions;
public bool Ranged;
public float CritDamage;
public float CritDistance;
public float CritBracket;
public float CritChance;
public float AttackFloat;
public AttackPrototype(string NewName, float NewAngleSwipe, float NewDelay, float NewMaxDistance, float NewMinDistance, float NewDamage, int NewMaxColl, bool NewRanged,
float NewCritDamage, float NewCritDistance, float NewCritBracket, float NewCritChance, float NewAttackFloat)
{
AttackName = NewName;
AngleSwipe = NewAngleSwipe;
delay = NewDelay;
MaxDistance = NewMaxDistance;
MinDistance = NewMinDistance;
Damage = NewDamage;
MaxCollisions = NewMaxColl;
Ranged = NewRanged;
CritDamage = NewCritDamage;
CritDistance = NewCritDistance;
CritBracket = NewCritBracket;
CritChance = NewCritChance;
AttackFloat = NewAttackFloat;
}
}
Here is the editor which I have trimmed slightly just so it is easier to read.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
[CustomEditor(typeof(CharacterDictionary))]
public class CharacterDesigner : Editor {
public enum tabs
{
Characters,
Inspector
};
tabs ActiveTab;
List<CharacterPrototype> Characters;
int ActiveSelection;
bool[] Attacks;
string visRange = "", user = "";
AttackDictionary AD;
public override void OnInspectorGUI()
{
DrawDefaultInspector();
CharacterDictionary myScript = (CharacterDictionary)target;
Characters = myScript.CharactersInspec;
if(Characters == null)
{
Debug.Log ("no list of characters");
Characters = new List<CharacterPrototype>();
}
if(GUILayout.Button("Reset Attacks", GUILayout.Width(250)))
{
Attacks = null;
}
GUILayout.BeginHorizontal ();
if(GUILayout.Button("Characters", GUILayout.Width(125)))
{
ActiveTab = tabs.Characters;
}
if(GUILayout.Button("Inspector", GUILayout.Width(125)))
{
ActiveTab = tabs.Inspector;
}
GUILayout.EndHorizontal ();
}
}