Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by gukosowa · Aug 01, 2013 at 06:13 PM · audiotexture2dserializationdatabasecorrection

Serializing AudioClip or Texture2D correction

Im about to build a GameManager, with all premade data in it. Then i was confronted with saving data and serializtion, haha i spend maybe a week to solve this, after written a punch of code.

So my question actually (as a beginner of coding in c#) is that the method to serialize Audio and Textures like i do is a good solution?

I save to file with the BinaryFormatter.

Audio:

 using UnityEngine;
 using System.Collections;
 [System.Serializable]
 public class AUDIO {
     public AUDIO(audioPath path,string clipname) 
     {
         fileName = clipname;
         fileEnum = path;
     }
     public AUDIO(audioPath path) 
     {
         fileName = "";
         fileEnum = path;
     }
     // Use this for initialization
     public string fileName = string.Empty;
     public audioPath fileEnum;
     public bool loop;
     public float volume = 1;
     public float pitch = 1;
     public float panLevel = 0;
     // AUDIO.getAudio() returns audiofile
     public AudioClip getAudio () {
         if (fileName != "") {
             try {
                 if (fileEnum == audioPath.BACKGROUNDMUSIC|| fileEnum == audioPath.BACKGROUNDSOUND) {
                     loop = true;
                 }
                 return Resources.Load("Audio/"+fileEnum.ToString()+"/"+fileName) as AudioClip;
             } catch {
                 AudioClip empty = new AudioClip() as AudioClip;
                 return empty;
             }
         } else {
             AudioClip empty = new AudioClip() as AudioClip;
             return empty;
         }
     }
     public AudioClip setAudio(audioPath path,AudioClip clip) {
         if (clip != null) {
             fileName = clip.name;
             fileEnum = path;
             
         }
         return clip;
     }
 }
 public enum audioPath {
     BACKGROUNDMUSIC,
     BACKGROUNDSOUND,
     MUSICEFFECT,
     SOUNDEFFECT
 }

and Textures:

 using UnityEngine;
 using System.Collections;
 [System.Serializable]
 public class IMG {
     public IMG(graphicPath path,string imgname) 
     {
         fileName = imgname;
         fileEnum = path;
     }
     public IMG(graphicPath path) 
     {
         fileName = "";
         fileEnum = path;
     }
     // Use this for initialization
     public string fileName = string.Empty;
     public graphicPath fileEnum;
     
     public Texture2D getTexture () {
         if (fileName != "") {
             try {
                 return Resources.Load("Graphics/"+fileEnum.ToString()+"/"+fileName) as Texture2D;
             } catch {
                 Texture2D empty = new Texture2D(0,0) as Texture2D;
                 return empty;
             }
         } else {
             Texture2D empty = new Texture2D(0,0) as Texture2D;
             return empty;
         }
     }
     public Texture2D setTexture(graphicPath path,Texture2D tex) {
         if (tex != null) {
             fileName = tex.name;
             fileEnum = path;
             
             
         }
         return tex;
     }
 }
 public enum graphicPath {
     ICONS,
     FACES
 }

Only a example to call AUDIO: In Database:

 public AUDIO TESTCLIP;

In MonoBehaviour:

         database.TESTCLIP = new AUDIO(audioPath.BGM,"BIGBOOMBGM");
         AudioClip testCLIP = database.TESTCLIP.getAudio();
         GameObject gameaudio = GameObject.Find("GameAudio");
         gameaudio.AddComponent<AudioSource>();
         gameaudio.GetComponent<AudioSource>().clip = testCLIP;
         gameaudio.GetComponent<AudioSource>().loop = database.TESTCLIP.loop;
         gameaudio.GetComponent<AudioSource>().panLevel = database.TESTCLIP.panLevel;
         gameaudio.GetComponent<AudioSource>().Play();

same thing for Textures.

Is this pice of code stable to build on?

Comment
Add comment · Show 4
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
avatar image DaveA · Aug 01, 2013 at 06:22 PM 0
Share

By 'serialize' do you mean 'save to a file'?

avatar image gukosowa · Aug 01, 2013 at 06:27 PM 0
Share

forgot to write sorry. saving to file using BinaryFormatter

avatar image gregzo · Aug 01, 2013 at 08:40 PM 0
Share

but the code you posted doesn't use BinaryFormatter... How can we tell you if it is correct? Reading what you posted, it seems like convoluted code for loading from Resources, not saving.

avatar image gukosowa · Aug 01, 2013 at 09:09 PM 0
Share

actually i use

 var stream = new FileStream(savePath+saveName, File$$anonymous$$ode.Create, FileAccess.Write, FileShare.Write);
         //create a Binary Formatter, and serialize the data into a Binary format
         //then close stream
         var formatter = new BinaryFormatter();
         formatter.Serialize(stream, database);
         stream.Close();

database is a Serializable public class. in there are multiple Audio and Texture2d (img) vars.

the question is that is it efficient to make such a class to read and store this kind of data. especially the Resources.Load on every "get" call?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by DaveA · Aug 02, 2013 at 06:10 AM

For textures, I would just use http://docs.unity3d.com/Documentation/ScriptReference/Texture2D.EncodeToPNG.html and the WWW class to read it in. Very easy.

I've not used Serialize, but on the face of it, seems like an ok solution. But yeah, putting stuff in resources is not needed. If you put in resources, it gets compiled for easy reading (I believe), but if you do your own file i/o, you can (should) put them elsewhere.

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 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

Related Questions

How to properly load textures? 2 Answers

How to get mipChain and linear values from existing Texture2D 0 Answers

Where can I find the default "serialized data" files that Unity save and load during run-time / editor time from the inspector? 0 Answers

Serialize loads of different data 0 Answers

Help saving Voxel Terrain between scenes 2 Answers


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