x


custom class serialization problem

I made a class like this

[System.Serializable]
public class AudioClipPlayer : Object
{
    private AudioClip clip;
    private float repeat;
};

and on the Inspector, it appears like this

-----------------------------------------------------
Audio Clip Player          None (Audio Clip Player) @
-----------------------------------------------------

Here's the problem I want to drag and drop a audio file to the 'None' area. But I have no idea what I have to do. I want to look like this

----------------------------------------------
Audio Clip Player          None (Audio Clip) @
----------------------------------------------

And I want to look exactly like above.. because I wanna make a array for AudioClipPlayer like this

[System.Serializable]
public class ZombiAudioClips
{
    public AudioClipPlayer[] idleClips;
}

I want to make that Unity recognize AudioClipPlayer to AudioClip. Is that possible? Thx for reading.

more ▼

asked Jan 04, 2011 at 09:51 AM

planC gravatar image

planC
5 16 11 18

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Remove the : Object for a start, it'll be making your class inherit UnityEngine.Object which makes very little sense. After that, you'll want to either make the fields public, or add [SerializeField] above both of them

[System.Serializable] public class AudioClipPlayer {

 public AudioClip clip;
 public float repeat;

};

or

[System.Serializable]
public class AudioClipPlayer
{
    [SerializeField]
    private AudioClip clip;
    [SerializeField]
    private float repeat; //note, these aren't going to be accessible from outside the class
};
more ▼

answered Jan 04, 2011 at 09:57 AM

Mike 3 gravatar image

Mike 3
33.3k 21 93 299

thank you Mike. but I want exactly looking like above.. because I wanna make a array for AudioClipPlayer like this [System.Serializable] public class ZombiAudioClips { public AudioClipPlayer[] idleClips; }

Jan 04, 2011 at 10:10 AM planC

Then it can't work. If you change it to how i'm saying, it should work as you want it. I'll add in a fixed class as an example

Jan 04, 2011 at 10:19 AM Mike 3

I would go with the top example, the bottom one is probably going to have protection issues trying to access the private members

Jan 04, 2011 at 10:22 AM Mike 3

It does not work for the code 'AudioClipPlayer[] idleClips'. I want to add some audio files to idleClips.

Jan 04, 2011 at 10:43 AM planC

Which one are you trying?

Jan 04, 2011 at 11:01 AM Mike 3
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x641
x444
x301

asked: Jan 04, 2011 at 09:51 AM

Seen: 2589 times

Last Updated: Jan 04, 2011 at 12:38 PM