Quantcast
Channel: Questions in topic: "correction"
Viewing all articles
Browse latest Browse all 41

Serializing AudioClip or Texture2D correction

$
0
0
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(); gameaudio.GetComponent().clip = testCLIP; gameaudio.GetComponent().loop = database.TESTCLIP.loop; gameaudio.GetComponent().panLevel = database.TESTCLIP.panLevel; gameaudio.GetComponent().Play(); same thing for Textures. Is this pice of code stable to build on?

Viewing all articles
Browse latest Browse all 41

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>