Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Aug 09, 2019 at 12:05 AM by darkal for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by darkal · Aug 02, 2019 at 03:10 AM · databasefileresourcesassetdatabasebinaryformatter

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();
     }
 }
 

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
0
Best Answer

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();
         }
     }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

113 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to access a text file from a built unity generated android application 0 Answers

Move file from streaming assets to other directory 1 Answer

HFS+ Compression Making Files Too Big 0 Answers

Can not load csv file 0 Answers

Loading subassets at runtime 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges