3
\$\begingroup\$

For the basic values I know how to do it like:

  • Item name (Simple string value)
  • Amount (Simple int value)
  • isNormalDrop (Simple bool value)

The problem is saving the Reflection variable (The script part near the bottom) which is of type 'UnityEngine.Object' and the GameObject variable how would I do this?

Custom Inspector

\$\endgroup\$
3
  • \$\begingroup\$ @Gnemlock The "AmmoPack"(GameObject) is a prefab in the assets and the Reflection variable is the script object the only problem is I don't know how to retrieve these from Assets as they aren't part of the scene \$\endgroup\$ Commented Sep 2, 2016 at 14:01
  • \$\begingroup\$ @Gnemlock You can drop in any script in the project the field next to it that currently has "AddMagWorth" is a public function in that script that it can call. (Although its easy to save this I just need the name of the method), the script object is needed to create a delegate of the method to then call from a seperate object in this case a Pickup item, the problem is saving the script object - I can attach the editor script if you like? \$\endgroup\$ Commented Sep 2, 2016 at 14:40
  • \$\begingroup\$ @Gnemlock as long as everything can be loaded correctly it doesn't need to be a .txt file , as there will probably be a lot of scripts I don't think a switch statement would work well it would have to be some way of either finding the script in the assets folder or loading the Object directly from the save \$\endgroup\$ Commented Sep 2, 2016 at 14:59

1 Answer 1

1
\$\begingroup\$

I figured out how to do this its using Unity's Asset Database and saving the scripts name as well as the Gameobjects name in order to retrieve them. keep in mind that AssetDatabase is UnityEditor only so to load assets during game time you need to put them in Resources and use Resources.Load

I save the Inspectors data in a XML File on a button click and then Load the data on button click and OnEnable. Here is the doc for XML Writing and Reading

Here is my Load and Save code just so you get an Idea:

Loading Data:

private void LoadItems(){
    var Path = Application.persistentDataPath + "//XMLSaves/data.xml";
    XmlDocument Document = new XmlDocument();
    Document.Load(Path);

    List<string> NamesL = new List<string>();

    Names.Clear();
    Prefabs.Clear();
    Script.Clear();
    isDropNormal.Clear();

    XmlNodeList XmlItems = Document.DocumentElement.SelectNodes("//Items/Item");
    foreach (XmlNode Node in XmlItems){          
        Names.Add(Node.SelectSingleNode("Name").InnerText);

        string Prefab = Node.SelectSingleNode("Prefab").InnerText;
        string[] Folders = new string[]{"Assets/Scripts/Drops/Prefabs"};
        string AssetPath = GetAssetPath(Prefab, Folders);
        GameObject Asset = (GameObject)AssetDatabase.LoadAssetAtPath(AssetPath,typeof(GameObject));
        Prefabs.Add(Asset);

        string ScriptName = Node.SelectSingleNode("Script").InnerText;
        string ScriptPath = GetAssetPath(ScriptName + " t:MonoScript");
        MonoScript MonoScriptPath = (MonoScript)AssetDatabase.LoadAssetAtPath(ScriptPath, typeof(MonoScript));
        Script.Add(MonoScriptPath);

        Methods.Add(Node.SelectSingleNode("Methods").InnerText);

        bool isDropNormalT = bool.Parse(Node.SelectSingleNode("isDropNormal").InnerText);
        isDropNormal.Add(isDropNormalT);
    }
    SizeProp = XmlItems.Count;

}

Saving Data:

private void WriteItemsToFile(){
    var Path = Application.persistentDataPath + "//XMLSaves";
    System.IO.Directory.CreateDirectory(Path);

    if (System.IO.File.Exists(Path + "//data.xml")){
        System.IO.File.Delete(Path + "//data.xml");
    }

    XmlWriterSettings settings = new XmlWriterSettings
    {
        Indent = true,
        IndentChars = "  ",
        NewLineChars = "\r\n",
        NewLineHandling = NewLineHandling.Replace
    };

    XmlWriter Writer = XmlWriter.Create(Path + "//data.xml", settings);

    Writer.WriteStartDocument();
    Writer.WriteStartElement("Items");
    for (int I = 0;I < SizeProp; I++){
        Writer.WriteStartElement("Item");
        Writer.WriteElementString("Name", Names[I]);
        Writer.WriteElementString("Prefab", Prefabs[I].name);
        Writer.WriteElementString("Script", Script[I].name);
        Writer.WriteElementString("Methods", Methods[I]);
        Writer.WriteElementString("isDropNormal", isDropNormal[I].ToString());
        Writer.WriteEndElement();
    }
    Writer.WriteEndElement();
    Writer.WriteEndDocument();

    Writer.Close();
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .