A federal judge in North Carolina approved a settlement on Tuesday that prohibits the state government from banning transgender people from using bathrooms in state buildings that match their gender identity, ending a yearslong legal battle that prompted a divisive cultural debate.

The settlement, proposed by civil rights groups and Gov. Roy Cooper, a Democrat, could also end a lawsuit challenging the constitutionality of two bathroom bills that thrust North Carolina into the epicenter of the nation’s culture wars over protections for L.G.B.T. people.

The settlement concerns the use of bathrooms in public buildings like state offices and highway rest stops. Left unresolved, however, was a part of the lawsuit challenging a moratorium on local anti-discrimination ordinances.

“It feels like a consolation prize,” said Joaquin Carcano, a transgender man and the lead plaintiff in the case. “Legally we can’t be punished under these laws, but this resolution doesn’t provide safety or peace of mind when we can still be fired for being who we are.”