Upvote:3
Except if you got a (somewhat uncommon) ‘limited territorial validity‘ visa (with a specific list of countries in the “valid for“ field), a Schengen visa is valid for all Schengen countries. There is no requirement to enter or leave through the country that issued it per se.
But in principle, if you were able to get a visa from Hungary, it means that Hungary was supposed to be your main destination, and with a single entry visa, it's best to stick to the plan you submitted to the consulate.
To the extent that you want to deviate from your plan and visit countries you hadn't planned to visit, doing it in the middle of the trip would make detection more difficult but it is not any more or any less problematic than doing at the middle or at the end of the trip.