- Home /
The question is answered, right answer was accepted
Replace AssetDatabase with Resources
So I am working on test of sorts for now it works in editor just fine because im using AssetDatabase to find and give me the file path however that doesnt work in built version of the game i know resources are said to beable do it but i havent found any examples that give the file location
(Yes I have all items i am looking for in a folder within the resources folder yes i know it will make final folder size larger)
here is my code bellow
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Linq;
using UnityEditor;
using UnityEngine.UI;
public class Item : MonoBehaviour
{
public Text TextDisplay;
public Object[] AllItems;
public int x = 0;
public ItemData ItemDat;
public PrimalItem ItemPrefab;
[System.Serializable]
public class ItemData
{
public string ItemPath;
public string CustomName;
public float Price;
}
// Start is called before the first frame update
void Start()
{
AllItems = Resources.LoadAll("", typeof(PrimalItem));
}
// Update is called once per frame
void Update()
{
TextDisplay.text = ItemPrefab.name;
}
public void Next()
{
if(x < AllItems.Length - 1)
{
x++;
ItemPrefab = (PrimalItem)AllItems[x];
}
else
{
x = 0;
}
}
public void Back()
{
if(x >= 1)
{
x--;
ItemPrefab = (PrimalItem)AllItems[x];
}
else
{
x = 0;
}
}
public void SaveFile()
{
ItemDat.ItemPath = AssetDatabase.GetAssetPath(ItemPrefab);
string destination = Application.persistentDataPath.ToString() + "/Save.dat";
FileStream file;
if (File.Exists(destination)) file = File.OpenWrite(destination);
else file = File.Create(destination);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(file, ItemDat);
file.Close();
}
public void LoadFile()
{
string destination = Application.persistentDataPath.ToString() + "/Save.dat";
FileStream file;
if (File.Exists(destination))
{
file = File.OpenRead(destination);
}
else
{
Debug.LogError("File Not Found");
return;
}
BinaryFormatter bf = new BinaryFormatter();
ItemDat = (ItemData)bf.Deserialize(file);
ItemPrefab = (PrimalItem)AssetDatabase.LoadAssetAtPath(ItemDat.ItemPath, typeof(Object));
x = 0;
file.Close();
}
}
Answer by darkal · Aug 02, 2019 at 04:06 PM
I was able figure it out and i tested it and it does function in built version of the game instead i stored everything in array and i referenced what save it as by string (Will change to int or id later) then checked my array for a match via the name this ofc will be replaced by id later but it functions as intended
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Linq;
using UnityEditor;
using UnityEngine.UI;
public class Item : MonoBehaviour
{
public Text TextDisplay;
public Object[] AllItems;
public int x = 0;
public ItemData ItemDat;
public PrimalItem ItemPrefab;
[System.Serializable]
public class ItemData
{
public string ItemPath;
public string CustomName;
public float Price;
}
// Start is called before the first frame update
void Start()
{
AllItems = Resources.LoadAll("", typeof(PrimalItem));
}
// Update is called once per frame
void Update()
{
TextDisplay.text = ItemPrefab.name;
ItemDat.ItemPath = ItemPrefab.name;
}
public void Next()
{
if(x < AllItems.Length - 1)
{
x++;
ItemPrefab = (PrimalItem)AllItems[x];
}
else
{
x = 0;
}
}
public void Back()
{
if(x >= 1)
{
x--;
ItemPrefab = (PrimalItem)AllItems[x];
}
else
{
x = 0;
}
}
public void SaveFile()
{
string destination = Application.persistentDataPath.ToString() + "/Save.dat";
FileStream file;
if (File.Exists(destination)) file = File.OpenWrite(destination);
else file = File.Create(destination);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(file, ItemDat);
file.Close();
}
public void LoadFile()
{
string destination = Application.persistentDataPath.ToString() + "/Save.dat";
FileStream file;
if (File.Exists(destination))
{
file = File.OpenRead(destination);
}
else
{
Debug.LogError("File Not Found");
return;
}
BinaryFormatter bf = new BinaryFormatter();
ItemDat = (ItemData)bf.Deserialize(file);
for (int i = 0; i < AllItems.Length; i++)
{
if(AllItems[i].name == ItemDat.ItemPath)
{
ItemPrefab = (PrimalItem)AllItems[i];
}
}
x = 0;
file.Close();
}
}