The trailer for “The Belko Experiment” came to James Gunn in a dream.

And if you’re thinking to yourself, “Gee, when I have dreams, they aren’t generally trailers for movies about everyday office workers that are compelled to violently kill each other in a ‘Battle Royale’-like experiment,” well, you probably don’t have a history of making gritty genre pictures like “Super” (Rainn Wilson decides he’s meant to be a superhero, starts killing people) or the 2006 zombie flick “Slither,” among others.

“I dreamt the trailer for this movie,” says Gunn, 46. “It’s really that simple. I woke up one morning and I dreamt the entire trailer, almost exactly as the trailer is being shown. ... That’s what it was. I woke up and I wanted to see that movie. So I wrote it, just kind of finding out what happened, what happened to characters.”

A process that began during a REM cycle culminates on Friday with the release of the movie, in which a crew of mostly American office workers doing some sort of bland contracting work in Colombia are locked into their building and told that a large number of them will die by a certain time, if a slightly lower number aren’t killed first.

Gunn wrote the script but begged off directing the picture starring an ensemble ranging from John Gallagher Jr. to Michael Rooker and Melonie Diaz. His replacement, after the dormant project was revived several years later, is Australian Greg McLean, best known for “Wolf Creek.”

“I was going to direct this movie when I had financing and first wrote the script, and was going to go down to Brazil to shoot it,” Gunn says, “but at that time I went through a divorce [from actress Jenna Fischer in 2007] and it didn’t seem to me like the thing I wanted to do at that point in my life was to go down to Brazil and make a movie about people shooting each other in the head. I wanted to be with my friends and family.”

Another life event has kept Gunn away from directing smaller projects that were once his forte: Marvel hired him to helm its “Guardians of the Galaxy” franchise. The first installment grossed more than $770 million worldwide in 2014 and earned universal critical acclaim. “Guardians of the Galaxy Vol. 2” arrives on May 5.

The budgets are higher, expectations are greater, but Gunn feels he’s found his calling in his signature series.

“Completely honestly, especially ‘Guardians Vol. 2,’ is the most creatively fulfilling and free experience I’ve had in my 20-something years of making movies,” he says.