use of resources

one,

A resource can also be referenced in the script

For example:

AudioClip audio file

Texture texture map

Material material

public AudioClip Audioa;
void update(){
    if(input.GetKeyDown(KeyCode.A)){
        AudioSource audioSource = GetComponent<AudioSource>();
        audioSource.PlayOneShot(Audioa);
    }
}

2. Resource array

An array type can also be defined in the script

For example, a music box stores a lot of music

public AudioClip[] Audios;

practise:

Create a music box, click the mouse to switch randomly

in,

index = Random.Range(min,max)

Used to randomly extract a number in [min.max), excluding max

public AudioClip[] AudioBox;

Define an array type to store music and refresh the inspector. Audio files can be dropped into the array by dragging them.

void start(){
    if(audiobox==null || audiobox.length==0){
        Debug.Log("fail")
    }
}

void update(){
    
    if(Input.GetMouseButtonDown(0)){
         NextSong();
    }
}
public void NextSong(){
    int index = Random.Range(0,audiobox.length);
    AudioSource a = GetComponent(AudioSource);
    AudioClip clip = this.audiobox[index];
    a.clip = clip;
    a.Play();
    

}

 

Guess you like

Origin blog.csdn.net/helongss/article/details/130091315