Example #1
0
    private void SaveData()
    {
        if (roomObjContainer.transform.childCount == 0)
        {
            Debug.LogError("No Room Game Object to save into room under " + roomObjGameObj + "!");
            return;
        }
        GameObject roomObj = roomObjContainer.transform.GetChild(0).gameObject;


        // serialized data i think
        SerializedProperty prop    = serializedObject.FindProperty("currentRoom");
        SerializedObject   propObj = new SerializedObject(prop.objectReferenceValue);

        propObj.FindProperty("roomName").stringValue    = serializedObject.FindProperty("roomName").stringValue;
        propObj.FindProperty("roomType").enumValueIndex = serializedObject.FindProperty("roomType").enumValueIndex;

        propObj.FindProperty("isJungle").boolValue  = serializedObject.FindProperty("isJungle").boolValue;
        propObj.FindProperty("isDungeon").boolValue = serializedObject.FindProperty("isDungeon").boolValue;
        propObj.FindProperty("isTemple").boolValue  = serializedObject.FindProperty("isTemple").boolValue;

        //propObj.FindProperty("hasNorthDoor").boolValue = serializedObject.FindProperty("hasNorthDoor").boolValue;
        //propObj.FindProperty("hasEastDoor").boolValue = serializedObject.FindProperty("hasEastDoor").boolValue;
        //propObj.FindProperty("hasSouthDoor").boolValue = serializedObject.FindProperty("hasSouthDoor").boolValue;
        //propObj.FindProperty("hasWestDoor").boolValue = serializedObject.FindProperty("hasWestDoor").boolValue;

        // tried doing this stuff originally:
        // https://answers.unity.com/questions/778647/objectreferencevalue-in-serializedproperty.html
        // switching to just saving with roomName


        string rName = serializedObject.FindProperty("roomName").stringValue;

        propObj.FindProperty("roomObjectPath").stringValue = gObjPath + rName;

        propObj.ApplyModifiedProperties();

        _target.Room2String();

        propObj.ApplyModifiedProperties();

        roomObj.name = rName;
        Room room = roomObj.GetComponent <Room>();

        room.roomData = _target.currentRoom;
        room.SetDoorRefs();
        bool   created  = false;
        string pathName = "Assets/Resources/" + gObjPath + rName + ".prefab";

        // should i check overriding?
        PrefabUtility.SaveAsPrefabAssetAndConnect(roomObj, pathName, InteractionMode.UserAction, out created);


        propObj.ApplyModifiedProperties();
        AssetDatabase.SaveAssets(); // not needed? having issues saving prefabs after quiting unity
    }