How to use _beforeTokenTransfer for NFT royalty?

Now I am building NFT marketplace and going to implement NFT royalty.
Can I implement royalty using _beforeTokenTransfer function?

function _beforeTokenTransfer(
      address operator,
      address from,
      address to,
      uint256[] memory ids,
      uint256[] memory amounts,
      bytes memory data
) internal virtual override(ERC1155, ERC1155Pausable, ERC1155Supply) {
    super._beforeTokenTransfer(operator, from, to, ids, amounts, data);
}

Please help how to customize this function for royalty.
Thank you.

Yes you can, but before, please take a look at our implementation of royalties in case it will be useful to you.